javascriptldapjs

How can I pass in an attribute dynamically when modifying a user with ldapJS


I'm using ldapjs and I'm trying to configure my own API to modify/update an LDAP directory and it works OK except when I want to modify an account e.g.

var change = new ldap.Change({
  operation: 'replace',
  modification: {
    company: 'Company Name'
  }
});

client.modify('cn=foo, o=example', change, function(err) {
  assert.ifError(err);
});

I'm finding it impossible to pass in dynamically the actual attribute 'company' (or whatever it is I need to alter), I've tried:

        var attr = "company";
        var value = "Company Name";

        var change = new ldap.Change({
            operation: 'replace',
            modification: {attr:value}
        })

But I get-

Error modifying user: NoSuchAttribute

and I've also tried it as a string:

        var attr = "company";
        var value = "Company Name";

        var modification = "{ " + attr + ":" + value + "}";

        var change = new ldap.Change({
            operation: 'replace',
            modification
        })

But I get:

Error('Only one attribute per Change allowed');

I'm not that experienced, so not really sure if this is possible or not?


Solution

  • Yes this is possible, you just have to change your code to

      const someCompany = 'someComapny'
      var change = new ldap.Change({
              operation: 'replace',
              modification: {company:someCompany}
       })
    
    

    if u want the company attribute to be dynamic u should use the computed property syntax like this

      const someCompany = 'someComapny'
      const companyKey = 'key'
      var change = new ldap.Change({
              operation: 'replace',
              modification: {[companyKey]:someCompany}
       })