简体   繁体   English

使用 ldapJS 修改用户时如何动态传递属性

[英]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 eg我正在使用ldapjs ,我正在尝试配置我自己的 API 来修改/更新 LDAP 目录,它工作正常,除非我想修改一个帐户,例如

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修改用户时出错: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'); Error('每次更改只允许一个属性');

I'm not that experienced, so not really sure if this is possible or not?我不是那么有经验,所以不确定这是否可能?

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}
   })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM