I am using Spring LDAP 2.04 and OpenLDAP. Using the model code at http://docs.spring.io/autorepo/docs/spring-ldap/current/reference/#dns-as-attribute-values I am trying to add an LDAP user to a group. This is my code:
public void addPersonToRole(String roleName, IUser user){
Name roleDn = buildGroupDn(roleName);
Name userDn = buildDn(user);
DirContextOperations ctx = ldapTemplate.lookupContext(roleDn);
ctx.addAttributeValue("uniqueMember",userDn);
try{
ldapTemplate.modifyAttributes(ctx);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
protected Name buildDn(IUser user){
return buildUserDnFromString("People",user.getUid());
}
protected Name buildUserDnFromString(String company, String userID){
return LdapNameBuilder.newInstance()
.add("ou", company)
.add("uid", userID)
.build();
}
protected Name buildGroupDn(String groupName){
return LdapNameBuilder.newInstance("ou=Roles")
.add("cn",groupName)
.build();
}
This works up to a point. The user will be added to the group as a uniqueMember, but without the fully qualified LDAP name, ie., instead of uid=user, ou=People, dc=company,dc=com only uid=user, ou=People is added. The buildDn() method is also called in my create() method and the full LDAP path gets included to successfully create the new user.
public void create(IUser user) {
DirContextAdapter context = new DirContextAdapter(buildDn(user));
mapToContext(user, context);
try{
ldapTemplate.bind(context);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
protected void mapToContext(IUser user, DirContextOperations context){
context.setAttributeValues("objectclass", new String[] { "top",
"person", "pilotPerson", "OpenLDAPperson" });
context.setAttributeValue("uid", user.getUid());
context.setAttributeValue("cn", user.getFullName());
context.setAttributeValue("sn", StringUtils.substringAfterLast(user.getFullName()," "));
if(StringUtils.isNotBlank(user.getDescription())) context.setAttributeValue("description", user.getDescription());
if(StringUtils.isNotBlank(user.getUserPassword())) context.setAttributeValue("userPassword", user.getUserPassword());
if(StringUtils.isNotBlank(user.getEmail())) context.setAttributeValue("mail",user.getEmail());
}
Is the mapToContect() call making a difference? If I try to explicitly add the company info to the User DN then I get a Malformed uniqueMember object error.
My java class implements BaseLdapNameAware and the following is included in the beans definition XML:
<bean class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor"/>
Two questions: 1. Are there any errors in my code preventing the group add? 2. Why is the company info (i.e., LDAP Base Path) dropped on the Group Add and not on the Create?