001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.db;
017:
018: import java.sql.Connection;
019: import org.jamwiki.UserHandler;
020: import org.jamwiki.model.WikiUserInfo;
021: import org.jamwiki.utils.Encryption;
022: import org.jamwiki.utils.WikiLogger;
023:
024: /**
025: * Implementation of the {@link org.jamwiki.UserHandler} interface that uses a
026: * database for storing user login, password and other basic user information.
027: */
028: public class DatabaseUserHandler implements UserHandler {
029:
030: private static final WikiLogger logger = WikiLogger
031: .getLogger(DatabaseUserHandler.class.getName());
032:
033: /**
034: *
035: */
036: public void addWikiUserInfo(WikiUserInfo userInfo,
037: Object transactionObject) throws Exception {
038: Connection conn = null;
039: try {
040: conn = WikiDatabase.getConnection(transactionObject);
041: WikiDatabase.queryHandler().insertWikiUserInfo(userInfo,
042: conn);
043: } catch (Exception e) {
044: DatabaseConnection.handleErrors(conn);
045: throw e;
046: } finally {
047: WikiDatabase.releaseConnection(conn, transactionObject);
048: }
049: }
050:
051: /**
052: *
053: */
054: public boolean authenticate(String username, String password)
055: throws Exception {
056: Connection conn = null;
057: try {
058: conn = DatabaseConnection.getConnection();
059: // password is stored encrypted, so encrypt password
060: String encryptedPassword = Encryption.encrypt(password);
061: WikiResultSet rs = WikiDatabase.queryHandler()
062: .lookupWikiUser(username, encryptedPassword, conn);
063: return (rs.size() == 0) ? false : true;
064: } catch (Exception e) {
065: DatabaseConnection.handleErrors(conn);
066: throw e;
067: } finally {
068: DatabaseConnection.closeConnection(conn);
069: }
070: }
071:
072: /**
073: *
074: */
075: private WikiUserInfo initWikiUserInfo(WikiResultSet rs)
076: throws Exception {
077: WikiUserInfo userInfo = new WikiUserInfo();
078: userInfo
079: .setUserId(rs.getInt(AnsiDataHandler.DATA_WIKI_USER_ID));
080: userInfo.setUsername(rs.getString("login"));
081: userInfo.setEmail(rs.getString("email"));
082: userInfo.setFirstName(rs.getString("first_name"));
083: userInfo.setLastName(rs.getString("last_name"));
084: userInfo.setEncodedPassword(rs.getString("encoded_password"));
085: return userInfo;
086: }
087:
088: /**
089: *
090: */
091: public boolean isWriteable() {
092: return true;
093: }
094:
095: /**
096: *
097: */
098: public WikiUserInfo lookupWikiUserInfo(String username)
099: throws Exception {
100: WikiResultSet rs = WikiDatabase.queryHandler()
101: .lookupWikiUserInfo(username);
102: return (rs.size() == 0) ? null : initWikiUserInfo(rs);
103: }
104:
105: /**
106: *
107: */
108: public void updateWikiUserInfo(WikiUserInfo userInfo,
109: Object transactionObject) throws Exception {
110: Connection conn = null;
111: try {
112: conn = WikiDatabase.getConnection(transactionObject);
113: WikiDatabase.queryHandler().updateWikiUserInfo(userInfo,
114: conn);
115: } catch (Exception e) {
116: DatabaseConnection.handleErrors(conn);
117: throw e;
118: } finally {
119: WikiDatabase.releaseConnection(conn, transactionObject);
120: }
121: }
122: }
|