Skip to content

Commit

Permalink
[FIX] New users aren't mapped to rooms with OAuth groups/channels map (
Browse files Browse the repository at this point in the history
…#27000)

Co-authored-by: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com>
  • Loading branch information
2 people authored and sampaiodiego committed Jan 24, 2023
1 parent 1004bff commit 7861c1e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
18 changes: 11 additions & 7 deletions apps/meteor/app/custom-oauth/server/custom_oauth_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,6 @@ export class CustomOAuth {
user.name = user.services[this.name].name;
}

callbacks.run('afterValidateNewOAuthUser', {
identity: user.services[this.name],
serviceName: this.name,
user,
});

return true;
});
}
Expand Down Expand Up @@ -440,5 +434,15 @@ Accounts.updateOrCreateUserFromExternalService = function (...args /* serviceNam
hook.apply(this, args);
}

return updateOrCreateUserFromExternalService.apply(this, args);
const [serviceName, serviceData] = args;

const user = updateOrCreateUserFromExternalService.apply(this, args);

callbacks.run('afterValidateNewOAuthUser', {
identity: serviceData,
serviceName,
user: Users.findOneById(user.userId),
});

return user;
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export class FederationHooks {
public static canAddFederatedUserToNonFederatedRoom(callback: (user: IUser | string, room: IRoom) => Promise<void>): void {
callbacks.add(
'federation.beforeAddUserAToRoom',
(params: { user: IUser | string }, room: IRoom): void => {
(params: { user: IUser | string; inviter?: IUser }, room: IRoom): void => {
if (!params || !params.user || !room) {
return;
}

Promise.await(callback(params.user, room));
},
callbacks.priority.HIGH,
Expand All @@ -47,6 +51,10 @@ export class FederationHooks {
callbacks.add(
'federation.beforeAddUserAToRoom',
(params: { user: IUser | string; inviter: IUser }, room: IRoom): void => {
if (!params || !params.user || !room) {
return;
}

Promise.await(callback(params.user, params.inviter, room));
},
callbacks.priority.HIGH,
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/ee/server/configuration/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { capitalize } from '@rocket.chat/string-helpers';
import type { IUser } from '@rocket.chat/core-typings';

import { OAuthEEManager } from '../lib/oauth/Manager';
import { onLicense } from '../../app/license/server';
Expand All @@ -9,13 +10,13 @@ import { Logger } from '../../../app/logger/server';
interface IOAuthUserService {
serviceName: string;
serviceData: Record<string, any>;
user: Record<string, any>;
user: IUser;
}

interface IOAuthUserIdentity {
serviceName: string;
identity: Record<string, any>;
user: Record<string, any>;
user: IUser;
}

interface IOAuthSettings {
Expand All @@ -38,7 +39,7 @@ function getOAuthSettings(serviceName: string): IOAuthSettings {
rolesClaim: settings.get(`Accounts_OAuth_Custom-${serviceName}-roles_claim`) as string,
groupsClaim: settings.get(`Accounts_OAuth_Custom-${serviceName}-groups_claim`) as string,
channelsAdmin: settings.get(`Accounts_OAuth_Custom-${serviceName}-channels_admin`) as string,
channelsMap: settings.get(`Accounts_OAuth_Custom-${serviceName}-channels_map`) as string,
channelsMap: settings.get(`Accounts_OAuth_Custom-${serviceName}-groups_channel_map`) as string,
};
}

Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/ee/server/lib/oauth/Manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Roles } from '@rocket.chat/models';
import type { IUser } from '@rocket.chat/core-typings';

import { Rooms } from '../../../../app/models/server';
import { addUserToRoom, createRoom } from '../../../../app/lib/server/functions';
Expand All @@ -9,7 +10,7 @@ export const logger = new Logger('OAuth');

export class OAuthEEManager {
static mapSSOGroupsToChannels(
user: Record<string, any>,
user: IUser,
identity: Record<string, any>,
groupClaimName: string,
channelsMap: Record<string, any> | undefined,
Expand All @@ -34,7 +35,7 @@ export class OAuthEEManager {
}
}
if (Array.isArray(groupsFromSSO) && groupsFromSSO.includes(ssoGroup)) {
addUserToRoom(room._id, user.username);
addUserToRoom(room._id, user);
}
}
}
Expand Down

0 comments on commit 7861c1e

Please sign in to comment.