⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jahialdapuser.java

📁 java 写的一个新闻发布系统
💻 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.lang.*;import java.security.Principal;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Properties;import java.util.Enumeration;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseConnectionException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.services.usermanager.JahiaUserManagerService;import org.jahia.services.usermanager.JahiaUserDBUtils;import org.jahia.services.usermanager.JahiaGroupManagerDBService;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.utils.JahiaConsole;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  Serge Huber * @version 1.0 */public class JahiaLDAPUser implements JahiaUser{    static final public String USERKEY_LDAP_PREFIX = "{ldap}";    /** 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 additional parameters. */    private Properties  mProperties = new Properties();    /** User home page property **/    private static final String mHOMEPAGE_PROP = "user_homepage";    /**     * 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 JahiaLDAPUser (int id, String name, String password,                           String userKey, int siteID,                           Properties properties)    {        mID         = id;        mUsername   = name;        mPassword   = password;        mUserKey	= USERKEY_LDAP_PREFIX + userKey;        mSiteID		= siteID;        if (properties != null) {            mProperties = properties;        }    }    public boolean equals (Object another) {        if (another instanceof Principal) {            if (another != null) {                if (mUsername == null) {                    JahiaConsole.println("JahiaLDAPUser",                                         "Username for user key [" + mUserKey + "] is null , please check your users.ldap.properties mapping file !");                    if ( ((Principal)another).getName() == null ) {                        return true;                    } else {                        return false;                    }                }                return (mUsername.equals (((Principal)another).getName()));            }        }        return false;    }    /**     * Retrieve the user's unique database identification number.     *     * @return  The user unique identification number.     */    public int getID () {        return mID;    }    public String getName () {        return getUserKey();    }    public String getUsername () {        return mUsername;    }    public String getUserKey() {        return mUserKey;    }    public int getSiteID() {        return mSiteID;    }    public void setSiteID(int siteID) {        mSiteID = siteID;    }    public Properties getProperties ()    {        return mProperties;    }    //    public String getProperty (String key) {        if ((mProperties != null) && (key != null)) {            return mProperties.getProperty (key);        }        return null;    }    /**     * 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;    }    /**     * Remove the specified property from the properties list.     *     * @param   key     Property's name.     *     * @return  Return true on success or false on any failure.     * @todo FIXME : not supported in this read-only implementation     */    public synchronized boolean removeProperty (String key)    {        boolean result = false;        if ((key != null) && (key.length() > 0)) {            // Remove these lines if LDAP problem --------------------            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                }            }            // End remove --------------------        }        return result;    }    /**     * 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.     *     * @todo FIXME : not supported in this read-only LDAP implementation     */    public synchronized boolean setPassword (String password)    {        boolean result = false;        return result;    }    /**     * 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.     *     * @todo FIXME : not supported in LDAP implementation, only sets internal     * values in memory.     *     * @todo FIXME : These following lines are a quick hack to permit to store     * user properties for readonly LDAP users. A better solution would be to     * create a service that is in charge of managing user properties.     */    public synchronized boolean setProperty (String key, String value)    {        boolean result = false;        if ((key != null) && (value != null)) {            // Remove these lines if LDAP problem --------------------            JahiaUserDBUtils utils = JahiaUserDBUtils.getInstance();            if (utils != null) {                try {                    if (getProperty (key) == null) {                        result = utils.addProperty (key, value, -1);                    } else {                        result = utils.updateProperty (key, value, -1);                    }                }                // 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                }            }            // End remove --------------------            if (result) {                mProperties.setProperty (key, value);            }        }        return result;    }    //-------------------------------------------------------------------------    /**     * Returns the user's home page id.     * -1 : undefined     *     * @return int The user homepage id.     */    public int getHomepageID (){        if (mProperties != null){            try {                // Get the home page from the Jahia DB.                // By default an external user is represented with a -1 user ID.                // Format example : {ldap}username:user_homepage                String value = mProperties.getProperty(mUserKey + ":" + 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;        // Set the home page into the Jahia DB.        // By default an external user is represented with a -1 user ID.        // Format example : {ldap}username:user_homepage        return setProperty(mUserKey + ":" + mHOMEPAGE_PROP,String.valueOf(id));    }    public boolean verifyPassword (String password) {        if (password != null) {            boolean loginResult = ServicesRegistry.getInstance().getJahiaUserManagerService().login(mUserKey, password);            if (loginResult) {                /** @todo here we must now update the properties of the user                 *  since he has access to more of his attributes once logged in                 */                return true;            }            /** @todo insert here LDAP connection check... */        }        return false;    }    /**     * 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();    }    /**     * 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         */        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 "LDAP";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -