hongfangze-ldap
v0.0.2
Published
comm.ldap
Readme
LDAP域操作类
介绍
AD域的一些操作
开始使用
npm install hongfangze-ldap
类及成员定义
/**
* Creates an instance of _LDAP.
* @param {("ldap" | "ldaps")} [protocol="ldap"] 域控制器协议
* @param {string} [host="127.0.0.1"] 域控制器IP地址
* @param {string} adminAccount 具有域控管理权的管理员账户
* @param {string} adminPassword 绑定DN管理员的密码
* @param {string} baseOU 域根OU,如:OU=Users,OU=example,请查看distinguishedName里的一部分
* @param {string} baseDN 域根DN,如:DC=example,DC=com
* @param {number} [port=389] 域控制器端口,ldap一般为389,ldaps一般为636
* @memberof _LDAP
*/
constructor(protocol: "ldap" | "ldaps", host: string, adminAccount: string, adminPassword: string, baseOU: string, baseDN: string, port?: number);
/**
* 连接到LDAP服务器
* @return {*}
* @memberof _LDAP
*/
connect(): Promise<void>;
/**
* 释放客户端
* @return {*}
* @memberof _LDAP
*/
destroy(): Promise<void>;
/**
* 登录
* @param {string} username
* @param {string} password
* @return {{
* result: boolean,
* message?: string,
* }} 是否成功及错误原因
* @memberof _LDAP
*/
login(username: string, password: string): Promise<{
result: boolean;
message?: string;
}>;
/**
* 查询AD的组织架构(OU)
* @param {("one" | "sub" | "base")} [scope="sub"] one=只查当前OU下一层,base=只查当前节点,sub=查询整个OU树
* @param {boolean} [tree=true] 是否以树结构展示,默认true
* @return {Idept[]} 组织架构对象
* @memberof _LDAP
*/
OU(scope?: "one" | "sub" | "base", tree?: boolean): Promise<Idept[]>;
/**
* 查询指定OU下的所有用户(排除AD内置系统账号)
* @param {("one" | "sub" | "base")} [scope="sub"] one=只查当前OU下一层,base=只查当前节点,sub=查询整个OU树
* @return {IUser[]} 用户列表
* @memberof _LDAP
*/
USER(scope?: "one" | "sub" | "base"): Promise<IUser[]>;
/**
* 获取部门及用户
* @param {("one" | "sub" | "base")} [scope="sub"] 是否递归部门
* @param {boolean} [tree=true] 是否以树结构返回部门,默认是
* @return {Idept[]} 部门及用户信息
* @memberof _LDAP
*/
List(scope?: "one" | "sub" | "base", tree?: boolean): Promise<Idept[]>;
/**
* 搜索AD
* @param {ISearchOptions} options 搜索条件
* @return {*}
* @memberof _LDAP
*/
search(options: ISearchOptions): Promise<any>;使用示例
import { LDAP } from "hongfangze-ldap";
// 初始化AD信息
const ldapInstance = new LDAP("ldap", "127.0.0.1", "admin", "password", "OU=Users,OU=example", "DC=example,DC=com");
// 连接到AD
await ldapInstance.connect();
// AD用户认证
res = await ldapInstance.login("username", "password");
console.log("认证结果:", res);
// 获取AD的OU信息
res = await ldapInstance.OU("sub");
console.log("OU树结构数据:", res);
// 获取AD的用户信息
res = await ldapInstance.USER("sub");
console.log("用户数据:", res);
// 获取AD的部门及用户
res = await ldapInstance.List("sub");
console.log("部门及用户数据:", res);
// 查询某个OU下的用户
res = await ldapInstance.search({
filter: '(&(objectClass=user)(memberOf=CN=xxx,OU=xxx,OU=Users,OU=example,DC=example,DC=com))',
scope: 'sub',
attributes: ['*'],
});
console.log("查询结果:", res);版本迭代记录
2026-03-13 v0.0.2
- 基础框架搭建。
