[jboss-svn-commits] JBoss Portal SVN: r5429 - trunk/identity/src/main/org/jboss/portal/identity2/ldap

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Oct 12 17:38:18 EDT 2006


Author: bdaw
Date: 2006-10-12 17:38:12 -0400 (Thu, 12 Oct 2006)
New Revision: 5429

Modified:
   trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleImpl.java
   trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleModuleImpl.java
   trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserImpl.java
   trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserModuleImpl.java
Log:
- initial simple role/user modules ldap implementation

Modified: trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleImpl.java
===================================================================
--- trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleImpl.java	2006-10-12 21:37:02 UTC (rev 5428)
+++ trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleImpl.java	2006-10-12 21:38:12 UTC (rev 5429)
@@ -21,7 +21,7 @@
 */
 package org.jboss.portal.identity2.ldap;
 
-import org.jboss.portal.identity2.Role;
+import org.jboss.portal.identity.Role;
 
 import java.util.Set;
 
@@ -36,6 +36,8 @@
 
    private String id;
 
+   private String displayName;
+
    private LDAPRoleImpl()
    {
 
@@ -46,26 +48,25 @@
       this.dn = dn;
    }
 
-
-
    public String getName()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+      return this.id;
    }
 
    public String getDisplayName()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+      return displayName;
    }
 
    public void setDisplayName(String name)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+      this.displayName = name;
    }
 
+   //TODO: fasade to MembershipModule.getUsers() method call
    public Set getUsers()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+      throw new UnsupportedOperationException("Not yet implemented");
    }
 
 

Modified: trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleModuleImpl.java
===================================================================
--- trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleModuleImpl.java	2006-10-12 21:37:02 UTC (rev 5428)
+++ trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPRoleModuleImpl.java	2006-10-12 21:38:12 UTC (rev 5429)
@@ -25,11 +25,20 @@
 import org.jboss.portal.identity.Role;
 import org.jboss.portal.identity.IdentityException;
 import org.jboss.portal.jems.as.system.AbstractJBossService;
+import org.jboss.portal.common.util.Tools;
 
 import javax.naming.directory.SearchResult;
 import javax.naming.directory.Attributes;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.BasicAttribute;
 import javax.naming.NamingException;
+import javax.naming.NamingEnumeration;
+import javax.naming.ldap.LdapContext;
 import java.util.Set;
