ad-util
v1.1.6
Published
Wrapper around common Active Directory tasks, allowing for object-based retrieval of user and group data, password changes, account disabling, and object creation and deletion.
Downloads
25
Maintainers
Readme
Active Directory Utility
This module provides a wrapper around common Active Directory tasks. It leverages the activedirectory and ldapjs packages for all LDAP calls.
Getting Started
Use the package's default export to instantiate an instance of the DirectoryUtility class.
const DirectoryUtility = require('ad-util');
const utility = new DirectoryUtility({
url: 'ldaps://10.0.1.1',
username: '[email protected]',
password: 'P@ssw0rd!',
});
The class constructor also passes any additional arguments on to the the ActiveDirectory constructor from the activedirectory package.
API
DirectoryUtility
The DirectoryUtility class exposes the following methods:
async getUsers(filter, location) -> [User]
Returns an array of Users, based on the provided filter.
Arguments
- String filter (optional) - The filter to use when searching.
- String location (optional) - The location to search in.
// Get all users
const users = await utility.getUsers();
// Get all users whose names start with "John"
const users = await utility.getUsers('(CN=John *)');
// Get all users in the Accounting/Users OU.
const users = await utility.getUsers(null, 'Accounting/Users');
async getUser(username) -> User
Returns the specified user, or undefined.
Arguments
- String username - The common name or sAMAccountName to search for.
const user = await utility.getUser('jsmith');
async addUser(location, attributes, password) -> User
Creates a new user.
Arguments
- String location - The location the object should be placed in path format (e.g: 'Corporation/Users').
- Object attributes - An object containing attributes to set on the user.
- String password (optional) - The password to set for the user (or none).
const user = await utility.addUser(
'Corporation/Users',
{
givenName: 'John',
sn: 'Smith',
sAMAccountName: 'jsmith',
},
'P@ssw0rd!',
);
async getGroups(filter, location) -> [Group]
Returns an array of Groups, based on the provided filter.
Arguments
- String filter (optional) - The filter to use when searching.
- String location (optional) - The location to search in.
// Get all groups
const groups = await utility.getGroups();
// Get all groups whose names start with "Domain"
const groups = await utility.getGroups('(CN=Domain *)');
// Get all groups in the Accounting/Groups OU.
const groups = await utility.getGroups(null, 'Accounting/Groups');
async getGroup(name) -> Group
Returns the specified group, or undefined.
Arguments
- String name - The common name or sAMAccountName to search for.
const group = await utility.getGroup('Administrators');
async addGroup(location, attributes) -> Group
Creates a new group.
Arguments
- String location - The location the object should be placed in path format (e.g: 'Corporation/Groups').
- Object attributes - An object containing attributes to set on the group.
const group = await utility.addGroup(
'Corporation/Groups',
{
sAMAccountName: 'Accounting',
},
);
DirectoryObject (Superclass)
The User and Group classes extend this class and expose the following properties and methods:
dn -> String
Returns the object's distinguished name.
console.log(object.dn);
cn -> String
Returns the object's common name.
console.log(object.cn);
groups -> [String]
Returns the object's group membership.
console.log(object.groups);
isMemberOf(groupName) -> Boolean
Returns a value indicating whether the user is a member of a group.
Arguments
- String group - The group's common name.
const admin = object.isMemberOf('Domain Administrators');
async reload()
Manually reloads the object's data.
await object.reload();
async set(attributes)
Sets attributes on the object and reloads the object's data.
Arguments
- Object attributes - An object containing attributes to set on the object.
await object.set({ sn: 'Doe' });
async remove()
Deletes the object.
await object.remove();
async move(location)
Move the object to a new location.
Arguments
- String location - The location the object should be placed in path format (e.g: 'Corporation/Accounting').
await object.move('Corporation/Accounting');
async setCommonName(commonName)
Move the object to a new location.
Arguments
- String commonName - The new common name for the object.
await object.setCommonName('New Object');
async addToGroup(group)
Adds the object to a Group.
Arguments
- Group group - The group to add the object to.
const group = await utility.getGroup('Administrators');
await object.addToGroup(group);
async removeFromGroup(group)
Removes the object from a Group.
Arguments
- Group group - The remove to remove the object from.
const group = await utility.getGroup('Administrators');
await object.removeFromGroup(group);
User
The User class exposes the following properties and methods:
locked -> Boolean
Returns true is the user is locked, otherwise false.
console.log(user.locked);
enabled -> Boolean
Returns true is the user is enabled, otherwise false.
console.log(user.enabled);
disabled -> Boolean
Returns true is the user is disabled, otherwise false.
console.log(user.disabled);
mustChangePassword -> Boolean
Returns true is the user must change their password, otherwise false.
console.log(user.mustChangePassword);
async enable()
Enables the user's account.
await user.enable();
async disable()
Disables the user's account.
await user.disable();
async unlock()
Unlocks the user's account.
await user.unlock();
async forcePasswordChange()
Forces the user to change their password on their next log on.
await user.forcePasswordChange();
async setPassword(password)
Changes the user's password.
Arguments
- String password - The new password.
await user.setPassword('P@ssw0rd!');
Group
The Group class exposes the following properties and methods:
members -> [String]
Returns the group's member's distinguished names.
console.log(group.members);
async addMember(member)
Adds a member to the group.
Arguments
- DirectoryObject member - The object to add to the group.
const user = await utility.getUser('jsmith');
await group.addMember(user);
async removeMember(member)
Removes a member from the group.
Arguments
- DirectoryObject member - The object to remove from the group.
const user = await utility.getUser('jsmith');
await group.removeMember(user);
Notes
Undocumented Methods
There are several undocumented, lower-level methods in the code. These are designed to be easily accessible and allow you perform low-level operations when necessary.