Appearance
informat.ldap LDAP操作
概述
使用informat.ldap
对象可链接到 LDAP(Lightweight Directory Access Protocol) 目录服务器,并进行搜索操作。
connect
创建LDAP连接 通过指定的信息连接LDAP服务器,如果连接失败或者用户名密码验证失败会抛出异常,使用connect
方法可以检查用户的用户名密码是否合法。 连接成功后,可以使用返回的LdapConnection 进行查询,修改,新增等操作。注意在操作完毕后需要调用 LdapConnection 的 close
方法关闭连接。
javascript
informat.ldap.connect(info)
参数 | 类型 | 描述 |
---|---|---|
info | LdapConnectionInfo | 连接ldap服务器的信息 |
返回值
返回LdapConnection对象
示例
js
let connection = null
try {
connection = informat.ldap.connect({
providerURL: 'LDAP://1.13.173.190:389',
securityPrincipal: 'user',
securityCredentials: 'pwd'
})
} catch (e) {
//连接ldap服务器或者认证失败
console.error('连接失败:', e)
} finally {
if (connection != null) {
connection.close();
}
}
LdapConnection
close
关闭LDAP连接
javascript
connection.close()
search
搜索满足条件的条目 返回根节点为basedn,并且满足filter指定的条件的的条目。
javascript
connection.search(basedn, filter, control)
参数 | 类型 | 描述 |
---|---|---|
basedn | String | 根节点的完整路径,例如 cn=users,dc=informat,dc=cn |
filter | String | 查询条件 |
control | LdapSearchControl | 搜索控制 |
返回值
类型为 Array<LdapSearchResult>
示例
js
// 查询cn=Users,dc=informat,dc=cn目录下10个条目,只返回id、name、age属性。
const result = connect.search('cn=Users,dc=informat,dc=cn', 'name=*', {
searchScope: 'SUBTREE', // 搜索的限定范围,默认为 SUBTREE。SUBTREE 返回所有满足条件的条目,ONELEVEL 返回同级的条目,OBJECT 只返回满足条件的对象
countLimit: 10, // 选填。返回的条目的最大数量,
returningAttributes: ['id', 'name', 'age'] // 选填。返回的属性列表
})
// 输出所有条目的名称及其属性
result.forEach(r => {
console.log(r.name);
r.attributes.forEach(ra => {
console.log(ra.id + " = " + ra.values[0])
})
})
list
枚举命名上下文中绑定的名称,以及绑定到它们的对象的类名
javascript
connection.list(name)
参数 | 类型 | 描述 |
---|---|---|
name | String | 名称 |
返回值
类型为 Array<LdapListResult> 返回绑定对象列表
getAttributes
查询指定节点的属性
javascript
connection.getAttributes(dn)
参数 | 类型 | 描述 |
---|---|---|
dn | String | 要修改属性的节点的路径 |
返回值
类型为 Array<LdapSearchResultAttribute> 返回节点的属性列表
addAttribute
新增属性到指定节点
javascript
connection.addAttribute(dn, attributes)
参数 | 类型 | 描述 |
---|---|---|
dn | String | 要修改属性的节点的路径 |
attributes | Array<LdapModifyAttribute> | 属性列表 |
updateAttribute
更新指定节点的属性
javascript
connection.updateAttribute(dn, attributes)
参数 | 类型 | 描述 |
---|---|---|
dn | String | 要修改属性的节点的路径 |
attributes | Array<LdapModifyAttribute> | 属性列表 |
deleteAttribute
删除指定节点的属性
javascript
connection.deleteAttribute(dn, attributes)
参数 | 类型 | 描述 |
---|---|---|
dn | String | 要修改属性的节点的路径 |
attributes | Array<LdapModifyAttribute> | 属性列表 |
decodeSID
解码Windows AD域中账号的SID
javascript
connection.decodeSID(sidAttribute)
参数 | 类型 | 描述 |
---|---|---|
sidAttribute | Object | sid属性值 |
返回值
类型为 String
返回解码后的SID值