+import java.util.List;
+import java.util.NoSuchElementException;
 
 /**
  * @author <a href="mailto:boleslaw.dawidowicz at jboss.org">Boleslaw Dawidowicz</a>
@@ -39,41 +48,178 @@
 {
    private LDAPConnectionContext connectionContext;
 
-   private String idAttributeName;
+   private String ridAttributeID;
 
+   private String containerDN;
+
+   private String displayNameAttributeID;
+
    public Role findRoleByName(String name) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      try
+      {
+         log.debug("findRoleByName(): name = " + name);
+
+         if (name == null)
+         {
+            throw new IdentityException("Role name canot be null");
+         }
+
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+         String filter = getRidAttributeID().concat("=").concat(name);
+         log.debug("Search filter: " + filter);
+
+
+         NamingEnumeration results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, controls);
+         List sr = Tools.toList(results);
+         if (sr.size() > 1)
+         {
+            throw new IdentityException("Found more than one role with id: " + name + "" +
+               "Posible data inconsistency");
+         }
+         return createRoleInstance((SearchResult)sr.iterator().next());
+
+      }
+      catch (NoSuchElementException e)
+      {
+         log.debug("No role found with name: " + name, e);
+      }
+      catch (NamingException e)
+      {
+         throw new IdentityException("Role search failed.", e);
+      }
+      return null;
    }
 
    public Set findRolesByNames(String[] names) throws IdentityException, IllegalArgumentException
    {
+
+      //TODO:
       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public Role findRoleById(Object id) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      if (id == null)
+      {
+         throw new IdentityException("Cannot search role with null id");
+      }
+      if (!(id instanceof String))
+      {
+         throw new IdentityException("Only String id is suppoted");
+      }
+      return findRoleById((String)id);
    }
 
    public Role findRoleById(String id) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      return findRoleByName(id);
    }
 
+
+
    public Role createRole(String name, String displayName) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+
+      //TODO: handle displayName (what attribute in schema?)
+      if (name == null)
+      {
+         throw new IdentityException("Role name cannot be null");
+      }
+
+      try
+      {
+         //
+         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext().lookup(getContainerDN());
+
+
+         //We store new entry using set of attributes. This should give more flexibility then
+         //extending user object from ContextDir - configure what objectClass place there
+         Attributes attrs = new BasicAttributes(true);
+
+         //TODO: let admin decide what objectClass put in here (maybe as a map in mbean params)
+         Attribute oc = new BasicAttribute("objectClass");
+         oc.add("top");
+         oc.add("groupOfNames");
+         attrs.put(oc);
+         //set the value for cn in case rid attribute is different
+         attrs.put("cn","");
+         attrs.put("member","");
+
+         //role name
+         attrs.put(getRidAttributeID(),name);
+
+         //display name
+         attrs.put(getDisplayNameAttributeID(), displayName);
+
+         String dn = getRidAttributeID().concat("=").concat(name);
+
+         log.debug("creating ldap entry for: " + dn + "; " + attrs );
+         ctx.createSubcontext(dn, attrs);
+      }
+      catch(NamingException e)
+      {
+         throw new IdentityException("Failed to create user", e);
+      }
+
+      return findRoleByName(name);
    }
 
    public void removeRole(Object id) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      LDAPRoleImpl ldapr = (LDAPRoleImpl)findRoleById(id);
+
+      if (ldapr == null)
+      {
+         throw new IdentityException("Cannot find role for removal");
+      }
+
+      if (ldapr.getDn() == null)
+      {
+         throw new IdentityException("Cannot obtain DN of role");
+      }
+
+      try
+      {
+         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext();//.lookup(getContainerDN());
+         log.debug("removing entry: " + ldapr.getDn());
+         ctx.unbind(ldapr.getDn());
+      }
+      catch(Exception e)
+      {
+         throw new IdentityException("Failed to remove role: ", e);
+      }
    }
 
    public int getRolesCount() throws IdentityException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      try
+      {
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+         //search all entries containing "cn" attribute
+         String filter = getRidAttributeID().concat("=").concat("*");
+         log.debug("Search filter: " + filter);
+
+
+         NamingEnumeration results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, controls);
+         List sr = Tools.toList(results);
+
+         return sr.size();
+
+      }
+      catch (NoSuchElementException e)
+      {
+         log.debug("No roles found", e);
+      }
+      catch (Exception e)
+      {
+         throw new IdentityException("Role search failed.", e);
+      }
+      return 0;
    }
 
    public Set findRoles() throws IdentityException
@@ -82,24 +228,45 @@
    }
 
 
-   protected LDAPRoleImpl createUserInstance(SearchResult sr) throws IdentityException
+   protected LDAPRoleImpl createRoleInstance(SearchResult sr) throws IdentityException
    {
       LDAPRoleImpl ldapr = null;
       try {
 
 
+         log.debug("SearchResult: " + sr );
+
          Attributes attrs = sr.getAttributes();
-         String dn = (String)attrs.get("dn").get();
-         ldapr = new LDAPRoleImpl(dn);
-         if (idAttributeName != null)
+         log.debug("Search result attributes: " + attrs);
+
+
+         //role name
+         Attribute uida = attrs.get(getRidAttributeID());
+         if (uida == null)
          {
-            String uid = (String)attrs.get(idAttributeName).get();
-            ldapr.setId(uid);
+            throw new IdentityException("LDAP entry doesn't contain proper attribute:" + getRidAttributeID());
          }
+         ldapr =  new LDAPRoleImpl(uida.getID().concat("=").concat((String)uida.get()) + "," + getContainerDN());
+         ldapr.setId((String)uida.get());
+
+         Attribute display = attrs.get(getDisplayNameAttributeID());
+         if (display != null)
+         {
+            ldapr.setDisplayName((String)display.get());
+         }
+
+         //TODO:handle password
+
+         log.debug("role uid: " + ldapr.getId());
+         log.debug("role dn: " + ldapr.getDn());
+
+
+
+
       }
       catch (NamingException e)
       {
-         throw new IdentityException("Couldn't create LDAPRoleImpl object from ldap entry", e);
+         throw new IdentityException("Couldn't create LDAPRoleImpl object from ldap entry (SearchResult)", e);
       }
 
       return ldapr;
@@ -110,14 +277,18 @@
    //*** Getter and Setters
    //**************************
 
-   public String getIdAttributeName()
+   public String getRidAttributeID()
    {
-      return idAttributeName;
+      if (this.ridAttributeID == null)
+      {
+         return "cn";
+      }
+      return ridAttributeID;
    }
 
-   public void setIdAttributeName(String idAttributeName)
+   public void setRidAttributeID(String ridAttributeID)
    {
-      this.idAttributeName = idAttributeName;
+      this.ridAttributeID = ridAttributeID;
    }
 
    public LDAPConnectionContext getConnectionContext()
@@ -129,4 +300,28 @@
    {
       this.connectionContext = connectionContext;
    }
+
+    public String getContainerDN()
+   {
+      return containerDN;
+   }
+
+   public void setContainerDN(String containerDN)
+   {
+      this.containerDN = containerDN;
+   }
+
+   public String getDisplayNameAttributeID()
+   {
+      if (this.displayNameAttributeID == null)
+      {
+         return getRidAttributeID();
+      }
+      return displayNameAttributeID;
+   }
+
+   public void setDisplayNameAttributeID(String displayNameAttributeID)
+   {
+      this.displayNameAttributeID = displayNameAttributeID;
+   }
 }

Modified: trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserImpl.java
===================================================================
--- trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserImpl.java	2006-10-12 21:37:02 UTC (rev 5428)
+++ trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserImpl.java	2006-10-12 21:38:12 UTC (rev 5429)
@@ -21,7 +21,7 @@
 */
 package org.jboss.portal.identity2.ldap;
 
