From 8f6c3d2fe04cf0e1ea224e454e41aa3d8dcd0141 Mon Sep 17 00:00:00 2001 From: Marco Descher Date: Wed, 8 Jan 2020 12:38:21 +0100 Subject: [PATCH] Synchronize saml roles to local user --- app/meteor-accounts-saml/server/saml_rocketchat.js | 10 ++++++++++ app/meteor-accounts-saml/server/saml_server.js | 12 ++++++++---- packages/rocketchat-i18n/i18n/de.i18n.json | 2 ++ packages/rocketchat-i18n/i18n/en.i18n.json | 2 ++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/meteor-accounts-saml/server/saml_rocketchat.js b/app/meteor-accounts-saml/server/saml_rocketchat.js index ba28f02a1139a..3cd1654e7192a 100644 --- a/app/meteor-accounts-saml/server/saml_rocketchat.js +++ b/app/meteor-accounts-saml/server/saml_rocketchat.js @@ -182,6 +182,13 @@ Meteor.methods({ i18nLabel: 'SAML_Role_Attribute_Name', i18nDescription: 'SAML_Role_Attribute_Name_Description', }); + settings.add(`SAML_Custom_${ name }_role_attribute_sync`, false, { + type: 'boolean', + group: 'SAML', + section: name, + i18nLabel: 'SAML_Role_Attribute_Sync', + i18nDescription: 'SAML_Role_Attribute_Sync_Description', + }); }, }); @@ -215,6 +222,7 @@ const getSamlConfigs = function(service) { authnContextComparison: settings.get(`${ service.key }_authn_context_comparison`), defaultUserRole: settings.get(`${ service.key }_default_user_role`), roleAttributeName: settings.get(`${ service.key }_role_attribute_name`), + roleAttributeSync: settings.get(`${ service.key }_role_attribute_sync`), secret: { privateKey: settings.get(`${ service.key }_private_key`), publicCert: settings.get(`${ service.key }_public_cert`), @@ -256,6 +264,7 @@ const configureSamlService = function(samlConfigs) { Accounts.saml.settings.debug = samlConfigs.debug; Accounts.saml.settings.defaultUserRole = samlConfigs.defaultUserRole; Accounts.saml.settings.roleAttributeName = samlConfigs.roleAttributeName; + Accounts.saml.settings.roleAttributeSync = samlConfigs.roleAttributeSync; return { provider: samlConfigs.clientConfig.provider, @@ -269,6 +278,7 @@ const configureSamlService = function(samlConfigs) { authnContextComparison: samlConfigs.authnContextComparison, defaultUserRole: samlConfigs.defaultUserRole, roleAttributeName: samlConfigs.roleAttributeName, + roleAttributeSync: samlConfigs.roleAttributeSync, }; }; diff --git a/app/meteor-accounts-saml/server/saml_server.js b/app/meteor-accounts-saml/server/saml_server.js index 23801cbcf0f54..35e07d0fd07d3 100644 --- a/app/meteor-accounts-saml/server/saml_server.js +++ b/app/meteor-accounts-saml/server/saml_server.js @@ -33,7 +33,7 @@ RoutePolicy.declare('/_saml/', 'network'); * Fetch SAML provider configs for given 'provider'. */ function getSamlProviderConfig(provider) { - if (! provider) { + if (!provider) { throw new Meteor.Error('no-saml-provider', 'SAML internal error', { method: 'getSamlProviderConfig' }); @@ -57,7 +57,7 @@ Meteor.methods({ } // This query should respect upcoming array of SAML logins const user = Users.getSAMLByIdAndSAMLProvider(Meteor.userId(), provider); - if (!user || !user.services || ! user.services.saml) { + if (!user || !user.services || !user.services.saml) { return; } @@ -245,7 +245,7 @@ Accounts.registerLoginHandler(function(loginRequest) { } const { emailField, usernameField, nameField, userDataFieldMap, regexes } = getUserDataMapping(); - const { defaultUserRole = 'user', roleAttributeName } = Accounts.saml.settings; + const { defaultUserRole = 'user', roleAttributeName, roleAttributeSync } = Accounts.saml.settings; if (loginResult && loginResult.profile && loginResult.profile[emailField]) { const emailList = Array.isArray(loginResult.profile[emailField]) ? loginResult.profile[emailField] : [loginResult.profile[emailField]]; @@ -384,6 +384,10 @@ Accounts.registerLoginHandler(function(loginRequest) { updateData.emails = emails; } + if (roleAttributeSync) { + updateData.roles = globalRoles; + } + Meteor.users.update({ _id: user._id, }, { @@ -432,7 +436,7 @@ Accounts.saml.subscribeToSAMLChannels = function(channels, user) { }); } } - } catch (err) { + } catch (err) { console.error(err); } }; diff --git a/packages/rocketchat-i18n/i18n/de.i18n.json b/packages/rocketchat-i18n/i18n/de.i18n.json index 2c2ed5c5fcb1a..694c5a39b2767 100644 --- a/packages/rocketchat-i18n/i18n/de.i18n.json +++ b/packages/rocketchat-i18n/i18n/de.i18n.json @@ -2756,6 +2756,8 @@ "SAML_Default_User_Role_Description": "Sie können mehrere Rollen angeben, indem Sie sie durch Kommas trennen.", "SAML_Role_Attribute_Name": "Rollenattributname", "SAML_Role_Attribute_Name_Description": "Wenn dieses Attribut in der SAML-Antwort gefunden wird, werden seine Werte als Rollennamen für neue Benutzer verwendet.", + "SAML_Role_Attribute_Sync": "Benutzerrollen synchronisieren", + "SAML_Role_Attribute_Sync_Description": "Synchronisiere Benutzerrollen beim Login (Überschreibt lokale Benutzerrollen).", "Saturday": "Samstag", "Save": "Speichern", "save-others-livechat-room-info": "Livechat-Informationen anderer Chats speichern", diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 38db8d3d282c7..4ea3cb81ed9e5 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -2789,6 +2789,8 @@ "SAML_Default_User_Role_Description": "You can specify multiple roles, separating them with commas.", "SAML_Role_Attribute_Name": "Role Attribute Name", "SAML_Role_Attribute_Name_Description": "If this attribute is found on the SAML response, it's values will be used as role names for new users.", + "SAML_Role_Attribute_Sync": "Sync User Roles", + "SAML_Role_Attribute_Sync_Description": "Sync SAML user roles on login (overwrites local user roles).", "Saturday": "Saturday", "Save": "Save", "save-others-livechat-room-info": "Save Others Livechat Room Info",