Announcement Announcement Module
Collapse
No announcement yet.
Adding user to group Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding user to group

    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:
    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;
    		}
    	}
    I have no more idea I hope someone can help me!
    Thanks,
    Florian

  • #2
    This code is working:
    Code:
    public boolean addUserToGroup(String userDinstinguishedName, String groupDistinguishedName) {
    		try {
    			log.debug("Group name: "+groupDistinguishedName);
    			
    			String distinguishedGroupName = groupDistinguishedName;
    			DistinguishedName distinguishedName = new DistinguishedName(userDinstinguishedName);
    			
    			ModificationItem[] modItems = new ModificationItem[] {
    					new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", distinguishedName.encode()))
    			};
    			
    			String[] split = distinguishedGroupName.split(",DC");
    			ldapTemplate.modifyAttributes(split[0], modItems);
    			log.debug("Added user "+userDinstinguishedName+" to group "+split[0]);
    			
    			return true;
    		}
    		catch(org.springframework.ldap.NameAlreadyBoundException e) {
    			log.debug("User "+userDinstinguishedName+" already member of group "+groupDistinguishedName);
    			return true;
    		}
    		catch(Exception e) {
    			log.debug("Error on adding user to group", e);
    			return false;
    		}
    	}

    Comment

    Working...
    X