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

📄 jahiausermanagerldapprovider.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    private void toConsole (String message)    {        JahiaConsole.println ("UserManager", message);    }    //--------------------------------------------------------------------------    private boolean addUserIntoLDAP (int id,                                     String username,                                     String password,                                     String userKey,                                     int siteID,                                     Properties properties)    {        return true;    }    private String removeKeyPrefix(String userKey) {        if (userKey.startsWith("{ldap}")) {            return userKey.substring(6);        } else {            return userKey;        }    }    //--------------------------------------------------------------------------    private JahiaLDAPUser lookupUserInLDAP (int siteID, String name)    {        JahiaLDAPUser user = lookupUserInLDAP(name);        if (user == null) {            return null;        }        user.setSiteID(siteID);        return user;    }    //--------------------------------------------------------------------------    private JahiaLDAPUser lookupUserInLDAP(String userKey)    {        JahiaLDAPUser user = null;        try {            SearchResult sr = getPublicUser(getPublicContext(), userKey);            if (sr == null) {                return null;            }            user = ldapToJahiaUser(sr, userKey);        } catch (SizeLimitExceededException slee) {            JahiaConsole.println("JahiaUserManagerLDAPProvider.lookupUserInLDAP",                                 "Search generated more than configured maximum search limit in " +                                 DEFAULT_CONFIGURATION_FILE +                                 ", limiting to " +                                 this.ldapProperties.getProperty(SEARCH_COUNT_LIMIT_PROP) +                                 " first results...");            user = null;        } catch (NamingException ne) {            JahiaConsole.printe("JahiaUserManagerLDAPProvider.lookupUserInLDAP", ne);            invalidatePublicCtx();            user = null;        }        return user;    }    /**     * Translates LDAP attributes to a JahiaUser properties set. Multi-valued     * attribute values are converted to Strings containing LINEFEED (\n)     * characters. This way it is quite simple to use String Tokenizers to     * extract multiple values. Note that if a value ALREADY contains a line     * feed characters this will cause unexpected behavior.     *     * @param sr result of a search on a LDAP directory context     * @param userKey may be null. If null the userKey will be extracted from     * the LDAP repository     *     * @return JahiaLDAPUser a user initialized with the properties loaded     * from the LDAP database, or null if no userKey could be determined for     * the user.     */    private JahiaLDAPUser ldapToJahiaUser(SearchResult sr, String userKey) {        JahiaLDAPUser user;        Properties userProps = new Properties();        String usingUserKey = userKey;        Attributes attrs = sr.getAttributes();        String name = sr.getName();        Enumeration attrsEnum = attrs.getAll();        while (attrsEnum.hasMoreElements()) {            Attribute curAttr = (Attribute) attrsEnum.nextElement();            String attrName = curAttr.getID();            StringBuffer attrValueBuf = new StringBuffer();            try {                Enumeration curAttrValueEnum= curAttr.getAll();                while (curAttrValueEnum.hasMoreElements()) {                    Object curAttrValueObj = curAttrValueEnum.nextElement();                    if ( (curAttrValueObj instanceof String) ) {                        attrValueBuf.append((String) curAttrValueObj);                    } else {                        JahiaConsole.println("JahiaUserManagerLDAPProvider.ldapToJahiaUser",                                             "Converting attribute <" + attrName + "> from class " +                                             curAttrValueObj.getClass().toString() + " to String...");                        /** @todo FIXME : for the moment we convert everything to String */                        attrValueBuf.append(curAttrValueObj);                    }                    attrValueBuf.append('\n');                }            } catch (NamingException ne) {                JahiaConsole.printe("JahiaUserManagerLDAPProvider.ldapToJahiaUser", ne);                attrValueBuf = new StringBuffer();            }            String attrValue = attrValueBuf.toString();            if (attrValue.endsWith("\n")) {                attrValue = attrValue.substring(0, attrValue.length()-1);            }            if ( (attrName != null) && (attrValue != null) ) {                if (usingUserKey == null) {                    if (attrName.equals(ldapProperties.getProperty(UID_SEARCH_ATTRIBUTE_PROP))) {                        usingUserKey = attrValue;                    }                }                userProps.setProperty(attrName, attrValue);            }        }        if (usingUserKey != null) {            mapLDAPToJahiaProperties(userProps);            // FIXME : Quick hack for merging Jahia DB user properties with LDAP user            mapDBToJahiaProperties(userProps, usingUserKey);            user = new JahiaLDAPUser(0, usingUserKey, "", usingUserKey, 0, userProps);            return user;        } else {            JahiaConsole.println("JahiaUserManagerLDAPProvider.ldapToJahiaUser",                                 "Ignoring entry " + sr.getName() +                                 " because it has no valid " +                                 ldapProperties.getProperty(UID_SEARCH_ATTRIBUTE_PROP) +                                 " attribute to be mapped onto user key...");            return null;        }    }    /**     * Map LDAP properties to Jahia user properties, such as first name,     * last name, etc...     * This method modifies the userProps object passed on parameters to add     * the new properties.     * @param userProps User properties to check for mappings. Basically what     * we do is copy LDAP properties to standard Jahia properties. This is     * defined in the user ldap properties file. Warning this object is modified     * by this method !     * @todo FIXME : if properties exist in LDAP that have the same name as     * Jahia properties these will be erased. We should probably look into     * making the properties names more unique such as org.jahia.propertyname     */    private void mapLDAPToJahiaProperties(Properties userProps) {        // copy attribute to standard Jahia properties if they exist both in        // the mapping and in the repository        /** @todo FIXME : THIS CODE IS UGLY BEURK ! (and I wrote it :( loom... ) */        String curProperty = ldapProperties.getProperty(USERNAME_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty(curProperty) != null) {                userProps.setProperty("username", userProps.getProperty(curProperty));            }        }        curProperty = ldapProperties.getProperty(FIRSTNAME_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty(curProperty) != null) {                userProps.setProperty("firstname", userProps.getProperty(curProperty));            }        }        curProperty = ldapProperties.getProperty(LASTNAME_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty(curProperty) != null) {                userProps.setProperty("lastname", userProps.getProperty(curProperty));            }        }        curProperty = ldapProperties.getProperty(EMAIL_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty(curProperty) != null) {                userProps.setProperty("email", userProps.getProperty(curProperty));            }        }        curProperty = ldapProperties.getProperty(ORGANIZATION_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty(curProperty) != null) {                userProps.setProperty("organization", userProps.getProperty(curProperty));            }        }    }    /**     * Maps Jahia user to LDAP properties using the definition     * mapping in the user LDAP configuration properties file. This modifies     * the userProps     * @param userProps     */    private void mapJahiaPropertiesToLDAP(Properties userProps) {        /** @todo FIXME : THIS CODE IS UGLY BEURK ! (and I wrote it :( loom... ) */        String curProperty = ldapProperties.getProperty(USERNAME_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty("username") != null) {                userProps.setProperty(curProperty, userProps.getProperty("username"));            }            userProps.remove("username");        }        curProperty = ldapProperties.getProperty(FIRSTNAME_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty("firstname") != null) {                userProps.setProperty(curProperty, userProps.getProperty("firstname"));            }            userProps.remove("firstname");        }        curProperty = ldapProperties.getProperty(LASTNAME_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty("lastname") != null) {                userProps.setProperty(curProperty, userProps.getProperty("lastname"));            }            userProps.remove("lastname");        }        curProperty = ldapProperties.getProperty(EMAIL_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty("email") != null) {                userProps.setProperty(curProperty, userProps.getProperty("email"));            }            userProps.remove("email");        }        curProperty = ldapProperties.getProperty(ORGANIZATION_ATTRIBUTE_MAP_PROP);        if (curProperty != null) {            if (userProps.getProperty("organization") != null) {                userProps.setProperty(curProperty, userProps.getProperty("organization"));            }            userProps.remove("organization");        }    }    //-------------------------------------------------------------------------    private boolean isNameValid (String name)    {        if (name == null) {            return false;        }        if (name.length() == 0) {            return false;        }        String authorizedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789{}";        char[] chars = authorizedCharacters.toCharArray();        char[] nameBuffer = name.toCharArray();        boolean badCharFound = false;        int i = 0;        while ((i < nameBuffer.length) && (!badCharFound))        {            int j = 0;            boolean ok = false;            while ((j < chars.length) && (!ok))            {                if (chars[j] == nameBuffer[i]) {                    ok = true;                }                j++;            }            badCharFound = (!ok);            if (badCharFound) {                JahiaConsole.println("JahiaUserManagerLDAPProvider.isNameValid",                                     "Bad character found in group name [" +                                     name +                                     "] at position " + Integer.toString(i));            }            i++;        }        return (!badCharFound);    }    /**     * Returns the internal public context variable. The point of this is to     * keep this connection open as long as possible, in order to reuser the     * connection.     * @return DirContext the current public context.     */    public DirContext getPublicContext() {        publicCtx = checkPublicCtx();        return publicCtx;    }    /**     * Performs a login of the specified user.     * @param userKey the user identifier defined in this service properties     * @param userPassword the password of the user     * @return String a string that contains the common name of this user     * whithin the repository.     */    public boolean login(String userKey, String userPassword) {        String personName = null;        String userFinalKey = userKey;        if ("".equals(userPassword)) {            JahiaConsole.println("JahiaUserManagerLDAPProvider.login",                                 "Empty passwords are not authorized for LDAP login ! Failing user " +                                 userKey + " login request.");            return false;        }        if (userFinalKey.startsWith("{ldap}")) {            userFinalKey = userKey.substring(6);        }        if (checkPublicCtx() == null) {            return false;        }        try {            personName = findNamebyUID(publicCtx, userFinalKey);            publicCtx = disconnectDir(publicCtx);            this.connectedToPublic = false;            DirContext privateCtx = null;            privateCtx = connectToPrivateDir(personName, userPassword);            if (privateCtx != null) {            } else {                personName = null;            }            privateCtx = disconnectDir(privateCtx);            // reconnect to public context            publicCtx = connectToPublicDir();        } catch (javax.naming.CommunicationException ce) {            JahiaConsole.printe("JahiaUserManagerLDAPProvider.login", ce);            JahiaConsole.println("JahiaUserManagerLDAPProvider.login", "Invalidading connection to public LDAP context...");            invalidatePublicCtx();            personName = null;        } catch (NamingException ne) {            personName = null;        }        return (personName!=null);    }    private DirContext checkPublicCtx () {        if ((publicCtx == null) || (connectedToPublic == false)) {            // this shouldn't happen... but timeouts have to be checked.            try {                publicCtx = connectToPublicDir();            } catch (NamingException ne) {                JahiaConsole.printe("JahiaUserManagerLDAPProvider.checkPublicCtx", ne);                publicCtx = null;

⌨️ 快捷键说明

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