Skip to content
Snippets Groups Projects
Commit efe01c6e authored by George Secrieru's avatar George Secrieru
Browse files

Refactoring - added support for dynamic field mapping

parent 2a3bb638
No related merge requests found
......@@ -186,6 +186,7 @@
"LDAP_Enable" : "Enable LDAP",
"LDAP_Port" : "LDAP Port",
"LDAP_Sync_User_Data" : "Keep user data in sync with server",
"LDAP_Sync_User_Data_FieldMap" : "User data field map",
"LDAP_Url" : "LDAP URL",
"Leave_room" : "Leave room",
"line" : "line",
......
......@@ -172,6 +172,7 @@
"LDAP_Dn" : "DN LDAP",
"LDAP_Port" : "Porta LDAP",
"LDAP_Sync_User_Data" : "Manter dados dos usuários sincronizados",
"LDAP_Sync_User_Data_FieldMap" : "Mapeamento de campos do usuário",
"LDAP_Url" : "URL LDAP",
"Leave_room" : "Sair da sala",
"line" : "linha",
......
......@@ -3,7 +3,7 @@ Future = Npm.require('fibers/future');
var slug = function (text) {
text = slugify(text, '.');
return text.replace(/[^0-9a-z-_.]/g, '');
}
};
// At a minimum, set up LDAP_DEFAULTS.url and .dn according to
// your needs. url should appear as "ldap://your.url.here"
......@@ -86,7 +86,7 @@ LDAP.prototype.ldapCheck = function(options) {
var bind = function(dn) {
dn = dn.replace(/#{username}/g, options.username);
console.log('Attempt to bind', dn)
console.log('Attempt to bind', dn);
//Attempt to bind to ldap server with provided info
client.bind(dn, options.ldapPass, function(err) {
try {
......@@ -127,7 +127,7 @@ LDAP.prototype.ldapCheck = function(options) {
});
}
});
}
};
if (LDAP_DEFAULTS.bindSearch && LDAP_DEFAULTS.bindSearch.trim() != '') {
try {
......@@ -290,29 +290,49 @@ Accounts.registerLoginHandler("ldap", function(loginRequest) {
throw new Meteor.Error("LDAP-login-error", "LDAP Authentication succeded, but no user exists in Mongo. Either create a user for this email or set LDAP_DEFAULTS.createNewUser to true");
}
if (userId && RocketChat.settings.get('LDAP_Sync_User_Data')) {
// LDAP sync data logic
syncUserData = RocketChat.settings.get('LDAP_Sync_User_Data');
syncUserDataFieldMap = RocketChat.settings.get('LDAP_Sync_User_Data_FieldMap').trim();
if (userId && syncUserData && syncUserDataFieldMap) {
userData = {};
if (ldapResponse.searchResults.hasOwnProperty('mail')) {
fieldMap = JSON.parse(syncUserDataFieldMap);
if ('object' == typeof ldapResponse.searchResults.mail) {
userData.emails = _.map(ldapResponse.searchResults.mail, function (item) {
return { address: item, verified: true};
});
} else {
userData.emails = [{
address: ldapResponse.searchResults.mail,
verified: true
}];
emailList = [];
_.map(fieldMap, function(userField, ldapField) {
if (!ldapResponse.searchResults.hasOwnProperty(ldapField)) {
return;
}
}
if (ldapResponse.searchResults.hasOwnProperty('name')) {
userData.name = ldapResponse.searchResults.givenName;
// restrict field mapping to a known list of fields
switch (userField) {
case 'email':
if ('object' == typeof ldapResponse.searchResults[ldapField]) {
_.map(ldapResponse.searchResults[ldapField], function (item) {
emailList.push({ address: item, verified: true });
});
} else {
emailList.push({ address: ldapResponse.searchResults[ldapField], verified: true });
}
break;
case 'name':
userData.name = ldapResponse.searchResults[ldapField];
break;
default:
break;
}
});
if (emailList.length) {
userData.emails = emailList;
}
if (_.size(userData)) {
Meteor.users.update(userId, { $set: userData });
}
}
return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment