📄 jahiausermanagerdbprovider.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.usermanager;import java.sql.*;import java.util.*;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.exceptions.database.JahiaDatabaseException;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;/** * * @author Fulco Houkes * @version 2.0 */public class JahiaUserManagerDBProvider extends JahiaUserManagerProvider implements UsersDBInterface{ private final String MSG_INTERNAL_ERROR = new String ("User Manager internal error"); private static final String USERNAME_PROPERTY_NAME = "username"; private static JahiaUserManagerDBProvider mUserManagerDBService; 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 JahiaUserManagerDBProvider getInstance () { if (mUserManagerDBService == null) { try { mUserManagerDBService = new JahiaUserManagerDBProvider (); } catch (JahiaException ex) { JahiaConsole.println ("User Manager", "Could not create an instance of the JahiaUserManagerDBService class"); } } return mUserManagerDBService; } public void init( JahiaPrivateSettings jSettings ) throws JahiaInitializationException { } //-------------------------------------------------------------------------- /** * This is the method that creates a new user in the system, with all the * specified attributes. * * @param name User identification name. * @param password User password * @param attributes User additional parameters. If the user has no additional * attributes, give a NULL pointer to this parameter. */ 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 JahiaDBUser user = null; user = new JahiaDBUser (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 (addUserIntoDB (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. * @retrun Return true on success, or false on any failure. */ public synchronized boolean deleteUser (JahiaUser user) { JahiaConsole.println("JahiaUserManagerDBProvider.deleteUSer","Started"); // try to avoid a NullPointerException if (user == null) { return false; } /* // cannot delete the root and guest user if ((((JahiaDBUser)user).getID() == ROOT_USER_ID) || (((JahiaDBUser)user).getID() == GUEST_USER_ID)) { return false; } */ // cannot delete the root user if ( ((JahiaDBUser)user).getID() == ROOT_USER_ID ) { return false; } // this part has to be synchronized in order to avoid a thread to read invalid data. boolean result = false; // remove de user from the database. if (deleteUserFromDB (user)) { JahiaConsole.println("JahiaUserManagerDBProvider.deleteUSer","User removed from db"); // remove the user from the cache mUserCache.remove(user.getName()); JahiaConsole.println("JahiaUserManagerDBProvider.deleteUSer","User removed from cache"); mGroupService.removeUserFromAllGroups (user); JahiaConsole.println("JahiaUserManagerDBProvider.deleteUSer","User removed from groups"); mACLService.removeUserFromAllACLs (user); JahiaConsole.println("JahiaUserManagerDBProvider.deleteUSer","User removed from acls"); // and now ... KILL THE VICTIM !!!!!!! user = null; result = true; } return result; } /** * Performs a login of the specified user. * @param userID the user identifier defined in this service properties * @param userPassword the password of the user * @return boolean true if the login succeeded, false otherwise */ public boolean login(String userID, String userPassword) { return true; // not necessary in this implementation. } //-------------------------------------------------------------------------- /** * Load all the user data and attributes. On success a reference on the user * is returned, otherwise NULL is returned. * * @param name User's identification name. * @return Return a reference on a new created jahiaUser object. */ public JahiaUser lookupUser (String userKey) { // first lookup in the cache. JahiaDBUser user = null; if (mUserCache.get(userKey) != null){ user = (JahiaDBUser)mUserCache.get(userKey); //JahiaConsole.println("JahiaUserManagerDBProvider.lookupUser", // " use with key=" + userKey + " is found in cache"); } if (user == null) { //JahiaConsole.println("JahiaUserManagerDBProvider.lookupUser", // " user with key=" + userKey + " is not found in cache"); user = lookupUserInDB (userKey); if (user != null) { mUserCache.put (userKey, user); } } return user; } //-------------------------------------------------------------------------- /** * Load all the user data and attributes. On success a reference on the user * is returned, otherwise NULL is returned. * * @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; } // 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(name) ) ){ return user; } } user = (JahiaUser)lookupUserInDB (siteID, name); if ( user != null ){ JahiaConsole.println("JahiaUserManagerDBProvider.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 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. * * @return Return a vector of strings holding the user identification names. */ public Vector getUsernameList (int siteID) { Vector result = new Vector();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -