Search code examples
node.jsopenldapldapjs

Error: modification must be an Attribute on groupOfNames member modify ldapjs


I have a code that should add an user to a groupOfNames in ldapjs

  function addUserToGroup(userDN, groupName, callback) {
    const groupDN = `cn=${groupName},ou=groups,dc=XXX`;
    console.log(userDN);
    console.log(groupDN);
    const change = new ldap.Change({
      operation: 'add',
      modification: {
        member: [userDN],
      },
    });
  
    client.modify(groupDN, change, (err) => {
      if (err) {
        console.error('Error adding user to group:', err);
      }
      callback(err, groupDN);
    });
  }

  addUserToGroup('uid=3d4dfc3d-7493-4a1d-9659-f1799a48a927,ou=people,dc=XXX', 'everybody', (err) => {
    if (err) {
      console.error('Error assigning user to group:', err);
    }
  });

but it throws this error

uid=3d4dfc3d-7493-4a1d-9659-f1799a48a927,ou=people,dc=XXX
cn=everybody,ou=groups,dc=XXX
C:\XXX\projects\ldaptest\node_modules\@ldapjs\change\index.js:55
      throw Error('modification must be an Attribute')
      ^

Error: modification must be an Attribute
    at Change.set modification [as modification] (C:\XXX\projects\ldaptest\node_modules\@ldapjs\change\index.js:55:13)
    at new Change (C:\XXX\projects\ldaptest\node_modules\@ldapjs\change\index.js:29:23)
    at addUserToGroup (C:\XXX\projects\ldaptest\index.js:169:20)
    at C:\XXX\projects\ldaptest\index.js:44:3
    at callbackWrapper (C:\XXX\projects\ldaptest\node_modules\ldapjs\lib\client\client.js:296:5)
    at sendResult (C:\XXX\projects\ldaptest\node_modules\ldapjs\lib\client\client.js:1244:12)
    at messageCallback (C:\XXX\projects\ldaptest\node_modules\ldapjs\lib\client\client.js:1269:16)
    at Parser.onMessage (C:\XXX\projects\ldaptest\node_modules\ldapjs\lib\client\client.js:925:14)
    at Parser.emit (node:events:527:28)
    at Parser.write (C:\XXX\projects\ldaptest\node_modules\ldapjs\lib\messages\parser.js:135:8)
[nodemon] app crashed - waiting for file changes before starting...

I change the code with some example online but could not find the working one. Any help?


Solution

  • I got the same error, however, I got the solution,

    const userPasswordAttribute = new ldap.Attribute({
      type: 'member',
      values: userDN
    });
    
    const change = new ldap.Change({
      operation: 'add',
      modification: userPasswordAttribute
    });
    

    You may refer to the page for detail.

    I am using ldapjs version 3.0.5