add mail address import capability from LDAP
Schplurtz le Déboulonné committed
May 15, 2020 at 09:38 UTC
9220c54aea20148f900ce8266499155a18e8600c
1 file changed
+65
-1
webserver.js
+65
-1
@@ -370,10 +370,25 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
370
if (shortname == null) { fn(new Error('no user identifier')); return; }
371
var userid = 'user/' + domain.id + '/' + shortname;
372
var user = obj.users[userid];
373
+ var email = null;
374
+ if (domain.ldapuseremail) {
375
+ email = xxuser[domain.ldapuseremail];
376
+ } else if (xxuser.mail) { // use default
377
+ email = xxuser.mail;
378
+ }
379
+ if ('[object Array]' == Object.prototype.toString.call(email)) {
380
+ // mail may be multivalued in ldap in which case, answer is an array. Use the 1st value.
381
+ email=email[0];
382
+ }
383
+ if (email) { email = email.toLowerCase(); } // it seems some code otherwhere also lowercase the emailaddress. be compatible.
384
385
if (user == null) {
386
// Create a new user
387
var user = { type: 'user', _id: userid, name: username, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000), domain: domain.id };
388
+ if (email) {
389
+ user['email'] = email;
390
+ user['emailVerified'] = true;
391
+ }
392
if (domain.newaccountsrights) { user.siteadmin = domain.newaccountsrights; }
393
var usercount = 0;
394
for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
@@ -394,6 +409,23 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
409
if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
410
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
411
}
412
+ // Check if user email has changed
413
+ var emailreason = null;
414
+ if (user.email && ! email) { // email unset in ldap => unset
415
+ delete user.email;
416
+ delete user.emailVerified;
417
+ emailreason = 'Unset email (no more email in LDAP)'
418
+ } else if (user.email != email) { // update email
419
+ user['email'] = email;
420
+ user['emailVerified'] = true;
421
+ emailreason = 'Set account email to ' + email + '. Sync with LDAP.';
422
+ }
423
+ if (emailreason) {
424
+ obj.db.SetUser(user);
425
+ var event = { etype: 'user', userid: userid, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msg: emailreason, domain: domain.id };
426
+ if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
427
+ parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
428
+ }
429
// If user is locker out, block here.
430
if ((user.siteadmin) && (user.siteadmin != 0xFFFFFFFF) && (user.siteadmin & 32) != 0) { fn('locked'); return; }
431
return fn(null, user._id);
@@ -407,6 +439,17 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
439
try { ldap.close(); } catch (ex) { console.log(ex); } // Close the LDAP object
440
if (err) { fn(new Error('invalid password')); return; }
441
var shortname = null;
442
+ var email = null;
443
+ if (domain.ldapuseremail) {
444
+ email = xxuser[domain.ldapuseremail];
445
+ } else if (xxuser.mail) {
446
+ email = xxuser.mail;
447
+ }
448
+ if ('[object Array]' == Object.prototype.toString.call(email)) {
449
+ // mail may be multivalued in ldap in which case, answer would be an array. Use the 1st one.
450
+ email=email[0];
451
+ }
452
+ if (email) { email = email.toLowerCase(); } // it seems some code otherwhere also lowercase the emailaddress. be compatible.
453
var username = xxuser['displayName'];
454
if (domain.ldapusername) { username = xxuser[domain.ldapusername]; }
455
if (domain.ldapuserbinarykey) {
@@ -430,6 +473,10 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
473
if (user == null) {
474
// This user does not exist, create a new account.
475
var user = { type: 'user', _id: userid, name: shortname, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000), domain: domain.id };
476
+ if (email) {
477
+ user['email'] = email;
478
+ user['emailVerified'] = true;
479
+ }
480
if (domain.newaccountsrights) { user.siteadmin = domain.newaccountsrights; }
481
var usercount = 0;
482
for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
@@ -450,6 +497,23 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
497
if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
498
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
499
}
500
+ // Check if user email has changed
501
+ var emailreason = null;
502
+ if (user.email && ! email) { // email unset in ldap => unset
503
+ delete user.email;
504
+ delete user.emailVerified;
505
+ emailreason = 'Unset email (no more email in LDAP)'
506
+ } else if (user.email != email) { // update email
507
+ user['email'] = email;
508
+ user['emailVerified'] = true;
509
+ emailreason = 'Set account email to ' + email + '. Sync with LDAP.';
510
+ }
511
+ if (emailreason) {
512
+ obj.db.SetUser(user);
513
+ var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msg: emailreason, domain: domain.id };
514
+ if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
515
+ parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
516
+ }
517
// If user is locker out, block here.
518
if ((user.siteadmin) && (user.siteadmin != 0xFFFFFFFF) && (user.siteadmin & 32) != 0) { fn('locked'); return; }
519
return fn(null, user._id);
@@ -5410,4 +5474,4 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
5474
}
5475
5476
return obj;
5413
-};
\ No newline at end of file
5477
+};