-import org.jboss.portal.identity2.User;
+import org.jboss.portal.identity.User;
 import org.jboss.portal.identity2.UserProfileModule;
 import org.jboss.portal.identity.ProfileMap;
 import org.jboss.portal.identity.IdentityException;
@@ -36,12 +36,17 @@
 public class LDAPUserImpl implements User
 {
 
+   //TODO:validatePassowrd method to handle different enc.
+
    private UserProfileModule userProfileModule;
 
    private String dn;
 
+   //In ldap implementation 
    private String id;
 
+   //private String userName;
+
    private LDAPUserImpl()
    {
 
@@ -58,127 +63,124 @@
 
    public boolean getEnabled()
    {
-      return false;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setEnabled(boolean enable)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void updatePassword(String password)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public boolean validatePassword(String password)
    {
-      return false;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public ProfileMap getProfile()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
-   public String getUserName()
-   {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
-   }
 
+
    public String getGivenName()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setGivenName(String givenName)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public String getFamilyName()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setFamilyName(String familyName)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public String getRealEmail()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setRealEmail(String realEmail)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public String getFakeEmail()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setFakeEmail(String fakeEmail)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public Date getRegistrationDate()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public boolean getViewRealEmail()
    {
-      return false;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setViewRealEmail(boolean viewRealEmail)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public Locale getPreferredLocale()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setPreferredLocale(Locale locale)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public String getSignature()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setSignature(String signature)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public Date getLastVisitDate()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setLastVisitDate(Date date)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public String getTheme()
    {
-      return null;  //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
    public void setTheme(String themeId)
    {
-      //To change body of implemented methods use File | Settings | File Templates.
+       throw new UnsupportedOperationException("Not yet implemented");
    }
 
 
@@ -202,4 +204,26 @@
    {
       return id;
    }
+
+   public UserProfileModule getUserProfileModule()
+   {
+      return userProfileModule;
+   }
+
+   public void setUserProfileModule(UserProfileModule userProfileModule)
+   {
+      this.userProfileModule = userProfileModule;
+   }
+
+
+
+   public String getUserName()
+   {
+      return this.id;
+   }
+
+   public void setUserName(String userName)
+   {
+      this.id = userName;
+   }
 }

Modified: trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserModuleImpl.java
===================================================================
--- trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserModuleImpl.java	2006-10-12 21:37:02 UTC (rev 5428)
+++ trunk/identity/src/main/org/jboss/portal/identity2/ldap/LDAPUserModuleImpl.java	2006-10-12 21:38:12 UTC (rev 5429)
@@ -26,51 +26,188 @@
 import org.jboss.portal.identity.User;
 import org.jboss.portal.identity.IdentityException;
 import org.jboss.portal.identity.NoSuchUserException;
+import org.jboss.portal.common.util.Tools;
 
 import javax.naming.directory.SearchResult;
 import javax.naming.directory.Attributes;
+import javax.naming.directory.SearchControls;
 import javax.naming.directory.Attribute;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.BasicAttribute;
 import javax.naming.NamingException;
+import javax.naming.NamingEnumeration;
+import javax.naming.Binding;
+import javax.naming.ldap.LdapContext;
 import java.util.Set;
+import java.util.List;
+import java.util.NoSuchElementException;
 
 /**
+ * Simple implementation of UserModule for LDAP support. Search of users is limited to one place
+ ** containerField -  DN of entry containing users (like ou=People,dc=example,dc=com).
+ * It's where users will be added using createUser() method. Under this DN users
+ * will be searched using ONELEVEL_SCOPE
+ ** uidAttributeID - attribute that stores user id. Default value is "uid"
+ *
  * @author <a href="mailto:boleslaw.dawidowicz at jboss.org">Boleslaw Dawidowicz</a>
  * @version $Revision: 1.1 $
  */
 public class LDAPUserModuleImpl implements UserModule
 {
+   private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LDAPUserModuleImpl.class);
 
    private LDAPConnectionContext connectionContext;
 
    private UserProfileModule userProfileModule;
 
-   private String idAttributeName;
+   /**
+    * Attribute that stores user id
+    */
+   private String uidAttributeID;
 
+   /**
+    * DN of entry containing users (like ou=People,dc=example,dc=com).
+    * It's where users will be added using createUser() method. Under this DN users
+    * will be searched using ONELEVEL_SCOPE
+    */
+   private String containerDN;
 
 
    public User findUserByUserName(String userName) throws IdentityException, IllegalArgumentException, NoSuchUserException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      try
+      {
+         log.debug("findUserByUserName(): username = " + userName);
+
+         if (userName == null)
+         {
+            throw new IdentityException("User name canot be null");
+         }
+
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+         String filter = getUidAttributeID().concat("=").concat(userName);
+         log.debug("Search filter: " + filter);
+
+
+         NamingEnumeration results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, controls);
+         List sr = Tools.toList(results);
+         if (sr.size() > 1)
+         {
+            throw new IdentityException("Found more than one user with id: " + userName + "" +
+               "Posible data inconsistency");
+         }
+         return createUserInstance((SearchResult)sr.iterator().next());
+
+      }
+      catch (NoSuchElementException e)
+      {
+         log.debug("No user found with name: " + userName, e);  
+      }
+      catch (NamingException e)
+      {
+         throw new IdentityException("User search failed.", e);
+      }
+      return null;
+
    }
 
    public User findUserById(Object id) throws IdentityException, IllegalArgumentException, NoSuchUserException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      if (id == null)
+      {
+         throw new IdentityException("Cannot search user with null id");
+      }
+      if (!(id instanceof String))
+      {
+         throw new IdentityException("Only String id is suppoted");
+      }
+      return findUserById((String)id);
+
    }
 
    public User findUserById(String id) throws IdentityException, IllegalArgumentException, NoSuchUserException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      return findUserByUserName(id);
    }
 
+   //TODO:how to properly store password? encoding and let specify the method?
    public User createUser(String userName, String password, String realEmail) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      //TODO:handle email parameter
+      if (userName == null)
+      {
+         throw new IdentityException("User name cannot be null");
+      }
+
+
+
+      try
+      {
+         //
+         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext().lookup(getContainerDN());
+
+
+         //We store new entry using set of attributes. This should give more flexibility then
+         //extending user object from ContextDir - configure what objectClass place there
+         Attributes attrs = new BasicAttributes(true);
+
+         //TODO: let admin decide what objectClass put in here (maybe as a map in mbean params)
+         Attribute oc = new BasicAttribute("objectClass");
+         oc.add("top");
+         oc.add("uidObject");
+         oc.add("person");
+         attrs.put(oc);
+         attrs.put(getUidAttributeID(),userName);
+         attrs.put("sn","");
+         attrs.put("cn","");
+
+         //TODO: correctly store password... encode it and choose the method?
+         attrs.put("userPassword", password);
+         //
+
+         String dn = getUidAttributeID().concat("=").concat(userName);
+
+         log.debug("creating ldap entry for: " + dn + "; " + attrs );
+         ctx.createSubcontext(dn, attrs);
+      }
+      catch(NamingException e)
+      {
+         throw new IdentityException("Failed to create user", e);
+      }
+
+      return findUserByUserName(userName);
    }
 
    public void removeUser(Object id) throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+
