When I try to add a user to a group I always get Malformed 'member' attribute value; nested exception is javax.naming.directory.InvalidAttributeValueExcept ion. I have nearly tried a dozen different ways but I did not succeed. This is my method to add an user to a group in an AD environment:
I have no more idea I hope someone can help me!Code:public boolean addUserToGroup(ADUser user, ADGroup group) { try { user = getUser("username", user.getUsername()); log.debug("User distinguished name: "+user.getDistinguishedName()); log.debug("Group distinguished name: "+group.getDistinguishedName()); String distinguishedGroupName = user.getDistinguishedName(); DistinguishedName distinguishedName = new DistinguishedName(user.getDistinguishedName()); // Getting the current members of the group List<String> groupMembersListString = group.getMembers(); groupMembersListString.add(distinguishedName.encode()); //List<DistinguishedName> groupMembers = new ArrayList<DistinguishedName>(); /*for(int i=0; i<groupMembersListString.size(); i++){ groupMembers.add(new DistinguishedName(groupMembersListString.get(i)).encode()); log.debug("Member: "+groupMembersListString.get(i)); }*/ // now we add the distinguished name to the attributes String[] split = distinguishedGroupName.split(",DC"); ModificationItem[] modItems = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("member", groupMembersListString.toArray())) }; ldapTemplate.modifyAttributes(split[0], modItems); log.debug("Added user to group "+split[0]); //DirContextOperations context = ldapTemplate.lookupContext(split[0]); //log.debug("Group lookup successfully.."); //context.addAttributeValue("member", user.getUsername()); //context.addAttributeValue("memberUid", user.getUsername()); //log.debug("Added attribute to group..."); //ldapTemplate.modifyAttributes(context); return true; } catch(Exception e) { log.debug("Error on adding user to group", e); return false; } }
Thanks,
Florian


Reply With Quote