Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] CAS Login not working with renamed users #12860

Merged
merged 2 commits into from Dec 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/rocketchat-cas/server/cas_server.js
Expand Up @@ -160,19 +160,35 @@ Accounts.registerLoginHandler(function(options) {
_.each(attr_map, function(source, int_name) {
// Source is our String to interpolate
if (_.isString(source)) {
let replacedValue = source;
_.each(ext_attrs, function(value, ext_name) {
source = source.replace(`%${ ext_name }%`, ext_attrs[ext_name]);
replacedValue = replacedValue.replace(`%${ ext_name }%`, ext_attrs[ext_name]);
});

int_attrs[int_name] = source;
logger.debug(`Sourced internal attribute: ${ int_name } = ${ source }`);
if (source !== replacedValue) {
int_attrs[int_name] = replacedValue;
logger.debug(`Sourced internal attribute: ${ int_name } = ${ replacedValue }`);
} else {
logger.debug(`Sourced internal attribute: ${ int_name } skipped.`);
}
}
});
}

// Search existing user by its external service id
logger.debug(`Looking up user by id: ${ result.username }`);
// First, look for a user that has logged in from CAS with this username before
let user = Meteor.users.findOne({ 'services.cas.external_id': result.username });
if (!user) {
// If that user was not found, check if there's any CAS user that is currently using that username on Rocket.Chat
// With this, CAS login will continue to work if the user is renamed on both sides and also if the user is renamed only on Rocket.Chat.
const username = new RegExp(`^${ result.username }$`, 'i');
user = Meteor.users.findOne({ 'services.cas.external_id': { $exists: true }, username });
if (user) {
// Update the user's external_id to reflect this new username.
Meteor.users.update(user, { $set: { 'services.cas.external_id': result.username } });
}
}

if (user) {
logger.debug(`Using existing user for '${ result.username }' with id: ${ user._id }`);
Expand Down