UNPKG

2.02 kBJavaScriptView Raw
1'use strict'
2const ldap = require('ldapjs')
3const ldapChanges = require('./changes')
4const utils = require('./utils')
5
6module.exports = createUser
7
8function createUser (params) {
9 return new Promise((resolve, reject) => {
10 // console.log('starting LDAP password reset')
11 // validate input
12 // if (
13 // (!userDn || userDn === '') &&
14 // (!upn || upn === '') &&
15 // (!username || username === '') &&
16 // (!email || email === '')
17 // ) {
18 // // inform the user of the error of their ways
19 // return reject('userDn, upn, username, or email is required')
20 // }
21 // // continue validating input
22 // if (!newPassword || newPassword === '') {
23 // // inform the user of the error of their ways
24 // return reject('newPassword is required')
25 // }
26 // create client connection
27 const client = this.getClient()
28 // login to LDAP
29 client.bind(params.adminDn, params.adminPassword, async (err) => {
30 // console.log('ldap client bind')
31 if (err) {
32 console.log(err)
33 client.destroy()
34 return reject(err)
35 }
36
37 const commonName = `${params.firstName} ${params.lastName}`
38 const userPrincipalName = `${params.username}@${params.domain}`
39 const entryDN = `CN=${commonName},${params.usersDn}`
40 const newUser = {
41 samAccountName: params.username,
42 name: commonName,
43 cn: commonName,
44 givenName: params.firstName,
45 sn: params.lastName,
46 displayName: commonName,
47 physicalDeliveryOfficeName: String(params.userId) || '',
48 mail: params.email,
49 userPassword: params.password,
50 objectClass: ["top", "person", "organizationalPerson", "user"],
51 userPrincipalName,
52 telephoneNumber: String(params.userId) ? '41' + String(params.userId) : ''
53 }
54 // create new user
55 client.add(entryDN, newUser, (err2, user) => {
56 client.destroy()
57 if (err2) reject(err2)
58 resolve(user)
59 })
60
61 })
62 })
63}