-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-import.js
283 lines (236 loc) · 10.5 KB
/
user-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @copyright Copyright (C) REALTECH AG, Germany - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Tobias Ceska <tobias.ceska@realtech.com>, December 2019
*/
"use strict";
const _=require("lodash")
, promiseLimit = require('p-limit')
, createDot4Client = require('dot4-api-client')
, Person = require('dot4-api-client/src/models/person')
;
//http://www.selfadsi.de/ads-attributes/user-userAccountControl.htm in binaer umwandeln und vorletzte Ziffer holen
const adUserDeactivated=(userAccountControl)=>{
return parseInt(userAccountControl,10).toString(2).substr(-2).substr(0,1) == '1'
}
module.exports = function(RED) {
function userImport(config) {
RED.nodes.createNode(this,config)
const node = this
, dot4ConfigNode = RED.nodes.getNode(config.dot4config)
, CI_TYPE_EXT_ID = "externalUserIdForImport"
, promiseLimitCollect = promiseLimit(4);
;
let alreadyRunning=false
, CI_TYPE_EXT_ID_PERS
;
if(dot4ConfigNode && dot4ConfigNode.username && _.get(dot4ConfigNode,"credentials.password") && dot4ConfigNode.tenant && dot4ConfigNode.url){
// node.log(JSON.stringify(dot4ConfigNode))
const dot4config = {
user: dot4ConfigNode.username
, password: _.get(dot4ConfigNode,"credentials.password")
, tenant: dot4ConfigNode.tenant
, baseUrl: dot4ConfigNode.url
, proxy: {
url: dot4ConfigNode.proxyurl
, username: dot4ConfigNode.proxyusername
, password: _.get(dot4ConfigNode,"credentials.proxypassword")
}
};
let dot4Client
, userManagementApi
;
node.on('input', async function(msg) {
if(alreadyRunning){
node.log("########### can't start multiple times #################")
} else {
try{
alreadyRunning=true;
if(!dot4Client || !userManagementApi) {
node.log(`createDot4Client. baseUrl: ${dot4config.baseUrl}, user: ${dot4config.user}, tenant: ${dot4config.tenant}, proxy: ${JSON.stringify(dot4config.proxy)}`)
node.status({fill:"green",shape:"ring",text:"connecting"});
dot4Client = createDot4Client(dot4config);
await dot4Client.connect();
node.log("connected to dot4")
userManagementApi=await dot4Client.createUserManagementApi()
}
node.status({fill:"green",shape:"ring",text:"loading metadata"});
const successfullyImportedUsers=[]
, preScriptUsers=await userManagementApi.loadAllUsers()
, existingDepartments=[]
, existingCompanies=await userManagementApi.loadAllCompanies()
;
let attrType=await userManagementApi.createOrActivateCiAttributeTypeIfNeeded(Person.getCiTypeAlias(), CI_TYPE_EXT_ID)
CI_TYPE_EXT_ID_PERS=attrType.propertyName
let uploadArray=msg.payload
, uploadError
;
if(_.get(config,"format")=="activeDirectory") {
if(_.get(msg,"payload.users"))
uploadArray=msg.payload.users
uploadArray=_.sortBy(uploadArray, ['sn', 'givenName']);
}
if(!_.isArray(uploadArray))
uploadArray=[uploadArray];
let supervisorAttrName=_.get(config,"supervisor_attr_name") || 'supervisor'
, supervisorAttrRef=_.get(config,"supervisor_attr_refname") || 'email'
if(_.get(config,"format")=="activeDirectory") {
supervisorAttrName="manager"
supervisorAttrRef="distinguishedName"
}
//create needed departments and companies, cannot be done during parallel user upload
for(const userParams of uploadArray){
let isDeactivated=false;
if(_.has(userParams,"isDeactivated"))
isDeactivated=userParams.isDeactivated
if(_.get(config,"format")=="activeDirectory") {
isDeactivated=adUserDeactivated(userParams.userAccountControl)
}
if(userParams.company && !isDeactivated){
let company=_.find(existingCompanies, {name: userParams.company})
if(!company ){
company=await userManagementApi.createCompany({name: userParams.company});
existingCompanies.push(company);
}
if(company) {
if(userParams.department){
let department=_.find(existingDepartments, {name: userParams.department, company_DEPA: company.id})
|| await userManagementApi.loadDepartment(userParams.department, company.id)
;
if(!department ){
node.log("creating department "+userParams.department)
department=await userManagementApi.createDepartment({name: userParams.department, company_DEPA: company.id});
}
if(department && existingDepartments.indexOf(department)==-1){
node.log("loaded department: "+department.name)
existingDepartments.push(department);
}
}
}
}
}
//upload users
let collectedPromises=[]
, userCnt=0
for(const userParams of uploadArray){
collectedPromises.push(promiseLimitCollect(async ()=>{
// node.log(JSON.stringify(userParams))
let dot4user;
_.forEach(userParams, (v,k)=>{
if(_.isString(v))
userParams[k]=v.trim()
})
if(_.get(config,"format")=="activeDirectory") {
dot4user={
email: userParams.mail
, firstName: userParams.givenName
, lastName: userParams.sn
, personnelNumber: userParams.employeeID
, isDeactivated: adUserDeactivated(userParams.userAccountControl)
, faxBusiness: userParams.facsimileTelephoneNumber
, description: userParams.displayName
, name: userParams.displayName
}
if(userParams.telephoneNumber)
dot4user.telephoneNumbersBusiness=[userParams.telephoneNumber]
if(userParams.mobile)
dot4user.mobilePhoneNumbersBusiness=[userParams.mobile]
//Custom Properties
_.forEach(userParams, (v,k)=>{
let k_PERS=k;
if(!k_PERS.endsWith("_PERS"))
k_PERS+='_PERS';
// node.log(`set ${k_PERS}`)
dot4user[k_PERS]=v;
})
} else {
dot4user=userParams
}
dot4user[CI_TYPE_EXT_ID_PERS]=dot4user[supervisorAttrRef] || dot4user[supervisorAttrRef+"_PERS"]
if(!_.get(dot4user,"email") ||!_.get(dot4user,"firstName") ||!_.get(dot4user,"lastName")){
throw new Error("missing user parameters/mapping")
}
let existingUser=_.find(preScriptUsers, o=>o.email_PERS.toLowerCase()==dot4user.email.toLowerCase());
if(_.get(config,"person_or_user")=="person") {
dot4user.userId_PERS = _.get(existingUser,"userId_PERS") //behalte bisherige Einstellung des CIs
dot4user.userExisting_PERS = _.get(existingUser, "userExisting_PERS")
}
//set company and department
if(userParams.company){
let company=_.find(existingCompanies, {name: userParams.company})
if(company) {
dot4user.company_PERS=company.id;
if(userParams.department){
let department=_.find(existingDepartments, {name: userParams.department, company_DEPA: company.id})
if(department) {
dot4user.department_PERS=department.id;
} else {
node.log(`cannot set department [${userParams.department}] at user [${dot4user.firstName} ${dot4user.lastName}]`)
}
}
} else {
node.log(`cannot set company [${userParams.company}] at user [${dot4user.firstName} ${dot4user.lastName}]`)
}
}
node.log(`uploading/updating in dot4: ${dot4user.firstName} ${dot4user.lastName}`);
// node.log("----------userImport: "+JSON.stringify(dot4user))
node.status({fill:"blue",shape:"ring",text:`processing ${dot4user.lastName} (${Math.round(++userCnt/uploadArray.length*100)}%)`});
let uploadResult=await userManagementApi.upsertPerson(dot4user);
if(uploadResult instanceof Error) {
uploadError=uploadResult.message
node.log(uploadError)
} else if(uploadResult){
// node.log('uploadResult: '+JSON.stringify(uploadResult))
successfullyImportedUsers.push({userParams, uploadResult})
}
}))
}
await Promise.all(collectedPromises)
//set supervisor relations
//------------------------
node.log("--------------------- set supervisor relations ---------------------------------------------")
collectedPromises=[]
//filter for users who have a supervisor
for(const uploaded of _.filter(successfullyImportedUsers, s=>_.get(s,"userParams."+supervisorAttrName) )){
collectedPromises.push(promiseLimitCollect(async ()=>{
node.status({fill:"blue",shape:"ring",text:`supervisor ${_.get(uploaded,"uploadResult.lastName_PERS")}`});
let supervisor=_.get(_.find(successfullyImportedUsers, o=>o.userParams[supervisorAttrRef]==uploaded.userParams[supervisorAttrName]), 'uploadResult')
|| _.find(preScriptUsers, o=>o[CI_TYPE_EXT_ID_PERS]==uploaded.userParams[supervisorAttrName])
;
if(supervisor){
uploaded.uploadResult.supervisor_PERS = supervisor.id
} else {
node.log(`cannot set supervisor [${uploaded.userParams[supervisorAttrName]}] for [${_.get(uploaded,"uploadResult.firstName_PERS")} ${_.get(uploaded,"uploadResult.lastName_PERS")}]`)
// node.log(JSON.stringify(_.get(uploaded,"uploadResult")))
return
}
let uploadResult=await userManagementApi.updatePerson(uploaded.uploadResult);
if(uploadResult instanceof Error)
node.log(uploadResult.message)
}))
}
await Promise.all(collectedPromises)
msg.payload=`imported ${successfullyImportedUsers.length}/${uploadArray.length} user(s) into dot4`
node.log(msg.payload)
node.send(msg);
alreadyRunning=false;
if( successfullyImportedUsers.length == uploadArray.length)
node.status({fill:"green",shape:"dot",text:"finished"});
else
node.status({fill:"red",shape:"dot",text: uploadError || "finished with error(s)"});
} catch(e) {
node.log("ERROR: "+e)
node.status({fill:"red",shape:"dot",text: `${e}`});
alreadyRunning=false;
msg.payload=`${e}`
node.send(msg)
}
}
});
} else {
node.status({fill:"red",shape:"ring",text:"missing params"});
}
}
RED.nodes.registerType("user-import",userImport);
}