001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.users.ip;
017
018import service.tut.pori.users.UserCore;
019import core.tut.pori.dao.clause.SQLClause.SQLType;
020import core.tut.pori.dao.SQLDAO;
021import core.tut.pori.users.UserIdentity;
022
023/**
024 * DAO for retrieving user details for an IP address.
025 */
026public class IPAuthenticationDAO extends SQLDAO {
027  /* tables */
028  private static final String TABLE_USERS_IP = DATABASE+".users_ip";
029  /* columns */
030  private static final String COLUMN_IP_ADDRESS = "ip_address";
031  /* sql scripts */
032  private static final String SQL_RESOLVE_USER_ID = "SELECT "+COLUMN_COUNT+", "+COLUMN_USER_ID+" FROM "+TABLE_USERS_IP+" WHERE "+COLUMN_IP_ADDRESS+"=?";
033  private static final int[] SQL_RESOLVE_USER_ID_SQL_TYPES = {SQLType.STRING.toInt()};
034  
035  /**
036   * Makes a table lookup to determine if the given ip address have an associated user identity.
037   * This will NOT check for currently authenticated users' IP address-user identity relations.
038   * 
039   * @param ipAddress
040   * @return the user identity associated with the given ip address, or null if none
041   */
042  public UserIdentity resolveUserIdentity(String ipAddress){
043    Long userId = (Long) getJdbcTemplate().queryForMap(SQL_RESOLVE_USER_ID, new Object[]{ipAddress}, SQL_RESOLVE_USER_ID_SQL_TYPES).get(COLUMN_USER_ID);
044    return (userId == null ? null : UserCore.getUserIdentity(userId));
045  }
046}