styles bugfix
This commit is contained in:
@@ -56,6 +56,30 @@ class ActiveDirectoryManager {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async getAllUsers(attributes = this.userAttributes) {
|
||||
const options = {
|
||||
baseDN: this.ad.baseDN,
|
||||
filter: '(&(objectClass=user)(objectCategory=person))',
|
||||
attributes: ['ObjectGUID'
|
||||
,'sAMAccountName'
|
||||
,'mail'
|
||||
,'givenName'
|
||||
,'sn'
|
||||
,'employeeID'
|
||||
,'title'
|
||||
,'department'
|
||||
,'streetAddress'
|
||||
,'telephoneNumber'
|
||||
,'physicalDeliveryOfficeName'
|
||||
,'distinguishedName']
|
||||
};
|
||||
const result = await this.ldapSearch(options);
|
||||
console.log(result)
|
||||
return result.users || [];
|
||||
}
|
||||
|
||||
|
||||
async getUserDN(username) {
|
||||
const user = await this.getUser(username);
|
||||
return user?.dn || null;
|
||||
@@ -256,3 +280,309 @@ class ActiveDirectoryManager {
|
||||
}
|
||||
|
||||
module.exports = ActiveDirectoryManager;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// const ldap = require('ldapjs');
|
||||
|
||||
// class ActiveDirectoryManager {
|
||||
// constructor({
|
||||
// url,
|
||||
// baseDN,
|
||||
// username,
|
||||
// password,
|
||||
// userAttributes = [],
|
||||
// groupAttributes = [],
|
||||
// computerAttributes = []
|
||||
// }) {
|
||||
// this.url = url;
|
||||
// this.baseDN = baseDN;
|
||||
// this.username = username;
|
||||
// this.password = password;
|
||||
|
||||
// this.userAttributes = userAttributes;
|
||||
// this.groupAttributes = groupAttributes;
|
||||
// this.computerAttributes = computerAttributes;
|
||||
|
||||
// this.client = ldap.createClient({
|
||||
// url: this.url,
|
||||
// reconnect: true,
|
||||
// timeout: 10000,
|
||||
// connectTimeout: 10000
|
||||
// });
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * -----------------------------------------------------
|
||||
// * CONNECTION HANDLING
|
||||
// * -----------------------------------------------------
|
||||
// */
|
||||
// async bind() {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// this.client.bind(this.username, this.password, (err) => {
|
||||
// if (err) return reject(err);
|
||||
// resolve();
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// async unbind() {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// this.client.unbind(err => {
|
||||
// if (err) return reject(err);
|
||||
// resolve();
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// async withConnection(fn) {
|
||||
// try {
|
||||
// await this.bind();
|
||||
// return await fn();
|
||||
// } finally {
|
||||
// await this.unbind();
|
||||
// }
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * -----------------------------------------------------
|
||||
// * INTERNAL HELPERS
|
||||
// * -----------------------------------------------------
|
||||
// */
|
||||
|
||||
// escape(value) {
|
||||
// return String(value).replace(/[*()\\]/g, '\\$&');
|
||||
// }
|
||||
|
||||
// async ldapSearch({ baseDN = this.baseDN, filter, attributes = [] }) {
|
||||
// const opts = {
|
||||
// filter,
|
||||
// scope: 'sub',
|
||||
// attributes,
|
||||
// paged: true
|
||||
// };
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// const results = [];
|
||||
|
||||
// this.client.search(baseDN, opts, (err, res) => {
|
||||
// if (err) return reject(err);
|
||||
|
||||
// res.on('searchEntry', (entry) => {
|
||||
// results.push(entry.object);
|
||||
// });
|
||||
|
||||
// res.on('error', (err) => reject(err));
|
||||
// res.on('end', () => resolve(results));
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * -----------------------------------------------------
|
||||
// * USER FUNCTIONS
|
||||
// * -----------------------------------------------------
|
||||
// */
|
||||
|
||||
// async getUser(username, attributes = this.userAttributes) {
|
||||
// const safe = this.escape(username);
|
||||
|
||||
// const filter = `(&(objectCategory=person)(objectClass=user)(|(sAMAccountName=${safe})(mail=${safe})(cn=${safe}))))`;
|
||||
|
||||
// const res = await this.ldapSearch({ filter, attributes });
|
||||
// return res[0] || null;
|
||||
// }
|
||||
|
||||
// async getUserDN(username) {
|
||||
// const user = await this.getUser(username);
|
||||
// return user?.distinguishedName || null;
|
||||
// }
|
||||
|
||||
// async findUsers(query, attributes = this.userAttributes) {
|
||||
// const safe = this.escape(query);
|
||||
|
||||
// const filter = `(&(objectCategory=person)(objectClass=user)(|(cn=${safe})(sAMAccountName=${safe})(mail=${safe})(displayName=${safe})))`;
|
||||
|
||||
// return await this.ldapSearch({ filter, attributes });
|
||||
// }
|
||||
|
||||
// async getAllUsers(attributes = this.userAttributes) {
|
||||
// const filter = '(&(objectCategory=person)(objectClass=user))';
|
||||
|
||||
// return await this.ldapSearch({ filter, attributes });
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * -----------------------------------------------------
|
||||
// * GROUP FUNCTIONS
|
||||
// * -----------------------------------------------------
|
||||
// */
|
||||
|
||||
// async getGroup(groupName, attributes = this.groupAttributes) {
|
||||
// const safe = this.escape(groupName);
|
||||
|
||||
// const filter = `(&(objectClass=group)(cn=${safe}))`;
|
||||
|
||||
// const res = await this.ldapSearch({ filter, attributes });
|
||||
// return res[0] || null;
|
||||
// }
|
||||
|
||||
// async findGroups(query, attributes = this.groupAttributes) {
|
||||
// const safe = this.escape(query);
|
||||
|
||||
// const filter = `(&(objectClass=group)(cn=${safe}))`;
|
||||
|
||||
// return await this.ldapSearch({ filter, attributes });
|
||||
// }
|
||||
|
||||
// async getAllGroups(attributes = this.groupAttributes) {
|
||||
// const filter = '(objectClass=group)';
|
||||
|
||||
// return await this.ldapSearch({ filter, attributes });
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * -----------------------------------------------------
|
||||
// * COMPUTER / OU FUNCTIONS
|
||||
// * -----------------------------------------------------
|
||||
// */
|
||||
|
||||
// async getComputer(name, attributes = this.computerAttributes) {
|
||||
// const safe = this.escape(name);
|
||||
|
||||
// const filter = `(&(objectClass=computer)(|(cn=${safe})(dNSHostName=${safe})))`;
|
||||
|
||||
// const res = await this.ldapSearch({ filter, attributes });
|
||||
// return res[0] || null;
|
||||
// }
|
||||
|
||||
// async getComputers(attributes = this.computerAttributes) {
|
||||
// const filter = '(objectClass=computer)';
|
||||
|
||||
// return await this.ldapSearch({ filter, attributes });
|
||||
// }
|
||||
|
||||
// async getComputersFromOU(ouDn, attributes = this.computerAttributes) {
|
||||
// const filter = '(objectClass=computer)';
|
||||
|
||||
// return await this.ldapSearch({
|
||||
// baseDN: ouDn,
|
||||
// filter,
|
||||
// attributes
|
||||
// });
|
||||
// }
|
||||
|
||||
// async findComputers(query, attributes = this.computerAttributes) {
|
||||
// const safe = this.escape(query);
|
||||
|
||||
// const filter = `(&(objectClass=computer)(|(cn=${safe})(dNSHostName=${safe})))`;
|
||||
|
||||
// return await this.ldapSearch({ filter, attributes });
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * -----------------------------------------------------
|
||||
// * GROUP MEMBERSHIP
|
||||
// * -----------------------------------------------------
|
||||
// */
|
||||
|
||||
// async isUserMemberOfDirect(username, groupName) {
|
||||
// const user = await this.getUser(username, ['distinguishedName']);
|
||||
// if (!user) return false;
|
||||
|
||||
// const userDN = user.distinguishedName;
|
||||
// const safeGroup = this.escape(groupName);
|
||||
|
||||
// const filter = `(&(objectClass=group)(cn=${safeGroup})(member=${userDN}))`;
|
||||
|
||||
// const res = await this.ldapSearch({ filter, attributes: ['cn'] });
|
||||
// return res.length > 0;
|
||||
// }
|
||||
|
||||
// async isUserMemberOfRecursive(username, groupName, visited = new Set()) {
|
||||
// const key = groupName.toLowerCase();
|
||||
// if (visited.has(key)) return false;
|
||||
// visited.add(key);
|
||||
|
||||
// const direct = await this.isUserMemberOfDirect(username, groupName);
|
||||
// if (direct) return true;
|
||||
|
||||
// const group = await this.getGroup(groupName, ['member']);
|
||||
// if (!group || !group.member) return false;
|
||||
|
||||
// const members = Array.isArray(group.member) ? group.member : [group.member];
|
||||
|
||||
// for (const dn of members) {
|
||||
// const match = dn.match(/CN=([^,]+)/i);
|
||||
// if (!match) continue;
|
||||
|
||||
// const subGroup = match[1];
|
||||
// const found = await this.isUserMemberOfRecursive(username, subGroup, visited);
|
||||
// if (found) return true;
|
||||
// }
|
||||
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// async getGroupSubgroups(groupName, visited = new Set()) {
|
||||
// const key = groupName.toLowerCase();
|
||||
// if (visited.has(key)) return [];
|
||||
|
||||
// visited.add(key);
|
||||
|
||||
// const group = await this.getGroup(groupName, ['member']);
|
||||
// if (!group || !group.member) return [];
|
||||
|
||||
// const members = Array.isArray(group.member) ? group.member : [group.member];
|
||||
|
||||
// const results = [];
|
||||
|
||||
// for (const dn of members) {
|
||||
// const match = dn.match(/CN=([^,]+)/i);
|
||||
// if (!match) continue;
|
||||
|
||||
// const subGroupName = match[1];
|
||||
// const sub = await this.getGroup(subGroupName).catch(() => null);
|
||||
// if (!sub) continue;
|
||||
|
||||
// results.push(sub);
|
||||
// results.push(...await this.getGroupSubgroups(subGroupName, visited));
|
||||
// }
|
||||
|
||||
// return results;
|
||||
// }
|
||||
|
||||
// async getGroupRecursive(groupName, visited = new Set()) {
|
||||
// const key = groupName.toLowerCase();
|
||||
// if (visited.has(key)) return null;
|
||||
|
||||
// visited.add(key);
|
||||
|
||||
// const group = await this.getGroup(groupName);
|
||||
// if (!group) return null;
|
||||
|
||||
// const result = {
|
||||
// ...group,
|
||||
// subgroups: []
|
||||
// };
|
||||
|
||||
// if (!group.member) return result;
|
||||
|
||||
// const members = Array.isArray(group.member) ? group.member : [group.member];
|
||||
|
||||
// for (const dn of members) {
|
||||
// const match = dn.match(/CN=([^,]+)/i);
|
||||
// if (!match) continue;
|
||||
|
||||
// const subGroupName = match[1];
|
||||
// const subTree = await this.getGroupRecursive(subGroupName, visited);
|
||||
// if (subTree) result.subgroups.push(subTree);
|
||||
// }
|
||||
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
|
||||
// module.exports = ActiveDirectoryManager;
|
||||
|
||||
Reference in New Issue
Block a user