-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move add and remove role code out of Roles model (#34488)
- Loading branch information
1 parent
b62b0a6
commit d37433b
Showing
11 changed files
with
78 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,45 @@ | ||
import { MeteorError } from '@rocket.chat/core-services'; | ||
import type { IRole, IUser, IRoom } from '@rocket.chat/core-typings'; | ||
import { Roles } from '@rocket.chat/models'; | ||
import { Roles, Subscriptions, Users } from '@rocket.chat/models'; | ||
|
||
import { validateRoleList } from './validateRoleList'; | ||
import { notifyOnSubscriptionChangedByRoomIdAndUserId } from '../../../app/lib/server/lib/notifyListener'; | ||
|
||
export const addUserRolesAsync = async (userId: IUser['_id'], roleIds: IRole['_id'][], scope?: IRoom['_id']): Promise<boolean> => { | ||
if (!userId || !roleIds?.length) { | ||
export const addUserRolesAsync = async (userId: IUser['_id'], roles: IRole['_id'][], scope?: IRoom['_id']): Promise<boolean> => { | ||
if (!userId || !roles?.length) { | ||
return false; | ||
} | ||
|
||
if (!(await validateRoleList(roleIds))) { | ||
if (!(await validateRoleList(roles))) { | ||
throw new MeteorError('error-invalid-role', 'Invalid role'); | ||
} | ||
|
||
await Roles.addUserRoles(userId, roleIds, scope); | ||
if (process.env.NODE_ENV === 'development' && (scope === 'Users' || scope === 'Subscriptions')) { | ||
throw new Error('Roles.addUserRoles method received a role scope instead of a scope value.'); | ||
} | ||
|
||
if (!Array.isArray(roles)) { | ||
roles = [roles]; | ||
process.env.NODE_ENV === 'development' && console.warn('[WARN] RolesRaw.addUserRoles: roles should be an array'); | ||
} | ||
|
||
for await (const roleId of roles) { | ||
const role = await Roles.findOneById<Pick<IRole, '_id' | 'scope'>>(roleId, { projection: { scope: 1 } }); | ||
|
||
if (!role) { | ||
process.env.NODE_ENV === 'development' && console.warn(`[WARN] RolesRaw.addUserRoles: role: ${roleId} not found`); | ||
continue; | ||
} | ||
|
||
if (role.scope === 'Subscriptions' && scope) { | ||
const addRolesResponse = await Subscriptions.addRolesByUserId(userId, [role._id], scope); | ||
if (addRolesResponse.modifiedCount) { | ||
void notifyOnSubscriptionChangedByRoomIdAndUserId(scope, userId); | ||
} | ||
} else { | ||
await Users.addRolesByUserId(userId, [role._id]); | ||
} | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters