feat: update name references on oauth login (#37954)
Some checks failed
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (${{ matrix.type }}) (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (${{ matrix.type }}) (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled

This commit is contained in:
Pierre Lehnen 2025-12-26 10:05:47 -03:00 committed by GitHub
parent 5fa150953b
commit c8050708a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 8 deletions

View File

@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': minor
---
Changes OAuth login process to update users' names throughout the whole workspace when an existing user logs in with a changed name

View File

@ -11,7 +11,9 @@ import _ from 'underscore';
import { normalizers, fromTemplate, renameInvalidProperties } from './transform_helpers'; import { normalizers, fromTemplate, renameInvalidProperties } from './transform_helpers';
import { isURL } from '../../../lib/utils/isURL'; import { isURL } from '../../../lib/utils/isURL';
import { client } from '../../../server/database/utils';
import { callbacks } from '../../../server/lib/callbacks'; import { callbacks } from '../../../server/lib/callbacks';
import { saveUserIdentity } from '../../lib/server/functions/saveUserIdentity';
import { notifyOnUserChange } from '../../lib/server/lib/notifyListener'; import { notifyOnUserChange } from '../../lib/server/lib/notifyListener';
import { registerAccessTokenService } from '../../lib/server/oauth/oauth'; import { registerAccessTokenService } from '../../lib/server/oauth/oauth';
import { settings } from '../../settings/server'; import { settings } from '../../settings/server';
@ -366,17 +368,55 @@ export class CustomOAuth {
} }
const serviceIdKey = `services.${serviceName}.id`; const serviceIdKey = `services.${serviceName}.id`;
const update = { const successCallbacks = [
$set: { async () => {
name: serviceData.name, const updatedUser = await Users.findOneById(user._id, { projection: { name: 1, emails: 1, [serviceIdKey]: 1 } });
...(this.keyField === 'username' && serviceData.email && { emails: [{ address: serviceData.email, verified: true }] }), if (updatedUser) {
[serviceIdKey]: serviceData.id, const { _id, ...diff } = updatedUser;
void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff });
}
}, },
}; ];
await Users.update({ _id: user._id }, update); const session = client.startSession();
try {
// Extend the session to match the ExtendedSession type expected by saveUserIdentity
Object.assign(session, {
onceSuccesfulCommit: (cb) => {
successCallbacks.push(cb);
},
});
void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff: update }); session.startTransaction();
const updater = Users.getUpdater();
if (this.keyField === 'username' && serviceData.email) {
updater.set('emails', [{ address: serviceData.email, verified: true }]);
}
updater.set(serviceIdKey, serviceData.id);
await saveUserIdentity({
_id: user._id,
name: serviceData.name,
updater,
session,
updateUsernameInBackground: true,
// Username needs to be included otherwise the name won't be updated in some collections
username: user.username,
});
await Users.updateFromUpdater({ _id: user._id }, updater, { session });
await session.commitTransaction();
} catch (e) {
await session.abortTransaction();
throw e;
} finally {
await session.endSession();
}
void Promise.allSettled(successCallbacks.map((cb) => cb()));
} }
}); });