diff --git a/app/cas/server/cas_server.js b/app/cas/server/cas_server.js index 2f5e24943d1..549c7cc9459 100644 --- a/app/cas/server/cas_server.js +++ b/app/cas/server/cas_server.js @@ -179,11 +179,12 @@ Accounts.registerLoginHandler(function(options) { // 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 + // If that user was not found, check if there's any Rocket.Chat user with that username // 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. + // It'll also allow non-CAS users to switch to CAS based login if (trustUsername) { const username = new RegExp(`^${ result.username }$`, 'i'); - user = Meteor.users.findOne({ 'services.cas.external_id': { $exists: true }, username }); + user = Meteor.users.findOne({ 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 } });