001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Jan 3, 2005 1:20:24 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.sso;
044:
045: import java.sql.PreparedStatement;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.util.Map;
049:
050: import net.jforum.JForumExecutionContext;
051: import net.jforum.dao.UserDAO;
052: import net.jforum.entities.User;
053: import net.jforum.exceptions.ForumException;
054: import net.jforum.util.DbUtils;
055: import net.jforum.util.MD5;
056: import net.jforum.util.preferences.SystemGlobals;
057:
058: /**
059: * Default login authenticator for JForum.
060: * This authenticator will validate the input against
061: * <i>jforum_users</i>.
062: *
063: * @author Rafael Steil
064: * @version $Id: DefaultLoginAuthenticator.java,v 1.10 2007/07/28 14:17:10 rafaelsteil Exp $
065: */
066: public class DefaultLoginAuthenticator implements LoginAuthenticator {
067: private UserDAO userModel;
068:
069: /**
070: * @see net.jforum.sso.LoginAuthenticator#setUserModel(net.jforum.dao.UserDAO)
071: */
072: public void setUserModel(UserDAO userModel) {
073: this .userModel = userModel;
074: }
075:
076: /**
077: * @see net.jforum.sso.LoginAuthenticator#validateLogin(String, String, java.util.Map)
078: */
079: public User validateLogin(String username, String password,
080: Map extraParams) {
081: User user = null;
082: ResultSet rs = null;
083: PreparedStatement p = null;
084:
085: try {
086: p = JForumExecutionContext.getConnection()
087: .prepareStatement(
088: SystemGlobals.getSql("UserModel.login"));
089: p.setString(1, username);
090: p.setString(2, MD5.crypt(password));
091:
092: rs = p.executeQuery();
093: if (rs.next() && rs.getInt("user_id") > 0) {
094: user = this .userModel.selectById(rs.getInt("user_id"));
095: }
096: } catch (SQLException e) {
097: throw new ForumException(e);
098: } finally {
099: DbUtils.close(rs, p);
100: }
101:
102: if (user != null && !user.isDeleted()
103: && (user.getActivationKey() == null || user.isActive())) {
104: return user;
105: }
106:
107: return null;
108: }
109: }
|