📄 jahiadbuser.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// NK 12.04.2001 - Changed to support Multi Sitepackage org.jahia.services.usermanager;import java.security.Principal;import java.util.Enumeration;import java.util.Properties;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseConnectionException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.registries.ServicesRegistry;/** * A JahiaUser represents a physical person who is defined by a username and * a password for authentification purpose. Every other property of a JahiaUser * is stored in it's properties list, which hold key-value string pairs. * For example email, firstname, lastname, ... information should be stored in * this properties list. * * @author Fulco Houkes * @version 1.1 */public class JahiaDBUser implements JahiaUser{ /** User unique identification number in the database. */ private int mID; /** User unique identification name */ private String mUsername; /** User password */ private String mPassword; /** Each user has an unique String identifier **/ private String mUserKey; /** Site id , the owner of this user */ private int mSiteID = -1; /** User home page property **/ private static final String mHOMEPAGE_PROP = "user_homepage"; /** User additional parameters. */ private Properties mProperties = new Properties(); //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Create a new JahiaDBUser class instance. The passed in password must * already be encrypted, no ecryption will be done. If the passed in * properties is null, then the user will have no additional parameter than * it's id, name and password. * * @param id * User unique identification number. * @param name * User identification name. * @param password * User password. * @param int siteID * The site id * @param properties * User properties. */ protected JahiaDBUser (int id, String name, String password, String userKey, int siteID, Properties properties) { mID = id; mUsername = name; mPassword = password; mUserKey = userKey; mSiteID = siteID; if (properties != null) { mProperties = properties; } } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // public boolean equals (Object another) { if (another instanceof Principal) { if (another != null) { return (mUsername.equals (((Principal)another).getName())); } } return false; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Retrieve the user's unique database identification number. * * @return The user unique identification number. */ public int getID () { return mID; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // public String getName () { return getUserKey(); } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // public String getUsername () { return mUsername; } //-------------------------------------------------------------------------- // NK 12 Avr. 2001 // Initial implementation // public String getUserKey() { return mUserKey; } //-------------------------------------------------------------------------- // NK 12 Avr. 2001 // Initial implementation // public int getSiteID() { return mSiteID; } public void setSiteID(int siteID) { mSiteID = siteID; } //------------------------------------------------------------------------- /** * Returns the user's home page id. * -1 : undefined * * @return int The user homepage id. */ public int getHomepageID (){ if (mProperties != null){ try { String value = mProperties.getProperty(mHOMEPAGE_PROP); if ( value == null ) return -1; return Integer.parseInt(value); } catch (Throwable t ){ t.printStackTrace(); } } return -1; } //------------------------------------------------------------------------- /** * Set the home page id. * * @param int The user homepage id. * @return false on error */ public boolean setHomepageID (int id){ /* if ( !removeProperty(mHOMEPAGE_PROP) ) return false; */ return setProperty(mHOMEPAGE_PROP,String.valueOf(id)); } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // public Properties getProperties () { return mProperties; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // public String getProperty (String key) { if ((mProperties != null) && (key != null)) { return mProperties.getProperty (key); } return null; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Return a unique hashcode identifiying the user. * * @return * Return a valid hashcode integer of the user, On failure, -1 is * returned. */ public int hashCode () { return mID; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Remove the specified property from the properties list. * * @param key Property's name. * * @return Return true on success or false on any failure. */ public synchronized boolean removeProperty (String key) { boolean result = false; if ((key != null) && (key.length() > 0)) { JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance(); if (utils != null) { try { result = utils.removeProperty (key, mID); } // general database exeception catch (JahiaDatabaseException ex) { // false will be returned automaticaly } // database connection failure catch (JahiaDatabaseConnectionException ex) { // false will be returned automaticaly } // catch all the Jahia exceptions catch (JahiaException ex) { // false will be returned automaticaly } } } if (result) { mProperties.remove(key); } return result; } //--------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Change the user's password. * * @param newPassword * New user's password * * @return * Return true id the old password is the same as the current one and * the new password is valid. Return false on any failure. */ public synchronized boolean setPassword (String password) { boolean result = false; // try to avoid a NullPointerException if (password != null) { // password should not be empty. if (password.length() > 0) { // Encrypt the new password String tmp = JahiaUserManagerService. encryptPassword (password); JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance(); if (utils != null) { try { result = utils.setPassword (password, mID); if(result) { mPassword = tmp; } } // general database exeception catch (JahiaDatabaseException ex) { // false will be returned automaticaly } // database connection failure catch (JahiaDatabaseConnectionException ex) { // false will be returned automaticaly } // catch all the Jahia exceptions catch (JahiaException ex) { // false will be returned automaticaly } } } } return result; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Add (or update if not already in the property list) a property key-value * pair in the user's properties list. * * @param key Property's name. * @param value Property's value. * * @return Return true on success or false on any failure. */ public synchronized boolean setProperty (String key, String value) { boolean result = false; if ((key != null) && (value != null)) { JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance(); if (utils != null) { try { if (getProperty (key) == null) { result = utils.addProperty (key, value, mID); } else { result = utils.updateProperty (key, value, mID); } } // general database exeception catch (JahiaDatabaseException ex) { // false will be returned automaticaly } // database connection failure catch (JahiaDatabaseConnectionException ex) { // false will be returned automaticaly } // catch all the Jahia exceptions catch (JahiaException ex) { // false will be returned automaticaly } } if (result) { mProperties.setProperty (key, value); } } return result; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // public boolean verifyPassword (String password) { if (password != null) { String test = JahiaUserManagerService.encryptPassword (password); return mPassword.equals (test); } return false; } //-------------------------------------------------------------------------- // FH 29 Mar. 2001 // Initial implementation // /** * Return a string representation of the user and it's internal state. * * @return A string representation of this user. */ public String toString () { StringBuffer output = new StringBuffer ("Detail of user ["+mUsername+"]\n"); output.append (" - ID ["+Integer.toString (mID)+"]"); output.append (" - password ["+mPassword+"]\n"); output.append (" - properties :"); Enumeration names = mProperties.propertyNames(); String name; if (names.hasMoreElements()) { output.append ("\n"); while (names.hasMoreElements()) { name = (String)names.nextElement(); output.append (" "+name+" -> ["+(String)mProperties.getProperty(name)+"]\n"); } } else { output.append (" -no properties-\n"); } return output.toString(); } //-------------------------------------------------------------------------- // NK 06 Avr. 2001 /** * Test if the user is an admin member * * * @return * Return true if the user is an admin member * false on any error. */ public boolean isAdminMember (int siteID){ return isMemberOfGroup(siteID, JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME); } /** * Test if the user is the root user * * @return * Return true if the user is the root user * false on any error. */ public boolean isRoot () { /** @todo FIXME in this implementation the super user is necessarily * always in the jahia database implementation */ if ( getID() == JahiaUserManagerDBProvider.ROOT_USER_ID ) { return true; } else { return false; } } //------------------------------------------------------------------------- public boolean isMemberOfGroup (int siteID, String name) { // Get the services registry ServicesRegistry servicesRegistry = ServicesRegistry.getInstance (); if (servicesRegistry != null) { // get the group management service JahiaGroupManagerService groupService = servicesRegistry.getJahiaGroupManagerService (); // lookup the requested group JahiaGroup group = groupService.lookupGroup (siteID,name); if (group != null) { return group.isMember (this); } } return false; } /** * Get the name of the provider of this user. * * @return * String representation of the name of the provider of this user */ public String getProviderName() { return "jahia"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -