Announcement Announcement Module
Collapse
No announcement yet.
Find groups of un user by ldapTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Find groups of un user by ldapTemplate

    Hello,
    First excuse me for my bad english, i hope that you understand me.
    i write the code bellow to test if an user belong to a group in my ldap. i use apache directory studio, and the LdapTemplate framework.

    Code:
    public String[] findLdapGroupUsers(String groupID) {
    		DistinguishedName dn = new DistinguishedName();
    		dn.add("ou","groups");
    		dn.add("cn",groupID);
    		return (String[]) ldapTemplate.lookup(dn, 
    			new ContextMapper() {
    				public Object mapFromContext(Object ctx) {
                                            DirContextAdapter adapter  = (DirContextAdapter) ctx;
    					return adapter.getStringAttributes("uniqueMember");
    				}
    			});
    	}
     
        private boolean checkInGroupAdmin(String uid){
     
                String[] uniqueMembers = findLdapGroupUsers("Admins");
                for(String um : uniqueMembers){
                	String[] attrs = um.split(",");
                	if(attrs!=null && attrs.length>0){
                		for(String attr : attrs){
                			String[] attributes = attr.split("=");
                			if("uid".equals(attributes[0]) && uid.equals(attributes[1])){
                				return true;
                			}else{
                				continue;
                			}
                		}
                	}
                }
    it work perfectly, but it is not optimized.instead of searching all users of a group and iterating the result, i want search the groups of an user.i searched the solution, but still not found it !!
    Any suggestions please.

  • #2
    It could help you too look into this query I perform when I want to know whether a user has the rights of a group
    Code:
    (&
     (memberOf:1.2.840.113556.1.4.1941:=CN=group_id,OU=groups,DC=my,DC=lan)
     (objectClass=user)
     (uid=username)
    )
    The part ":1.2.840.113556.1.4.1941:" searchs also for users who are indirectly within this group.
    In Java you yould do something like that:
    Code:
    String groupDN = ...;
    String username = ...;
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("memberOf:1.2.840.113556.1.4.1941:", groupDN));
    filter.and(new EqualsFilter("objectClass", "user"));
    filter.and(new EqualsFilter("uid", username));
    ldapTemplate.searchForObject(DistinguishedName.EMPTY_PATH, filter.encode(), new MyMapper());

    Comment

    Working...
    X