How to add user to an LDAP group with LDAP.js

2019-06-09 16:58发布

问题:

I'm using LDAPjs inside a Node.js application. My app is creating a new user as intended, but I've not been able to find an example of how to go about adding that new user to an existing group.

Given a client object that was created with arequire('ldapjs').createClient() call, how do I add a given user (via its DN) to an existing group?

回答1:

You would use a function like the following, supplying to it a bound client as well as the DN that you want added to the group. In this example the group's DN is hard coded for simplicity's sake:

function addToGroup(client, dn) {
  var groupDn = "CN=MyGroup,OU=groups,DC=mydomain,DC=com";
  var change = new ldap.Change({
    operation: 'add',
    modification: {
      member: [dn]
    }
  });

  client.modify(groupDn, change, function(err, res) {
    if (err) {
      console.error("Looks like group add FAILED: %j", err);
    } else {
      console.log("Looks like group add WORKED: %j", res);
    }
  });
}