mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-09-11 08:55:32 +03:00
LDAP improvements (#4283)
This commit is contained in:
parent
0489e2d571
commit
466c765df5
374
webserver.js
374
webserver.js
@ -440,253 +440,177 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
|
|||||||
}, 0);
|
}, 0);
|
||||||
});
|
});
|
||||||
} else if (domain.auth == 'ldap') {
|
} else if (domain.auth == 'ldap') {
|
||||||
if (domain.ldapoptions.url == 'test') {
|
// This method will handle LDAP login
|
||||||
// Fake LDAP login
|
const ldapHandler = function ldapHandlerFunc(err, xxuser) {
|
||||||
var xxuser = domain.ldapoptions[name.toLowerCase()];
|
if (ldapHandlerFunc.ldapobj) { try { ldapHandlerFunc.ldapobj.close(); } catch (ex) { console.log(ex); } } // Close the LDAP object
|
||||||
if (xxuser == null) {
|
if (err) { fn(new Error('invalid password')); return; }
|
||||||
fn(new Error('invalid password'));
|
|
||||||
return;
|
// Save this LDAP user to file if needed
|
||||||
|
if (typeof domain.ldapsaveusertofile == 'string') {
|
||||||
|
obj.fs.appendFile(domain.ldapsaveusertofile, JSON.stringify(xxuser, null, 2) + '\r\n\r\n', function (err) { });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Work on getting the userid for this LDAP user
|
||||||
|
var shortname = null;
|
||||||
|
if ('[object Array]' == Object.prototype.toString.call(email)) {
|
||||||
|
// mail may be multivalued in ldap in which case, answer would be an array. Use the 1st one.
|
||||||
|
email = email[0];
|
||||||
|
}
|
||||||
|
if (email) { email = email.toLowerCase(); } // it seems some code otherwhere also lowercase the emailaddress. be compatible.
|
||||||
|
var username = xxuser['displayName'];
|
||||||
|
if (domain.ldapusername) { username = xxuser[domain.ldapusername]; }
|
||||||
|
if (domain.ldapuserbinarykey) {
|
||||||
|
// Use a binary key as the userid
|
||||||
|
if (xxuser[domain.ldapuserbinarykey]) { shortname = Buffer.from(xxuser[domain.ldapuserbinarykey], 'binary').toString('hex').toLowerCase(); }
|
||||||
|
} else if (domain.ldapuserkey) {
|
||||||
|
// Use a string key as the userid
|
||||||
|
if (xxuser[domain.ldapuserkey]) { shortname = xxuser[domain.ldapuserkey]; }
|
||||||
} else {
|
} else {
|
||||||
// Save this LDAP user to file if needed
|
// Use the default key as the userid
|
||||||
if (typeof domain.ldapsaveusertofile == 'string') {
|
if (xxuser.objectSid) { shortname = Buffer.from(xxuser.objectSid, 'binary').toString('hex').toLowerCase(); }
|
||||||
obj.fs.appendFile(domain.ldapsaveusertofile, JSON.stringify(xxuser, null, 2) + '\r\n\r\n', function (err) { });
|
else if (xxuser.objectGUID) { shortname = Buffer.from(xxuser.objectGUID, 'binary').toString('hex').toLowerCase(); }
|
||||||
}
|
else if (xxuser.name) { shortname = xxuser.name; }
|
||||||
|
else if (xxuser.cn) { shortname = xxuser.cn; }
|
||||||
|
}
|
||||||
|
if (shortname == null) { fn(new Error('no user identifier')); return; }
|
||||||
|
if (username == null) { username = shortname; }
|
||||||
|
var userid = 'user/' + domain.id + '/' + shortname;
|
||||||
|
|
||||||
// Work on getting the userid for this LDAP user
|
// Work on getting the email address for this LDAP user
|
||||||
var username = xxuser['displayName'];
|
var email = null;
|
||||||
if (domain.ldapusername) { username = xxuser[domain.ldapusername]; }
|
if (domain.ldapuseremail) { email = xxuser[domain.ldapuseremail]; } else if (xxuser.mail) { email = xxuser.mail; } // Use given feild name or default
|
||||||
var shortname = null;
|
if ('[object Array]' == Object.prototype.toString.call(email)) { email = email[0]; } // Mail may be multivalued in LDAP in which case, answer is an array. Use the 1st value.
|
||||||
if (domain.ldapuserbinarykey) {
|
if (email) { email = email.toLowerCase(); } // it seems some code elsewhere also lowercase the emailaddress, so let's be consistant.
|
||||||
// Use a binary key as the userid
|
|
||||||
if (xxuser[domain.ldapuserbinarykey]) { shortname = Buffer.from(xxuser[domain.ldapuserbinarykey], 'binary').toString('hex'); }
|
|
||||||
} else if (domain.ldapuserkey) {
|
|
||||||
// Use a string key as the userid
|
|
||||||
if (xxuser[domain.ldapuserkey]) { shortname = xxuser[domain.ldapuserkey]; }
|
|
||||||
} else {
|
|
||||||
// Use the default key as the userid
|
|
||||||
if (xxuser.objectSid) { shortname = Buffer.from(xxuser.objectSid, 'binary').toString('hex').toLowerCase(); }
|
|
||||||
else if (xxuser.objectGUID) { shortname = Buffer.from(xxuser.objectGUID, 'binary').toString('hex').toLowerCase(); }
|
|
||||||
else if (xxuser.name) { shortname = xxuser.name; }
|
|
||||||
else if (xxuser.cn) { shortname = xxuser.cn; }
|
|
||||||
}
|
|
||||||
if (shortname == null) { fn(new Error('no user identifier')); return; }
|
|
||||||
if (username == null) { username = shortname; }
|
|
||||||
var userid = 'user/' + domain.id + '/' + shortname;
|
|
||||||
|
|
||||||
// Work on getting the email address for this LDAP user
|
// Work on getting the real name for this LDAP user
|
||||||
var email = null;
|
var realname = null;
|
||||||
if (domain.ldapuseremail) { email = xxuser[domain.ldapuseremail]; } else if (xxuser.mail) { email = xxuser.mail; } // Use given feild name or default
|
if (domain.ldapuserrealname) { realname = xxuser[domain.ldapuserrealname]; }
|
||||||
if ('[object Array]' == Object.prototype.toString.call(email)) { email = email[0]; } // Mail may be multivalued in LDAP in which case, answer is an array. Use the 1st value.
|
|
||||||
if (email) { email = email.toLowerCase(); } // it seems some code elsewhere also lowercase the emailaddress, so let's be consistant.
|
|
||||||
|
|
||||||
// Work on getting the real name for this LDAP user
|
// Work on getting the phone number for this LDAP user
|
||||||
var realname = null;
|
var phonenumber = null;
|
||||||
if (domain.ldapuserrealname) { realname = xxuser[domain.ldapuserrealname]; }
|
if (domain.ldapuserphonenumber) { phonenumber = xxuser[domain.ldapuserphonenumber]; }
|
||||||
|
|
||||||
// Work on getting the real name for this LDAP user
|
// Work on getting the image of this LDAP user
|
||||||
var phonenumber = null;
|
/*
|
||||||
if (domain.ldapuserphonenumber) { phonenumber = xxuser[domain.ldapuserphonenumber]; }
|
var userimage = null;
|
||||||
|
if (domain.ldapuserimage && xxuser[domain.ldapuserimage]) {
|
||||||
|
console.log('IMAGE', Buffer.from(xxuser[domain.ldapuserimage], 'utf8'));
|
||||||
|
userimage = 'data:image/jpeg;base64,' + Buffer.from(xxuser[domain.ldapuserimage], 'binary').toString('base64');
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Check if the user already exists
|
// Display user information extracted from LDAP data
|
||||||
var user = obj.users[userid];
|
/*
|
||||||
if (user == null) {
|
console.log('shortname', shortname);
|
||||||
// Create a new user
|
console.log('email', email);
|
||||||
var user = { type: 'user', _id: userid, name: username, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000), access: Math.floor(Date.now() / 1000), domain: domain.id };
|
console.log('realname', realname);
|
||||||
if (email) { user['email'] = email; user['emailVerified'] = true; }
|
console.log('phonenumber', phonenumber);
|
||||||
if (domain.newaccountsrights) { user.siteadmin = domain.newaccountsrights; }
|
console.log('userimage', userimage);
|
||||||
if (obj.common.validateStrArray(domain.newaccountrealms)) { user.groups = domain.newaccountrealms; }
|
*/
|
||||||
var usercount = 0;
|
|
||||||
for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
|
|
||||||
if (usercount == 0) { user.siteadmin = 4294967295; /*if (domain.newaccounts === 2) { delete domain.newaccounts; }*/ } // If this is the first user, give the account site admin.
|
|
||||||
|
|
||||||
// Auto-join any user groups
|
// If there is a testing userid, use that
|
||||||
if (typeof domain.newaccountsusergroups == 'object') {
|
if (ldapHandlerFunc.ldapShortName) {
|
||||||
for (var i in domain.newaccountsusergroups) {
|
shortname = ldapHandlerFunc.ldapShortName;
|
||||||
var ugrpid = domain.newaccountsusergroups[i];
|
userid = 'user/' + domain.id + '/' + shortname;
|
||||||
if (ugrpid.indexOf('/') < 0) { ugrpid = 'ugrp/' + domain.id + '/' + ugrpid; }
|
}
|
||||||
var ugroup = obj.userGroups[ugrpid];
|
|
||||||
if (ugroup != null) {
|
|
||||||
// Add group to the user
|
|
||||||
if (user.links == null) { user.links = {}; }
|
|
||||||
user.links[ugroup._id] = { rights: 1 };
|
|
||||||
|
|
||||||
// Add user to the group
|
// Check if the user already exists
|
||||||
ugroup.links[user._id] = { userid: user._id, name: user.name, rights: 1 };
|
var user = obj.users[userid];
|
||||||
db.Set(ugroup);
|
if (user == null) {
|
||||||
|
// This user does not exist, create a new account.
|
||||||
|
var user = { type: 'user', _id: userid, name: username, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000), access: Math.floor(Date.now() / 1000), domain: domain.id };
|
||||||
|
if (email) { user['email'] = email; user['emailVerified'] = true; }
|
||||||
|
if (domain.newaccountsrights) { user.siteadmin = domain.newaccountsrights; }
|
||||||
|
if (obj.common.validateStrArray(domain.newaccountrealms)) { user.groups = domain.newaccountrealms; }
|
||||||
|
var usercount = 0;
|
||||||
|
for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
|
||||||
|
if (usercount == 0) { user.siteadmin = 4294967295; /*if (domain.newaccounts === 2) { delete domain.newaccounts; }*/ } // If this is the first user, give the account site admin.
|
||||||
|
|
||||||
// Notify user group change
|
// Auto-join any user groups
|
||||||
var event = { etype: 'ugrp', ugrpid: ugroup._id, name: ugroup.name, desc: ugroup.desc, action: 'usergroupchange', links: ugroup.links, msgid: 71, msgArgs: [user.name, ugroup.name], msg: 'Added user ' + user.name + ' to user group ' + ugroup.name, addUserDomain: domain.id };
|
if (typeof domain.newaccountsusergroups == 'object') {
|
||||||
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user group. Another event will come.
|
for (var i in domain.newaccountsusergroups) {
|
||||||
parent.DispatchEvent(['*', ugroup._id, user._id], obj, event);
|
var ugrpid = domain.newaccountsusergroups[i];
|
||||||
}
|
if (ugrpid.indexOf('/') < 0) { ugrpid = 'ugrp/' + domain.id + '/' + ugrpid; }
|
||||||
|
var ugroup = obj.userGroups[ugrpid];
|
||||||
|
if (ugroup != null) {
|
||||||
|
// Add group to the user
|
||||||
|
if (user.links == null) { user.links = {}; }
|
||||||
|
user.links[ugroup._id] = { rights: 1 };
|
||||||
|
|
||||||
|
// Add user to the group
|
||||||
|
ugroup.links[user._id] = { userid: user._id, name: user.name, rights: 1 };
|
||||||
|
db.Set(ugroup);
|
||||||
|
|
||||||
|
// Notify user group change
|
||||||
|
var event = { etype: 'ugrp', ugrpid: ugroup._id, name: ugroup.name, desc: ugroup.desc, action: 'usergroupchange', links: ugroup.links, msgid: 71, msgArgs: [user.name, ugroup.name], msg: 'Added user ' + user.name + ' to user group ' + ugroup.name, addUserDomain: domain.id };
|
||||||
|
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user group. Another event will come.
|
||||||
|
parent.DispatchEvent(['*', ugroup._id, user._id], obj, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
obj.users[user._id] = user;
|
obj.users[user._id] = user;
|
||||||
|
obj.db.SetUser(user);
|
||||||
|
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountcreate', msgid: 128, msgArgs: [user.name], msg: 'Account created, name is ' + user.name, domain: domain.id };
|
||||||
|
if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to create the user. Another event will come.
|
||||||
|
obj.parent.DispatchEvent(['*', 'server-users'], obj, event);
|
||||||
|
return fn(null, user._id);
|
||||||
|
} else {
|
||||||
|
// This is an existing user
|
||||||
|
// If the display username has changes, update it.
|
||||||
|
if (user.name != username) {
|
||||||
|
user.name = username;
|
||||||
obj.db.SetUser(user);
|
obj.db.SetUser(user);
|
||||||
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountcreate', msgid: 128, msgArgs: [user.name], msg: 'Account created, name is ' + user.name, domain: domain.id };
|
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msgid: 127, msgArgs: [user.name], msg: 'Changed account display name to ' + user.name, domain: domain.id };
|
||||||
if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to create the user. Another event will come.
|
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.
|
||||||
obj.parent.DispatchEvent(['*', 'server-users'], obj, event);
|
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
|
||||||
return fn(null, user._id);
|
}
|
||||||
|
// Check if user email has changed
|
||||||
|
var emailreason = null;
|
||||||
|
if (user.email && !email) { // email unset in ldap => unset
|
||||||
|
delete user.email;
|
||||||
|
delete user.emailVerified;
|
||||||
|
emailreason = 'Unset email (no more email in LDAP)'
|
||||||
|
} else if (user.email != email) { // update email
|
||||||
|
user['email'] = email;
|
||||||
|
user['emailVerified'] = true;
|
||||||
|
emailreason = 'Set account email to ' + email + '. Sync with LDAP.';
|
||||||
|
}
|
||||||
|
if (emailreason) {
|
||||||
|
obj.db.SetUser(user);
|
||||||
|
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msg: emailreason, domain: domain.id };
|
||||||
|
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.
|
||||||
|
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
|
||||||
|
}
|
||||||
|
// If user is locker out, block here.
|
||||||
|
if ((user.siteadmin) && (user.siteadmin != 0xFFFFFFFF) && (user.siteadmin & 32) != 0) { fn('locked'); return; }
|
||||||
|
return fn(null, user._id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (domain.ldapoptions.url == 'test') {
|
||||||
|
// Test LDAP login
|
||||||
|
var xxuser = domain.ldapoptions[name.toLowerCase()];
|
||||||
|
if (xxuser == null) { fn(new Error('invalid password')); return; } else {
|
||||||
|
ldapHandler.ldapShortName = name.toLowerCase();
|
||||||
|
if (typeof xxuser == 'string') {
|
||||||
|
// This test LDAP user points to a JSON file we user information, load it.
|
||||||
|
ldapHandler(null, require(xxuser));
|
||||||
} else {
|
} else {
|
||||||
// This is an existing user
|
// THe user information is in the config.json, use it.
|
||||||
// If the display username has changes, update it.
|
ldapHandler(null, xxuser);
|
||||||
if (user.name != username) {
|
|
||||||
user.name = username;
|
|
||||||
obj.db.SetUser(user);
|
|
||||||
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msgid: 127, msgArgs: [user.name], msg: 'Changed account display name to ' + user.name, domain: domain.id };
|
|
||||||
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.
|
|
||||||
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
|
|
||||||
}
|
|
||||||
// Check if user email has changed
|
|
||||||
var emailreason = null;
|
|
||||||
if (user.email && !email) { // email unset in ldap => unset
|
|
||||||
delete user.email;
|
|
||||||
delete user.emailVerified;
|
|
||||||
emailreason = 'Unset email (no more email in LDAP)'
|
|
||||||
} else if (user.email != email) { // update email
|
|
||||||
user['email'] = email;
|
|
||||||
user['emailVerified'] = true;
|
|
||||||
emailreason = 'Set account email to ' + email + '. Sync with LDAP.';
|
|
||||||
}
|
|
||||||
if (emailreason) {
|
|
||||||
obj.db.SetUser(user);
|
|
||||||
var event = { etype: 'user', userid: userid, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msg: emailreason, domain: domain.id };
|
|
||||||
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.
|
|
||||||
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
|
|
||||||
}
|
|
||||||
// If user is locker out, block here.
|
|
||||||
if ((user.siteadmin) && (user.siteadmin != 0xFFFFFFFF) && (user.siteadmin & 32) != 0) { fn('locked'); return; }
|
|
||||||
return fn(null, user._id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// LDAP login
|
// LDAP login
|
||||||
var LdapAuth = require('ldapauth-fork');
|
var LdapAuth = require('ldapauth-fork');
|
||||||
var ldap = new LdapAuth(domain.ldapoptions);
|
var ldap = new LdapAuth(domain.ldapoptions);
|
||||||
ldap.on('error', function (err) { console.log('ldap error: ', err); });
|
ldapHandler.ldapobj = ldap;
|
||||||
ldap.authenticate(name, pass, function (err, xxuser) {
|
ldap.on('error', function (err) {
|
||||||
try { ldap.close(); } catch (ex) { console.log(ex); } // Close the LDAP object
|
try { ldap.close(); } catch (ex) { console.log(ex); } // Close the LDAP object
|
||||||
if (err) { fn(new Error('invalid password')); return; }
|
console.log('ldap error: ', err);
|
||||||
|
|
||||||
// Save this LDAP user to file if needed
|
|
||||||
if (typeof domain.ldapsaveusertofile == 'string') {
|
|
||||||
obj.fs.appendFile(domain.ldapsaveusertofile, JSON.stringify(xxuser, null, 2) + '\r\n\r\n', function (err) { });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Work on getting the userid for this LDAP user
|
|
||||||
var shortname = null;
|
|
||||||
if ('[object Array]' == Object.prototype.toString.call(email)) {
|
|
||||||
// mail may be multivalued in ldap in which case, answer would be an array. Use the 1st one.
|
|
||||||
email = email[0];
|
|
||||||
}
|
|
||||||
if (email) { email = email.toLowerCase(); } // it seems some code otherwhere also lowercase the emailaddress. be compatible.
|
|
||||||
var username = xxuser['displayName'];
|
|
||||||
if (domain.ldapusername) { username = xxuser[domain.ldapusername]; }
|
|
||||||
if (domain.ldapuserbinarykey) {
|
|
||||||
// Use a binary key as the userid
|
|
||||||
if (xxuser[domain.ldapuserbinarykey]) { shortname = Buffer.from(xxuser[domain.ldapuserbinarykey], 'binary').toString('hex').toLowerCase(); }
|
|
||||||
} else if (domain.ldapuserkey) {
|
|
||||||
// Use a string key as the userid
|
|
||||||
if (xxuser[domain.ldapuserkey]) { shortname = xxuser[domain.ldapuserkey]; }
|
|
||||||
} else {
|
|
||||||
// Use the default key as the userid
|
|
||||||
if (xxuser.objectSid) { shortname = Buffer.from(xxuser.objectSid, 'binary').toString('hex').toLowerCase(); }
|
|
||||||
else if (xxuser.objectGUID) { shortname = Buffer.from(xxuser.objectGUID, 'binary').toString('hex').toLowerCase(); }
|
|
||||||
else if (xxuser.name) { shortname = xxuser.name; }
|
|
||||||
else if (xxuser.cn) { shortname = xxuser.cn; }
|
|
||||||
}
|
|
||||||
if (shortname == null) { fn(new Error('no user identifier')); return; }
|
|
||||||
if (username == null) { username = shortname; }
|
|
||||||
var userid = 'user/' + domain.id + '/' + shortname;
|
|
||||||
|
|
||||||
// Work on getting the email address for this LDAP user
|
|
||||||
var email = null;
|
|
||||||
if (domain.ldapuseremail) { email = xxuser[domain.ldapuseremail]; } else if (xxuser.mail) { email = xxuser.mail; } // Use given feild name or default
|
|
||||||
if ('[object Array]' == Object.prototype.toString.call(email)) { email = email[0]; } // Mail may be multivalued in LDAP in which case, answer is an array. Use the 1st value.
|
|
||||||
if (email) { email = email.toLowerCase(); } // it seems some code elsewhere also lowercase the emailaddress, so let's be consistant.
|
|
||||||
|
|
||||||
// Work on getting the real name for this LDAP user
|
|
||||||
var realname = null;
|
|
||||||
if (domain.ldapuserrealname) { realname = xxuser[domain.ldapuserrealname]; }
|
|
||||||
|
|
||||||
// Work on getting the real name for this LDAP user
|
|
||||||
var phonenumber = null;
|
|
||||||
if (domain.ldapuserphonenumber) { phonenumber = xxuser[domain.ldapuserphonenumber]; }
|
|
||||||
|
|
||||||
// Check if the user already exists
|
|
||||||
var user = obj.users[userid];
|
|
||||||
if (user == null) {
|
|
||||||
// This user does not exist, create a new account.
|
|
||||||
var user = { type: 'user', _id: userid, name: username, creation: Math.floor(Date.now() / 1000), login: Math.floor(Date.now() / 1000), access: Math.floor(Date.now() / 1000), domain: domain.id };
|
|
||||||
if (email) { user['email'] = email; user['emailVerified'] = true; }
|
|
||||||
if (domain.newaccountsrights) { user.siteadmin = domain.newaccountsrights; }
|
|
||||||
if (obj.common.validateStrArray(domain.newaccountrealms)) { user.groups = domain.newaccountrealms; }
|
|
||||||
var usercount = 0;
|
|
||||||
for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
|
|
||||||
if (usercount == 0) { user.siteadmin = 4294967295; /*if (domain.newaccounts === 2) { delete domain.newaccounts; }*/ } // If this is the first user, give the account site admin.
|
|
||||||
|
|
||||||
// Auto-join any user groups
|
|
||||||
if (typeof domain.newaccountsusergroups == 'object') {
|
|
||||||
for (var i in domain.newaccountsusergroups) {
|
|
||||||
var ugrpid = domain.newaccountsusergroups[i];
|
|
||||||
if (ugrpid.indexOf('/') < 0) { ugrpid = 'ugrp/' + domain.id + '/' + ugrpid; }
|
|
||||||
var ugroup = obj.userGroups[ugrpid];
|
|
||||||
if (ugroup != null) {
|
|
||||||
// Add group to the user
|
|
||||||
if (user.links == null) { user.links = {}; }
|
|
||||||
user.links[ugroup._id] = { rights: 1 };
|
|
||||||
|
|
||||||
// Add user to the group
|
|
||||||
ugroup.links[user._id] = { userid: user._id, name: user.name, rights: 1 };
|
|
||||||
db.Set(ugroup);
|
|
||||||
|
|
||||||
// Notify user group change
|
|
||||||
var event = { etype: 'ugrp', ugrpid: ugroup._id, name: ugroup.name, desc: ugroup.desc, action: 'usergroupchange', links: ugroup.links, msgid: 71, msgArgs: [user.name, ugroup.name], msg: 'Added user ' + user.name + ' to user group ' + ugroup.name, addUserDomain: domain.id };
|
|
||||||
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user group. Another event will come.
|
|
||||||
parent.DispatchEvent(['*', ugroup._id, user._id], obj, event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
obj.users[user._id] = user;
|
|
||||||
obj.db.SetUser(user);
|
|
||||||
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountcreate', msgid: 128, msgArgs: [user.name], msg: 'Account created, name is ' + user.name, domain: domain.id };
|
|
||||||
if (obj.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to create the user. Another event will come.
|
|
||||||
obj.parent.DispatchEvent(['*', 'server-users'], obj, event);
|
|
||||||
return fn(null, user._id);
|
|
||||||
} else {
|
|
||||||
// This is an existing user
|
|
||||||
// If the display username has changes, update it.
|
|
||||||
if (user.name != username) {
|
|
||||||
user.name = username;
|
|
||||||
obj.db.SetUser(user);
|
|
||||||
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msgid: 127, msgArgs: [user.name], msg: 'Changed account display name to ' + user.name, domain: domain.id };
|
|
||||||
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.
|
|
||||||
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
|
|
||||||
}
|
|
||||||
// Check if user email has changed
|
|
||||||
var emailreason = null;
|
|
||||||
if (user.email && !email) { // email unset in ldap => unset
|
|
||||||
delete user.email;
|
|
||||||
delete user.emailVerified;
|
|
||||||
emailreason = 'Unset email (no more email in LDAP)'
|
|
||||||
} else if (user.email != email) { // update email
|
|
||||||
user['email'] = email;
|
|
||||||
user['emailVerified'] = true;
|
|
||||||
emailreason = 'Set account email to ' + email + '. Sync with LDAP.';
|
|
||||||
}
|
|
||||||
if (emailreason) {
|
|
||||||
obj.db.SetUser(user);
|
|
||||||
var event = { etype: 'user', userid: user._id, username: user.name, account: obj.CloneSafeUser(user), action: 'accountchange', msg: emailreason, domain: domain.id };
|
|
||||||
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.
|
|
||||||
parent.DispatchEvent(['*', 'server-users', user._id], obj, event);
|
|
||||||
}
|
|
||||||
// If user is locker out, block here.
|
|
||||||
if ((user.siteadmin) && (user.siteadmin != 0xFFFFFFFF) && (user.siteadmin & 32) != 0) { fn('locked'); return; }
|
|
||||||
return fn(null, user._id);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
ldap.authenticate(name, pass, ldapHandler);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Regular login
|
// Regular login
|
||||||
|
Loading…
Reference in New Issue
Block a user