+      LDAPUserImpl ldapu = (LDAPUserImpl)findUserById(id);
+
+      if (ldapu == null)
+      {
+         throw new IdentityException("Cannot find user for removal");
+      }
+
+      if (ldapu.getDn() == null)
+      {
+         throw new IdentityException("Cannot obtain DN of user");
+      }
+
+      try
+      {
+         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext();//.lookup(getContainerDN());
+         log.debug("removing entry: " + ldapu.getDn());
+         ctx.unbind(ldapu.getDn());
+      }
+      catch(Exception e)
+      {
+         throw new IdentityException("Failed to remove user: ", e);
+      }
+
+
    }
 
    public Set findUsers(int offset, int limit) throws IdentityException, IllegalArgumentException
@@ -85,7 +222,31 @@
 
    public int getUserCount() throws IdentityException, IllegalArgumentException
    {
-      throw new UnsupportedOperationException("Not yet implemented");
+      try
+      {
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+
+         //search all entries containing "uid" attribute
+         String filter = getUidAttributeID().concat("=").concat("*");
+         log.debug("Search filter: " + filter);
+
+
+         NamingEnumeration results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, controls);
+         List sr = Tools.toList(results);
+
+         return sr.size();
+
+      }
+      catch (NoSuchElementException e)
+      {
+         log.debug("No users found", e);
+      }
+      catch (Exception e)
+      {
+         throw new IdentityException("User search failed.", e);
+      }
+      return 0;
    }
 
 
