Author: tmortagne
Date: 2008-02-14 08:46:23 +0100 (Thu, 14 Feb 2008)
New Revision: 7722

Modified:
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPUtils.java
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/user/impl/LDAP/XWikiLDAPAuthServiceImpl.java
Log:
XWIKI-1079:
* Fix cache initialization
* Makes XWikiLDAPUtis usable for other LDAP servers than default one (to be 
used by LDAP plugin latter).

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPUtils.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPUtils.java
    2008-02-14 00:55:18 UTC (rev 7721)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPUtils.java
    2008-02-14 07:46:23 UTC (rev 7722)
@@ -24,6 +24,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -77,14 +78,19 @@
     private static final String LDAP_DEFAULT_UID = "cn";
 
     /**
+     * Contains caches for each LDAP host:port.
+     */
+    private static Map cachePool = new Hashtable();
+
+    /**
      * The LDAP connection.
      */
     private XWikiLDAPConnection connection;
 
     /**
-     * Contains caches for each ldap host:port.
+     * The LDAP attribute containing the identifier for a user.
      */
-    private Map cachePool = new HashMap();
+    private String uidAttributeName = LDAP_DEFAULT_UID;
 
     static {
         LDAP_GROUP_CLASS.add("group".toLowerCase());
@@ -109,7 +115,23 @@
     }
 
     /**
-     * Get the cache with the provided name for a particular ldap server.
+     * @param uidAttributeName the LDAP attribute containing the identifier 
for a user.
+     */
+    public void setUidAttributeName(String uidAttributeName)
+    {
+        this.uidAttributeName = uidAttributeName;
+    }
+
+    /**
+     * @return the LDAP attribute containing the identifier for a user.
+     */
+    public String getUidAttributeName()
+    {
+        return uidAttributeName;
+    }
+
+    /**
+     * Get the cache with the provided name for a particular LDAP server.
      * 
      * @param cacheName the name of the cache.
      * @param context the XWiki context.
@@ -118,18 +140,23 @@
      */
     public XWikiCache getCache(String cacheName, XWikiContext context) throws 
