📄 jahiausermanagerldapprovider.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.usermanager;import java.io.*;import java.util.*;import java.sql.*;import javax.naming.Context;import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.SizeLimitExceededException;import javax.naming.directory.*;import org.jahia.data.JahiaDBDOMObject;import org.jahia.data.JahiaDOMObject;import org.jahia.utils.DBRowDataFilter;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.JahiaInitializationException;import org.jahia.registries.ServicesRegistry;import org.jahia.services.acl.JahiaACLManagerService;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.services.database.JahiaIncrementorsDBService;import org.jahia.settings.JahiaPrivateSettings;import org.jahia.utils.JahiaConsole;import org.jahia.utils.JahiaTools;/** * An LDAP provider implementation for the management of users. This class works * with another UserLDAPService in org.jahia.services.ldap that contains the * configuration of the LDAP repository for user management. * * @todo Khue suggested that we might want to use the site ID to use multiple * connection to different LDAP repository. This is a very interesting suggestion * but is not yet implemented. * * @author Serge Huber * @version 2.0 */public class JahiaUserManagerLDAPProvider extends JahiaUserManagerProvider { private static String DEFAULT_CONFIGURATION_FILE = "users.ldap.properties"; private static String CONTEXT_FACTORY_PROP = "users.ldap.context.factory"; private static String LDAP_URL_PROP = "users.ldap.url"; private static String AUTHENTIFICATION_MODE_PROP = "users.ldap.authentification.mode"; private static String PUBLIC_BIND_DN_PROP = "users.ldap.public.bind.dn"; private static String PUBLIC_BIND_PASSWORD_PROP = "users.ldap.public.bind.password"; private static String UID_SEARCH_ATTRIBUTE_PROP = "users.ldap.uid.search.attribute"; private static String UID_SEARCH_NAME_PROP = "users.ldap.uid.search.name"; private static String DN_IDENTIFIER_ATTRIBUTE_PROP = "users.ldap.dn.identifier.attribute"; // Jahia to LDAP properties mapping constants private static String USERNAME_ATTRIBUTE_MAP_PROP = "users.ldap.username.attribute.map"; private static String FIRSTNAME_ATTRIBUTE_MAP_PROP = "users.ldap.firstname.attribute.map"; private static String LASTNAME_ATTRIBUTE_MAP_PROP = "users.ldap.lastname.attribute.map"; private static String EMAIL_ATTRIBUTE_MAP_PROP = "users.ldap.email.attribute.map"; private static String ORGANIZATION_ATTRIBUTE_MAP_PROP = "users.ldap.organization.attribute.map"; private static String SEARCH_COUNT_LIMIT_PROP = "users.ldap.search.countlimit"; private static String SEARCH_WILDCARD_ATTRIBUTE_LIST = "users.ldap.search.wildcards.attributes"; private Properties ldapProperties = null; private DirContext publicCtx = null; private boolean connectedToPublic = false; private Vector searchWildCardAttributeList = null; private final String MSG_INTERNAL_ERROR = new String ("JahiaUserManagerLDAPProvider"); private static JahiaUserManagerLDAPProvider mUserManagerLDAPService; private Hashtable mUserCache; private JahiaGroupManagerDBService mGroupService = null; private JahiaACLManagerService mACLService = null; private JahiaIncrementorsDBService mIncrementorService = null; private JahiaDBPoolService mDBPoolService = null; /** Root user unique identification number */ public static final int ROOT_USER_ID = 0; /** Guest user unique identification number */ public static final int GUEST_USER_ID = 1; //-------------------------------------------------------------------------- /** * Create an new instance of the User Manager Service if the instance do not * exist, or return the existing instance. * * @return Return the instance of the User Manager Service. */ public static JahiaUserManagerLDAPProvider getInstance () { if (mUserManagerLDAPService == null) { try { mUserManagerLDAPService = new JahiaUserManagerLDAPProvider (); } catch (JahiaException ex) { JahiaConsole.println ("User Manager", "Could not create an instance of the JahiaUserManagerLDAPProvider class"); } } return mUserManagerLDAPService; } //-------------------------------------------------------------------------- /** * This is the method that creates a new user in the system, with all the * specified attributes. * * @param name User login name. * @param password User password * @param userKey User identifier on the Jahia installation * @param siteID Identifier of the site this user is defined in * @param properties User additional parameters. If the user has no additional * attributes, give a NULL pointer to this parameter. * @return a JahiaUser object containing an instance of the created user, * in this case a instance of JahiaLDAPUser. */ public synchronized JahiaUser createUser (String name, String password, String userKey, int siteID, Properties properties) { if (!isNameValid (name)) { return null; } // try to avoid a NullPointerException if (!isNameValid (password)) { return null; } // Check first if the user already exists in the database. if (userExists (siteID, name)) { return null; } // get the user and guest group JahiaGroup usersGroup = mGroupService.getUsersGroup (siteID); JahiaGroup guestGroup = mGroupService.getGuestGroup (siteID); if ((usersGroup == null) || (guestGroup == null)) { toConsole ("createUser() : could not get the [users] or/and [guest] group instance."); return null; } // Get the next available user ID int userID; try { userID = mIncrementorService.autoIncrement ("jahia_users"); toConsole ("got new user ID = ["+Integer.toString(userID)+"]"); } catch (JahiaException ex) { JahiaConsole.println ("UserManager", "Exception !!! Could not get a new user ID from the incrementor DB"); return null; } // Encrypt the password password = encryptPassword (password); if (password == null) { toConsole ("createUser() could not encrypt the user password."); return null; } // Create the user JahiaLDAPUser user = null; user = new JahiaLDAPUser (userID, name, password, userKey, siteID, properties); if (user == null) { toConsole ("createUser() couldn't create and instance of JahiaUser class"); return null; } // add the user into the cache if the user could be added into the database. if (addUserIntoLDAP (userID, name, password, userKey, siteID, properties)) { mUserCache.put (userKey, user); toConsole ("User ["+name+"] was added into the database and in the cache"); // by default each user is added to the users and guest group usersGroup.addMember (user); guestGroup.addMember (user); } else { toConsole ("Could not add the user ["+name+"] in the database!!"); user = null; } return user; } //-------------------------------------------------------------------------- /** * This method removes a user from the system. All the user's attributes are * remove, and also all the related objects belonging to the user. On success, * true is returned and the user parameter is not longer valid. Return false * on any failure. * * @param user reference on the user to be deleted. * @return Return true on success, or false on any failure. */ public synchronized boolean deleteUser (JahiaUser user) { return false; /** @todo not yet supported since the LDAP is read-only. */ } //-------------------------------------------------------------------------- /** * Load all the user data and attributes. On success a reference on the user * is returned, otherwise NULL is returned. * * @param userKey User's identification name. * @return a reference on a new created jahiaUser object. */ public JahiaUser lookupUser (String userKey) { String tmpUserKey = removeKeyPrefix (userKey); // first lookup in the cache. JahiaLDAPUser user = null; if (mUserCache.get(tmpUserKey) != null){ user = (JahiaLDAPUser)mUserCache.get(tmpUserKey); //JahiaConsole.println("JahiaUserManagerLDAPProvider.lookupUser", // " use with key=" + userKey + " is found in cache"); } if (user == null) { //JahiaConsole.println("JahiaUserManagerLDAPProvider.lookupUser", // " user with key=" + userKey + " is not found in cache"); user = lookupUserInLDAP (tmpUserKey); if (user != null) { mUserCache.put (tmpUserKey, user); } } return user; } //-------------------------------------------------------------------------- /** * Load all the user data and attributes. On success a reference on the user * is returned, otherwise NULL is returned. * * @param siteID the identifier of the site the user belongs to * @param name User's identification name. * @return Return a reference on a new created jahiaUser object. * @author NK */ public JahiaUser lookupUser (int siteID, String name) { // try to avoid a NullPointerException if (!isNameValid (name)) { return null; } String tmpUserName = removeKeyPrefix (name); // first lookup in the cache. Enumeration enum = mUserCache.elements(); JahiaUser user = null; while ( enum.hasMoreElements() ){ user = (JahiaUser)enum.nextElement(); if ( (user.getSiteID() == siteID) && ( user.getUsername().equals(tmpUserName) ) ){ return user; } } user = (JahiaUser)lookupUserInLDAP (siteID, tmpUserName); if ( user != null ){ JahiaConsole.println("JahiaUserManagerLDAPService.lookupUser"," user not null"); mUserCache.put(user.getName(),user); } return user; } //-------------------------------------------------------------------------- /** * This function checks into the system if the name has already been * assigned to another user. * * @param siteID the identifier of the site the user logged in to. * @param name User login name. * @return Return true if the specified name has not been assigned yet, * return false on any failure. */ public boolean userExists (int siteID, String name) { // try to avoid a NullPointerException if (name == null) { return false; } // name should not be empty. if (name.length() == 0) { return false; } return (lookupUser (siteID, name) != null); } //-------------------------------------------------------------------------- /** * This method returns the list of all the user names registed into the system. * * @param siteID the identifier of the site to get the user list for * @return Return a vector of strings holding the user identification names. */ public Vector getUsernameList (int siteID) { return getUserList(siteID); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -