LdapUserInfoProvider.java 10.8 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package org.jivesoftware.messenger.ldap;

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.UserInfo;
import org.jivesoftware.messenger.user.UserInfoProvider;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.user.spi.BasicUserInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.naming.NamingEnumeration;
import javax.naming.directory.*;

/**
32 33 34 35 36
 * LDAP implementation of the UserInfoProvider interface. The LdapUserIDProvider
 * can operate in two modes -- in the pure LDAP mode, all user data is stored in
 * the LDAP store. This mode generally requires modifications to the LDAP schema
 * to accommodate data that Messenger needs. In the mixed mode, data that Messenger
 * needs is stored locally.
Matt Tucker's avatar
Matt Tucker committed
37 38 39 40
 *
 * @author Jim Berrettini
 */
public class LdapUserInfoProvider implements UserInfoProvider {
41

Matt Tucker's avatar
Matt Tucker committed
42
    private static final String LOAD_USER_BY_ID =
43 44
        "SELECT name, nameVisible, email, emailVisible, " +
        "creationDate, modificationDate FROM jiveUser WHERE userID=?";
Matt Tucker's avatar
Matt Tucker committed
45
    private static final String INSERT_USER =
46 47 48
        "INSERT INTO jiveUser (userID, password, name, nameVisible, " +
        "email, emailVisible, creationDate, modificationDate) " +
        "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
Matt Tucker's avatar
Matt Tucker committed
49
    private static final String SAVE_USER =
50 51 52 53 54
        "UPDATE jiveUser SET name=?, nameVisible=?, email=?," +
        "emailVisible=?, creationDate=?, modificationDate=? WHERE " +
        "userID=?";

    private LdapManager manager;
Matt Tucker's avatar
Matt Tucker committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    /**
     * Constructor initializes the internal LdapManager instance.
     */
    public LdapUserInfoProvider() {
        manager = LdapManager.getInstance();
    }

    /**
     * <p>Obtain the UserInfo of a user. Will retrieve either from LDAP or locally, depending on mode of operation.</p>
     *
     * @param id
     * @return a user info object.
     * @throws UserNotFoundException
     */
    public UserInfo getInfo(long id) throws UserNotFoundException {
        if (manager.getMode() == LdapManager.ALL_LDAP_MODE) {
            return getInfoFromLdap(id);
        }
        UserInfo info = null;
        try {
            info = getInfoFromDb(id);
        }
        catch (UserNotFoundException e) {
            info = generateNewUserInfoInDb(id);
        }
        return info;
    }

    /**
     * <p>Sets the user's info. In pure LDAP mode, this is unsupported.</p>
     *
     * @param id   user ID for setting info.
     * @param info to set.
     * @throws UserNotFoundException
     * @throws UnauthorizedException
     * @throws UnsupportedOperationException
     */
    public void setInfo(long id, UserInfo info)
            throws UserNotFoundException, UnauthorizedException, UnsupportedOperationException {
        if (manager.getMode() == LdapManager.ALL_LDAP_MODE) { // can't do this in LDAP
            throw new UnsupportedOperationException("All LDAP mode: Cannot modify data in LDAP.");
        }
        // in mixed mode, update the database.
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SAVE_USER);
            pstmt.setString(1, info.getName());
            pstmt.setInt(2, info.isNameVisible() ? 1 : 0);
            pstmt.setString(3, info.getEmail());
            pstmt.setInt(4, info.isEmailVisible() ? 1 : 0);
            pstmt.setString(5, StringUtils.dateToMillis(info.getCreationDate()));
            pstmt.setString(6, StringUtils.dateToMillis(info.getModificationDate()));
            pstmt.setLong(7, id);
Derek DeMoro's avatar
Derek DeMoro committed
111
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
112 113 114 115 116 117
        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            throw new UnauthorizedException();
        }
        finally {
118 119 120 121
            try { if (pstmt != null) pstmt.close(); }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) con.close(); }
            catch (Exception e) { Log.error(e); }
