I am new with using Ldap API for Java and I want to add a user to a group but I'm failing all the time.
I am using this code for adding specific user to a specific group:
private void insertUserToGroup(List<DistinguishedName> memberOf, DistinguishedName newUserDN) {
try
{
// Loop all groups to put the user in.
for(DistinguishedName groupDn : memberOf) {
String encodedGroupDn = groupDn.encode(); // Example: "cn=GROUP_SKL, ou=roles"
String encodedUserDn = newUserDN.encode(); // Example: "cn=user_dfh, ou=external"
// Now. Add user to a group.
ModificationItem member[] = new ModificationItem[1];
member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", encodedUserDn));
ldapTemplate.modifyAttributes(encodedGroupDn, member);
}
} catch ( InvalidAttributeValueException exc ) {
throw exc;
} catch ( NameAlreadyBoundException exc ) {
throw exc;
} catch ( NameNotFoundException exc ) {
throw exc;
} catch (Exception exc) {
throw exc;
}
}
The input arguments is list of groups and the user and you can see in the example comments how the DistinguishedName will look like.
1. And the exception i get when i run ldapTemplate.modifyAttributes(encodedGroupDn, member); is:
org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - 00000525: NameErr: DSID-031A11CC, problem 2001 (NO_OBJECT), data 0, best match of: '' ];
nested exception is javax.naming.NameNotFoundException: [LDAP: error code 32 - 00000525: NameErr: DSID-031A11CC, problem 2001 (NO_OBJECT), data 0, best match of: '' ];
remaining name 'cn=GROUP_SKL, ou=roles'
2. I have tried to run modifyAttributes(...) with other attributes like "description", "company" and that works for both Groups and Users but not the attribute "member".
3. So the question is. What name do it expect? Is the DistinguishedName wrong? Or is this way to add a user to a group totally wrong? Or is it some details I am missing?