@@ -95,25 +256,39 @@
       try {
 
 
+         log.debug("SearchResult: " + sr );
+
          Attributes attrs = sr.getAttributes();
-         String dn = (String)attrs.get("dn").get();
-         ldapu = new LDAPUserImpl(dn);
-         if (idAttributeName != null)
+         log.debug("Search result attributes: " + attrs);
+
+
+         Attribute uida = attrs.get(getUidAttributeID());
+         if (uida == null)
          {
-            String uid = (String)attrs.get(idAttributeName).get();
-            ldapu.setId(uid);
+            throw new IdentityException("LDAP entry doesn't contain proper attribute:" + getUidAttributeID());
          }
+         ldapu =  new LDAPUserImpl(uida.getID().concat("=").concat((String)uida.get()) + "," + getContainerDN());
+         ldapu.setId((String)uida.get());
+
+
+         //TODO:handle password
+
+         log.debug("user uid: " + ldapu.getId());
+         log.debug("user dn: " + ldapu.getDn());
+
+
+
+
       }
       catch (NamingException e)
       {
-         throw new IdentityException("Couldn't create LDAPUserImpl object from ldap entry", e);
+         throw new IdentityException("Couldn't create LDAPUserImpl object from ldap entry (SearchResult)", e);
       }
 
       return ldapu;
    }
 
 
-
    //**************************
    //*** Getter and Setters
    //**************************
@@ -128,14 +303,18 @@
       this.userProfileModule = userProfileModule;
    }
 
-   public String getIdAttributeName()
+   public String getUidAttributeID()
    {
-      return idAttributeName;
+      if (this.uidAttributeID == null)
+      {
+         return "uid";
+      }
+      return uidAttributeID;
    }
 
-   public void setIdAttributeName(String idAttributeName)
+   public void setUidAttributeID(String uidAttributeID)
    {
-      this.idAttributeName = idAttributeName;
+      this.uidAttributeID = uidAttributeID;
    }
 
    public LDAPConnectionContext getConnectionContext()
@@ -148,4 +327,27 @@
       this.connectionContext = connectionContext;
    }
 
+   public String getContainerDN()
+   {
+      return containerDN;
+   }
+
+   public void setContainerDN(String containerDN)
+   {
+      this.containerDN = containerDN;
+   }
+
+   /*public String getNameAttributeID()
+   {
+      if (this.nameAttributeID == null)
+      {
+         return "cn";
+      }
+      return nameAttributeID;
+   }
+
+   public void setNameAttributeID(String nameAttributeID)
+   {
+      this.nameAttributeID = nameAttributeID;
+   }*/
 }




More information about the jboss-svn-commits mailing list