Matt Tucker's avatar
Matt Tucker committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        }
    }

    /**
     * Pure LDAP method for getting info for a given userID.
     *
     * @param id of user.
     * @return UserInfo for that user.
     * @throws UserNotFoundException
     */
    private UserInfo getInfoFromLdap(long id) throws UserNotFoundException {
        BasicUserInfo userInfo = null;
        DirContext ctx = null;
        try {
            String userDN = null;
            ctx = manager.getContext();
            // Search for the dn based on the username.
            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            constraints.setReturningAttributes(new String[]{"jiveUserID"});

            StringBuffer filter = new StringBuffer();
            filter.append("(").append("jiveUserID").append("=");
            filter.append(id).append(")");

            NamingEnumeration answer = ctx.search("", filter.toString(), constraints);
            if (answer == null || !answer.hasMoreElements()) {
                throw new UserNotFoundException("User not found: " + id);
            }
            userDN = ((SearchResult)answer.next()).getName();

            // Load record.
            String[] attributes = new String[]{
                "jiveUserID", manager.getUsernameField(), manager.getNameField(),
                manager.getEmailField(), "jiveNameVisible",
                "jiveEmailVisible", "jiveCDate", "jiveMDate", "jiveProps"
            };
            Attributes attrs = ctx.getAttributes(userDN, attributes);
            id = Long.parseLong((String)attrs.get("jiveUserID").get());
            String username = (String)attrs.get(manager.getUsernameField()).get();
            String name = null;
            String email = null;
            boolean nameVisible = false;
            boolean emailVisible = false;
            Date creationDate, modificationDate;
            Attribute nameField = attrs.get(manager.getNameField());
            if (nameField != null) {
                name = (String)nameField.get();
            }
            Attribute emailField = attrs.get(manager.getEmailField());
            if (emailField != null) {
                email = (String)emailField.get();
            }
            nameVisible = new Boolean((String)attrs.get("jiveNameVisible").get()).booleanValue();
            emailVisible = new Boolean((String)attrs.get("jiveEmailVisible").get()).booleanValue();
            creationDate = new Date(Long.parseLong((String)attrs.get("jiveCDate").get()));
            modificationDate = new Date(Long.parseLong((String)attrs.get("jiveMDate").get()));
            userInfo = new BasicUserInfo(id, name, email, nameVisible, emailVisible, creationDate, modificationDate);
        }
        catch (Exception e) {
            throw new UserNotFoundException(e);
        }
        finally {
            try {
                ctx.close();
            }
            catch (Exception e) {
            }
        }
        return userInfo;
    }

    /**
     * Mixed mode method for retrieving User Info for a given user ID.
     *
     * @param id for user.
     * @return UserInfo for user.
     * @throws UserNotFoundException
     */
    private UserInfo getInfoFromDb(long id) throws UserNotFoundException {
        BasicUserInfo userInfo = null;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_USER_BY_ID);
            pstmt.setLong(1, id);

            ResultSet rs = pstmt.executeQuery();
            if (!rs.next()) {
                throw new UserNotFoundException();
            }
            // We trim() the dates before trying to parse them because some
            // databases pad with extra characters when returning the data.
            userInfo = new BasicUserInfo(id,
                    rs.getString(1), // name
                    rs.getString(3), // email
                    rs.getInt(2) == 1, // name visible
                    rs.getInt(4) == 1, // email visible
                    new java.util.Date(Long.parseLong(rs.getString(5).trim())), // creation date
                    new java.util.Date(Long.parseLong(rs.getString(6).trim()))); // modification date

        }
        catch (SQLException e) {
            throw new UserNotFoundException("Failed to read user " + id + " from database.", e);
        }
        catch (NumberFormatException nfe) {
            Log.error("WARNING: There was an error parsing the dates " +
                    "returned from the database. Ensure that they're being stored " +
                    "correctly.");
            throw new UserNotFoundException("User with id "
                    + id + " could not be loaded from the database.");
        }
        finally {
236 237 238 239
            try { if (pstmt != null) pstmt.close(); }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) con.close(); }
            catch (Exception e) { Log.error(e); }
Matt Tucker's avatar
Matt Tucker committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        }
        return userInfo;
    }

    /**
     * Mixed mode method for creating default UserInfo locally for a new user.
     *
     * @param id
     * @return UserInfo for that user.
     */
    private UserInfo generateNewUserInfoInDb(long id) {
        Connection con = null;
        PreparedStatement pstmt = null;
        Date now = new Date();
        try {
            // Add the user record in jiveUser
            pstmt = con.prepareStatement(INSERT_USER);
            pstmt.setLong(1, id);
            pstmt.setString(2, "");
            pstmt.setString(3, "");
            pstmt.setInt(4, 1); // name visible
            pstmt.setString(5, "");
            pstmt.setInt(6, 0); // email visible
            pstmt.setString(7, StringUtils.dateToMillis(now));
            pstmt.setString(8, StringUtils.dateToMillis(now));
Derek DeMoro's avatar
Derek DeMoro committed
265
            pstmt.executeUpdate();
Matt Tucker's avatar
Matt Tucker committed
266 267 268 269 270
        }
        catch (SQLException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        finally {
271 272 273 274
            try { if (pstmt != null) pstmt.close(); }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) con.close(); }
            catch (Exception e) { Log.error(e); }
Matt Tucker's avatar
Matt Tucker committed
275 276 277 278 279 280 281 282 283 284 285
        }
        return new BasicUserInfo(id,
                "", // name
                "", // email
                true, // name visible
                false, // email visible
                now, // creation date
                now);
    }

}