Author: tmortagne
Date: 2008-02-15 11:24:10 +0100 (Fri, 15 Feb 2008)
New Revision: 7748

Added:
   xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/ldap/
   
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfigTest.java
Modified:
   xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfig.java
Log:
XWIKI-1079:
* Modify the groups mapping configuration format to be able to store it in 
XWikiPreferences (to be able to set differents LDAP for each wikis)
* Add new LDAP parameters to XWikiPreferences class updtate
* Add XWikiLDAPConfig unit tests

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java
===================================================================
--- xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java 
2008-02-15 02:48:51 UTC (rev 7747)
+++ xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java 
2008-02-15 10:24:10 UTC (rev 7748)
@@ -2529,6 +2529,27 @@
         needsUpdate |=
             bclass.addTextField("documentBundles", "Internationalization 
Document Bundles", 60);
 
+        // LDAP
+        needsUpdate |= bclass.addBooleanField("ldap", "Ldap", "yesno");
+        needsUpdate |= bclass.addTextField("ldap_server", "Ldap server 
adress", 60);
+        needsUpdate |= bclass.addNumberField("ldap_port", "Ldap server port", 
5, "int");
+        needsUpdate |= bclass.addTextField("ldap_bind_DN", "Ldap login 
matching", 60);
+        needsUpdate |= bclass.addTextField("ldap_bind_pass", "Ldap password 
matching", 60);
+        needsUpdate |= bclass.addTextField("ldap_user_group", "Ldap group 
filter", 60);
+        needsUpdate |= bclass.addTextField("ldap_base_DN", "Ldap base DN", 60);
+        needsUpdate |= bclass.addTextField("ldap_UID_attr", "Ldap base DN", 
60);
+        needsUpdate |= bclass.addTextField("ldap_fields_mapping", "Ldap user 
fiels mapping", 60);
+        needsUpdate |=
+            bclass.addBooleanField("ldap_update_user", "Update user from 
LDAP", "yesno");
+        needsUpdate |= bclass.addTextField("group_mapping", "Ldap groups 
mapping", 80);
+        needsUpdate |=
+            bclass.addNumberField("ldap_groupcache_expiration", "LDAP groups 
members cache", 10,
+                "long");
+        needsUpdate |=
+            bclass.addStaticListField("ldap_mode_group_sync", "LDAP groups 
sync mode",
+                "always|create");
+        needsUpdate |= bclass.addBooleanField("ldap_trylocal", "Try local 
login", "yesno");
+        
         if (((BooleanClass) 
bclass.get("showLeftPanels")).getDisplayType().equals("checkbox")) {
             ((BooleanClass) 
bclass.get("showLeftPanels")).setDisplayType("yesno");
             ((BooleanClass) 
bclass.get("showRightPanels")).setDisplayType("yesno");

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfig.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfig.java
   2008-02-15 02:48:51 UTC (rev 7747)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfig.java
   2008-02-15 10:24:10 UTC (rev 7748)
@@ -1,6 +1,5 @@
 package com.xpn.xwiki.plugin.ldap;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -20,7 +19,7 @@
     /**
      * LDAP properties names suffix in xwiki.cfg.
      */
-    public static final String CFG_LDAP_SUFFIX = "ldap_";
+    public static final String CFG_LDAP_SUFFIX = "xwiki.authentication.ldap.";
 
     /**
      * LDAP port property name in xwiki.cfg.
@@ -28,14 +27,9 @@
     public static final String CFG_LDAP_PORT = CFG_LDAP_SUFFIX + "port";
 
     /**
-     * LDAP group mapping properties names suffix in xwiki.cfg.
-     */
-    public static final String CFG_LDAP_GROUPMAPPING_SUFFIX = CFG_LDAP_PORT + 
"group_mapping_";
-
-    /**
      * LDAP properties names suffix in XWikiPreferences.
      */
-    public static final String PREF_LDAP_SUFFIX = "xwiki.authentication.ldap.";
+    public static final String PREF_LDAP_SUFFIX = "ldap_";
 
     /**
      * LDAP port property name in XWikiPreferences.
@@ -68,26 +62,11 @@
     private static XWikiLDAPConfig instance;
 
     /**
-     * The mapping between XWiki users and LDAP users.
-     */
-    private Map groupMappings;
-
-    /**
-     * The mapping between XWiki users attributes and LDAP users attributes.
-     */
-    private Map userMappings;
-
-    /**
-     * The name of users attributes.
-     */
-    private List userAttributeList = new ArrayList();
-
-    /**
      * Protected constructor. Use [EMAIL PROTECTED] #getInstance()}.
      */
     private XWikiLDAPConfig()
     {
-        
+
     }
 
     /**
@@ -124,8 +103,7 @@
 
         if (param == null || "".equals(param)) {
             try {
-                param =
-                    context.getWiki().Param(cfgName);
+                param = context.getWiki().Param(cfgName);
             } catch (Exception e) {
                 // ignore
             }
@@ -137,7 +115,7 @@
 
         return param;
     }
-    
+
     /**
      * First try to retrieve value from XWiki Preferences and then from 
xwiki.cfg Syntax ldap_*name*
      * (for XWiki Preferences) will be changed to ldap.*name* for xwiki.cfg.
@@ -174,9 +152,9 @@
         int port;
 
         try {
-            port = context.getWiki().getXWikiPreferenceAsInt(CFG_LDAP_PORT, 
context);
+            port = context.getWiki().getXWikiPreferenceAsInt(PREF_LDAP_PORT, 
context);
         } catch (Exception e) {
-            port = (int) context.getWiki().ParamAsLong(PREF_LDAP_PORT, 0);
+            port = (int) context.getWiki().ParamAsLong(CFG_LDAP_PORT, 0);
         }
 
         return port;
@@ -190,36 +168,32 @@
      */
     public Map getGroupMappings(XWikiContext context)
     {
-        if (this.groupMappings == null) {
-            this.groupMappings = new HashMap();
+        Map groupMappings = new HashMap();
 
-            int pos = 1;
-            String grouplistmapping =
-                getLDAPParam(CFG_LDAP_GROUPMAPPING_SUFFIX + pos, "", context);
-            while (grouplistmapping != null && grouplistmapping.length() > 0) {
-                int splitt = grouplistmapping.indexOf('=');
+        String param = getLDAPParam("ldap_group_mapping", "", context);
 
-                if (splitt < 1) {
-                    LOG.error("Error parsing ldap_group_mapping attribute in 
xwiki.cfg: "
-                        + grouplistmapping);
-                } else {
-                    String xwikigroup = grouplistmapping.substring(0, splitt);
-                    String ldapgroup = grouplistmapping.substring(splitt + 1);
+        String[] mappingTable = param.split("\\|");
 
-                    this.groupMappings.put(ldapgroup, xwikigroup);
+        for (int i = 0; i < mappingTable.length; ++i) {
+            String mapping = mappingTable[i].trim();
 
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Groupmapping found: " + xwikigroup + " " + 
ldapgroup);
-                    }
-                }
+            int splitIndex = mapping.indexOf('=');
 
-                ++pos;
-                grouplistmapping =
-                    getLDAPParam(CFG_LDAP_GROUPMAPPING_SUFFIX + pos, null, 
context);
+            if (splitIndex < 1) {
+                LOG.error("Error parsing ldap_group_mapping attribute: " + 
mapping);
+            } else {
+                String xwikigroup = mapping.substring(0, splitIndex);
+                String ldapgroup = mapping.substring(splitIndex + 1);
+
+                groupMappings.put(ldapgroup, xwikigroup);
+
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Groupmapping found: " + xwikigroup + " " + 
ldapgroup);
+                }
             }
         }
 
-        return this.groupMappings;
+        return groupMappings;
     }
 
     /**
@@ -231,40 +205,34 @@
      */
     public Map getUserMappings(List attrListToFill, XWikiContext context)
     {
-        if (this.userMappings == null) {
-            this.userMappings = new HashMap();
+        Map userMappings = new HashMap();
 
-            String ldapFieldMapping = getLDAPParam("ldap_fields_mapping", 
null, context);
+        String ldapFieldMapping = getLDAPParam("ldap_fields_mapping", null, 
context);
 
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Ready to create user from LDAP with fields " + 
ldapFieldMapping);
-            }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Ready to create user from LDAP with fields " + 
ldapFieldMapping);
+        }
 
-            if (ldapFieldMapping != null && ldapFieldMapping.length() > 0) {
-                String[] fields = ldapFieldMapping.split(USERMAPPING_SEP);
+        if (ldapFieldMapping != null && ldapFieldMapping.length() > 0) {
+            String[] fields = ldapFieldMapping.split(USERMAPPING_SEP);
 
-                for (int j = 0; j < fields.length; j++) {
-                    String[] field = 
fields[j].split(USERMAPPING_XWIKI_LDAP_LINK);
-                    if (2 == field.length) {
-                        String xwikiattr = field[0].replace(" ", "");
-                        String ldapattr = field[1].replace(" ", "");
+            for (int j = 0; j < fields.length; j++) {
+                String[] field = fields[j].split(USERMAPPING_XWIKI_LDAP_LINK);
+                if (2 == field.length) {
+                    String xwikiattr = field[0].replace(" ", "");
+                    String ldapattr = field[1].replace(" ", "");
 
-                        this.userMappings.put(ldapattr, xwikiattr);
+                    userMappings.put(ldapattr, xwikiattr);
 
-                        this.userAttributeList.add(ldapattr);
-                    } else {
-                        LOG.error("Error parsing ldap_fields_mapping attribute 
in xwiki.cfg: "
-                            + fields[j]);
-                    }
+                    attrListToFill.add(ldapattr);
+                } else {
+                    LOG.error("Error parsing ldap_fields_mapping attribute in 
xwiki.cfg: "
+                        + fields[j]);
                 }
             }
         }
 
-        if (attrListToFill != null) {
-            attrListToFill.addAll(this.userAttributeList);
-        }
-
-        return this.userMappings;
+        return userMappings;
     }
 
     /**

Added: 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfigTest.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfigTest.java
                               (rev 0)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfigTest.java
       2008-02-15 10:24:10 UTC (rev 7748)
@@ -0,0 +1,215 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package com.xpn.xwiki.plugin.ldap;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+import org.jmock.cglib.MockObjectTestCase;
+
+import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.XWikiConfig;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.web.XWikiEngineContext;
+
+/**
+ * Test [EMAIL PROTECTED] XWikiLDAPConfig};
+ * 
+ * @version $Id: $
+ */
+public class XWikiLDAPConfigTest extends MockObjectTestCase
+{
+    private XWikiContext prefContext;
+
+    private XWikiContext cfgContext;
+
+    private static final Map PREFERENCES = new HashMap();
+
+    private static final XWikiConfig CONFIG = new XWikiConfig();
+    
+    private static final Map RESULT_CFG_USERMAPPING = new Hashtable();
+    private static final Map RESULT_PREF_USERMAPPING = new Hashtable();
+    
+    private static final Map RESULT_CFG_GROUPMAPPING = new Hashtable();
+    private static final Map RESULT_PREF_GROUPMAPPING = new Hashtable();
+
+    private static void addProperty(String prefName, String cfgName, String 
prefValue,
+        String cfgValue)
+    {
+        PREFERENCES.put(prefName, prefValue);
+        CONFIG.setProperty(cfgName, cfgValue);
+    }
+
+    static {
+        CONFIG.setProperty("xwiki.authentication.ldap.authclass",
+            "com.xpn.xwiki.user.impl.LDAP.LDAPAuthServiceImpl");
+
+        addProperty("ldap", "xwiki.authentication.ldap", "0", "1");
+        addProperty("ldap_server", "xwiki.authentication.ldap.server", 
"localhost", "127.0.0.1");
+        addProperty("ldap_port", "xwiki.authentication.ldap.port", "10000", 
"11111");
+        addProperty("ldap_check_level", 
"xwiki.authentication.ldap.check_level", "0", "1");
+        addProperty("ldap_base_DN", "xwiki.authentication.ldap.base_DN", 
"o=sevenSeas",
+            "o=sevenSeas2");
+        addProperty("ldap_bind_DN", "xwiki.authentication.ldap.bind_DN",
+            "cn={0},ou=people,o=sevenSeas", "cn={0},ou=people,o=sevenSeas2");
+        addProperty("ldap_bind_pass", "xwiki.authentication.ldap.bind_pass", 
"{1}", "{1}2");
+        addProperty("ldap_UID_attr", "xwiki.authentication.ldap.UID_attr", 
"uid", "uid2");
+        addProperty("ldap_groupcache_expiration",
+            "xwiki.authentication.ldap.groupcache_expiration", "10000", 
"11111");
+        addProperty("ldap_user_group", "xwiki.authentication.ldap.user_group", 
"0", "1");
+        addProperty("ldap_validate_password", 
"xwiki.authentication.ldap.validate_password", "1",
+            "0");
+        addProperty("ldap_update_user", 
"xwiki.authentication.ldap.update_user", "0", "1");
+        addProperty("ldap_trylocal", "xwiki.authentication.ldap.trylocal", 
"0", "1");
+        addProperty("ldap_mode_group_sync", 
"xwiki.authentication.ldap.mode_group_sync",
+            "always", "create");
+
+        addProperty("ldap_fields_mapping", 
"xwiki.authentication.ldap.fields_mapping",
+            "name=uid,last_name=sn", "name=uid2,last_name=sn2");
+        
+        RESULT_PREF_USERMAPPING.put("uid", "name");
+        RESULT_PREF_USERMAPPING.put("sn", "last_name");
+        RESULT_CFG_USERMAPPING.put("uid2", "name");
+        RESULT_CFG_USERMAPPING.put("sn2", "last_name");
+        
+        addProperty("ldap_group_mapping", 
"xwiki.authentication.ldap.group_mapping",
+            "XWiki.XWikiAdminGroup=cn=HMS 
Toto,ou=crews,ou=groups,o=sevenSeas|XWiki.XWikiAdminGroup=cn=HMS 
Titi,ou=crews,ou=groups,o=sevenSeas",
+            "XWiki.XWikiAdminGroup=cn=HMS 
Toto,ou=crews,ou=groups,o=sevenSeas2|XWiki.XWikiAdminGroup=cn=HMS 
Titi,ou=crews,ou=groups,o=sevenSeas2");
+        
+        RESULT_PREF_GROUPMAPPING.put("cn=HMS 
Toto,ou=crews,ou=groups,o=sevenSeas", "XWiki.XWikiAdminGroup");
+        RESULT_PREF_GROUPMAPPING.put("cn=HMS 
Titi,ou=crews,ou=groups,o=sevenSeas", "XWiki.XWikiAdminGroup");
+        RESULT_CFG_GROUPMAPPING.put("cn=HMS 
Toto,ou=crews,ou=groups,o=sevenSeas2", "XWiki.XWikiAdminGroup");
+        RESULT_CFG_GROUPMAPPING.put("cn=HMS 
Titi,ou=crews,ou=groups,o=sevenSeas2", "XWiki.XWikiAdminGroup");
+    }
+
+    protected void setUp() throws XWikiException
+    {
+        this.prefContext = new XWikiContext();
+
+        new XWiki(new XWikiConfig(), this.prefContext)
+        {
+            public void initXWiki(XWikiConfig config, XWikiContext context,
+                XWikiEngineContext engine_context, boolean noupdate) throws 
XWikiException
+            {
+                context.setWiki(this);
+                setConfig(config);
+            }
+
+            public String getXWikiPreference(String prefname, String 
default_value,
+                XWikiContext context)
+            {
+                return PREFERENCES.get(prefname).toString();
+            }
+        };
+
+        this.cfgContext = new XWikiContext();
+
+        new XWiki(CONFIG, this.cfgContext)
+        {
+            public void initXWiki(XWikiConfig config, XWikiContext context,
+                XWikiEngineContext engine_context, boolean noupdate) throws 
XWikiException
+            {
+                context.setWiki(this);
+                setConfig(config);
+            }
+
+            public String getXWikiPreference(String prefname, String 
default_value,
+                XWikiContext context)
+            {
+                return default_value;
+            }
+        };
+    }
+
+    // 
///////////////////////////////////////////////////////////////////////////////////////:
+    // Tests
+
+    public void testGetLDAPParam1()
+    {
+        assertEquals("0", XWikiLDAPConfig.getInstance().getLDAPParam("ldap",
+            "xwiki.authentication.ldap", null, prefContext));
+        assertEquals("1", XWikiLDAPConfig.getInstance().getLDAPParam("ldap",
+            "xwiki.authentication.ldap", null, cfgContext));
+    }
+
+    public void testGetLDAPParam2()
+    {
+        assertEquals("localhost", 
XWikiLDAPConfig.getInstance().getLDAPParam("ldap_server", null,
+            prefContext));
+        assertEquals("127.0.0.1", 
XWikiLDAPConfig.getInstance().getLDAPParam("ldap_server", null,
+            cfgContext));
+    }
+
+    public void testIsLDAPEnabled()
+    {
+        assertEquals(false, 
XWikiLDAPConfig.getInstance().isLDAPEnabled(prefContext));
+        assertEquals(true, 
XWikiLDAPConfig.getInstance().isLDAPEnabled(cfgContext));
+    }
+
+    public void testGetLDAPPort()
+    {
+        assertEquals(10000, 
XWikiLDAPConfig.getInstance().getLDAPPort(prefContext));
+        assertEquals(11111, 
XWikiLDAPConfig.getInstance().getLDAPPort(cfgContext));
+    }
+
+    public void testGetGroupMappings()
+    {
+        Map prefMapping = 
XWikiLDAPConfig.getInstance().getGroupMappings(prefContext);
+        
+        assertEquals(RESULT_PREF_GROUPMAPPING, prefMapping);
+        
+        Map cfgMapping = 
XWikiLDAPConfig.getInstance().getGroupMappings(cfgContext);
+        
+        assertEquals(RESULT_CFG_GROUPMAPPING, cfgMapping);
+    }
+
+    public void testGetUserMappings()
+    {
+        List prefAttrList = new ArrayList();
+
+        Map prefMapping = 
XWikiLDAPConfig.getInstance().getUserMappings(prefAttrList, prefContext);
+
+        assertEquals("uid", (String) prefAttrList.get(0));
+        assertEquals("sn", (String) prefAttrList.get(1));
+        
+        assertEquals(RESULT_PREF_USERMAPPING, prefMapping);
+        
+        // ///
+
+        List cfgAttrList = new ArrayList();
+
+        Map cfgMapping = 
XWikiLDAPConfig.getInstance().getUserMappings(cfgAttrList, cfgContext);
+
+        assertEquals("uid2", (String) cfgAttrList.get(0));
+        assertEquals("sn2", (String) cfgAttrList.get(1));
+        
+        assertEquals(RESULT_CFG_USERMAPPING, cfgMapping);
+    }
+
+    public void testGetCacheExpiration()
+    {
+        assertEquals(10000, 
XWikiLDAPConfig.getInstance().getCacheExpiration(prefContext));
+        assertEquals(11111, 
XWikiLDAPConfig.getInstance().getCacheExpiration(cfgContext));
+    }
+}


Property changes on: 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/ldap/XWikiLDAPConfigTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
notifications mailing list
notifications@xwiki.org
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to