XWikiException
     {
-        XWikiCache cache = null;
+        XWikiCache cache;
 
         String cacheKey =
             connection.getConnection().getHost() + ":" + 
connection.getConnection().getPort();
 
-        Map cacheMap = (Map) cachePool.get(cacheKey);
+        Map cacheMap;
 
+        if (cachePool.containsKey(cacheKey)) {
+            cacheMap = (Map) cachePool.get(cacheKey);
+        } else {
+            cacheMap = new Hashtable();
+            cachePool.put(cacheKey, cacheMap);
+        }
+
         if (cacheMap.containsKey(cacheName)) {
             cache = (XWikiCache) cacheMap.get(cacheName);
-        }
-
-        if (cache == null) {
+        } else {
             cache = context.getWiki().getCacheService().newCache("ldap." + 
cacheName);
             cacheMap.put(cacheKey, cache);
         }
@@ -149,16 +176,15 @@
      * Execute LDAP query to get all group's members.
      * 
      * @param groupDN the group to retrieve the members of and scan for 
subgroups.
-     * @param uidAttributeName the attribute containing the identifier for a 
user.
      * @return the LDAP search result.
      */
-    private List searchGroupsMembers(String groupDN, String uidAttributeName)
+    private List searchGroupsMembers(String groupDN)
     {
         String[] attrs = new String[2 + LDAP_GROUP_MEMBER.size()];
 
         int i = 0;
         attrs[i++] = LDAP_OBJECTCLASS;
-        attrs[i++] = uidAttributeName;
+        attrs[i++] = getUidAttributeName();
         for (Iterator it = LDAP_GROUP_MEMBER.iterator(); it.hasNext();) {
             attrs[i++] = (String) it.next();
         }
@@ -211,11 +237,6 @@
     {
         boolean isGroup = false;
 
-        XWikiLDAPConfig config = XWikiLDAPConfig.getInstance();
-
-        String uidAttributeName =
-            config.getLDAPParam(XWikiLDAPConfig.PROP_LDAP_UID, 
LDAP_DEFAULT_UID, context);
-
         String id = null;
 
         for (Iterator seachAttributeIt = searchAttributeList.iterator(); 
seachAttributeIt
@@ -230,14 +251,14 @@
                 if (LDAP_GROUP_CLASS.contains(objectName.toLowerCase())) {
                     isGroup = true;
                 }
-            } else if (key.equalsIgnoreCase(uidAttributeName)) {
+            } else if (key.equalsIgnoreCase(getUidAttributeName())) {
                 id = searchAttribute.value;
             }
         }
 
         if (!isGroup) {
             if (id == null) {
-                LOG.error("Could not find attribute " + uidAttributeName + " 
for LDAP dn "
+                LOG.error("Could not find attribute " + getUidAttributeName() 
+ " for LDAP dn "
                     + groupDN);
             }
 
@@ -276,13 +297,8 @@
             return true;
         }
 
-        XWikiLDAPConfig config = XWikiLDAPConfig.getInstance();
+        List searchAttributeList = searchGroupsMembers(groupDN);
 
-        String uidAttributeName =
-            config.getLDAPParam(XWikiLDAPConfig.PROP_LDAP_UID, 
LDAP_DEFAULT_UID, context);
-
-        List searchAttributeList = searchGroupsMembers(groupDN, 
uidAttributeName);
-
         if (searchAttributeList != null) {
             isGroup =
                 getGroupMembers(groupDN, memberMap, subgroups, 
searchAttributeList, context);
@@ -325,7 +341,9 @@
                 LOG.debug("Retrieving Members of the group: " + groupDN);
             }
 
-            if (getGroupMembers(groupDN, groupMembers, new ArrayList(), 
context)) {
+            boolean isGroup = getGroupMembers(groupDN, groupMembers, new 
ArrayList(), context);
+
+            if (isGroup) {
                 synchronized (cache) {
                     cache.putInCache(groupDN, groupMembers);
                 }
@@ -348,16 +366,8 @@
     {
         String result = null;
 
-        XWikiLDAPConfig config = XWikiLDAPConfig.getInstance();
+        String ldapuser = getUidAttributeName() + "=" + userName.toLowerCase();
 
-        String uidAttributeName =
-            config.getLDAPParam(XWikiLDAPConfig.PROP_LDAP_UID, 
LDAP_DEFAULT_UID, context);
-        if (uidAttributeName == null || uidAttributeName.length() == 0) {
-            uidAttributeName = LDAP_DEFAULT_UID;
-        }
-
-        String ldapuser = uidAttributeName + "=" + userName.toLowerCase();
-
         for (Iterator it = groupMembers.keySet().iterator(); it.hasNext();) {
             String u = (String) it.next();
             // implementing it case-insensitive for now

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/user/impl/LDAP/XWikiLDAPAuthServiceImpl.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/user/impl/LDAP/XWikiLDAPAuthServiceImpl.java
       2008-02-14 00:55:18 UTC (rev 7721)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/user/impl/LDAP/XWikiLDAPAuthServiceImpl.java
       2008-02-14 07:46:23 UTC (rev 7722)
@@ -124,6 +124,9 @@
         XWikiLDAPConnection connector = new XWikiLDAPConnection();
         XWikiLDAPUtils ldapUtils = new XWikiLDAPUtils(connector);
 
+        
ldapUtils.setUidAttributeName(config.getLDAPParam(XWikiLDAPConfig.PROP_LDAP_UID,
+            LDAP_DEFAULT_UID, context));
+
         try {
             // 
////////////////////////////////////////////////////////////////////
             // 1. Check for superadmin
@@ -542,7 +545,7 @@
         try {
             if (LOG.isDebugEnabled()) {
                 LOG.debug(String.format("Adding user {0} to xwiki group {1}", 
new String[] {
-                userName, groupName}));
+                    userName, groupName}));
             }
 
             String fullWikiUserName = XWIKI_USER_SPACE + XWIKI_SPACE_NAME_SEP 
+ userName;
@@ -575,7 +578,7 @@
 
         } catch (Exception e) {
             LOG.error(String.format("Failed to add a user [{0}] to a group 
[{1}]", new String[] {
-            userName, groupName}), e);
+                userName, groupName}), e);
         }
     }
 

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to