@@ -4,7 +4,6 @@ import { Subscriptions, Users } from '@rocket.chat/models';
44import type { Collection , FindCursor , Db , Filter , FindOptions , Document , CountDocumentsOptions } from 'mongodb' ;
55
66import { BaseRaw } from './BaseRaw' ;
7- import { notifyOnSubscriptionChangedByRoomIdAndUserId } from '../../../app/lib/server/lib/notifyListener' ;
87
98export class RolesRaw extends BaseRaw < IRole > implements IRolesModel {
109 constructor ( db : Db , trash ?: Collection < RocketChatRecordDeleted < IRole > > ) {
@@ -19,37 +18,6 @@ export class RolesRaw extends BaseRaw<IRole> implements IRolesModel {
1918 return options ? this . find ( query , options ) : this . find ( query ) ;
2019 }
2120
22- async addUserRoles ( userId : IUser [ '_id' ] , roles : IRole [ '_id' ] [ ] , scope ?: IRoom [ '_id' ] ) : Promise < boolean > {
23- if ( process . env . NODE_ENV === 'development' && ( scope === 'Users' || scope === 'Subscriptions' ) ) {
24- throw new Error ( 'Roles.addUserRoles method received a role scope instead of a scope value.' ) ;
25- }
26-
27- if ( ! Array . isArray ( roles ) ) {
28- roles = [ roles ] ;
29- process . env . NODE_ENV === 'development' && console . warn ( '[WARN] RolesRaw.addUserRoles: roles should be an array' ) ;
30- }
31-
32- for await ( const roleId of roles ) {
33- const role = await this . findOneById < Pick < IRole , '_id' | 'scope' > > ( roleId , { projection : { scope : 1 } } ) ;
34-
35- if ( ! role ) {
36- process . env . NODE_ENV === 'development' && console . warn ( `[WARN] RolesRaw.addUserRoles: role: ${ roleId } not found` ) ;
37- continue ;
38- }
39-
40- if ( role . scope === 'Subscriptions' && scope ) {
41- // TODO remove dependency from other models - this logic should be inside a function/service
42- const addRolesResponse = await Subscriptions . addRolesByUserId ( userId , [ role . _id ] , scope ) ;
43- if ( addRolesResponse . modifiedCount ) {
44- void notifyOnSubscriptionChangedByRoomIdAndUserId ( scope , userId ) ;
45- }
46- } else {
47- await Users . addRolesByUserId ( userId , [ role . _id ] ) ;
48- }
49- }
50- return true ;
51- }
52-
5321 async isUserInRoles ( userId : IUser [ '_id' ] , roles : IRole [ '_id' ] [ ] , scope ?: IRoom [ '_id' ] ) : Promise < boolean > {
5422 if ( process . env . NODE_ENV === 'development' && ( scope === 'Users' || scope === 'Subscriptions' ) ) {
5523 throw new Error ( 'Roles.isUserInRoles method received a role scope instead of a scope value.' ) ;
@@ -78,30 +46,6 @@ export class RolesRaw extends BaseRaw<IRole> implements IRolesModel {
7846 return false ;
7947 }
8048
81- async removeUserRoles ( userId : IUser [ '_id' ] , roles : IRole [ '_id' ] [ ] , scope ?: IRoom [ '_id' ] ) : Promise < boolean > {
82- if ( process . env . NODE_ENV === 'development' && ( scope === 'Users' || scope === 'Subscriptions' ) ) {
83- throw new Error ( 'Roles.removeUserRoles method received a role scope instead of a scope value.' ) ;
84- }
85-
86- for await ( const roleId of roles ) {
87- const role = await this . findOneById < Pick < IRole , '_id' | 'scope' > > ( roleId , { projection : { scope : 1 } } ) ;
88-
89- if ( ! role ) {
90- continue ;
91- }
92-
93- if ( role . scope === 'Subscriptions' && scope ) {
94- const removeRolesResponse = await Subscriptions . removeRolesByUserId ( userId , [ roleId ] , scope ) ;
95- if ( removeRolesResponse . modifiedCount ) {
96- void notifyOnSubscriptionChangedByRoomIdAndUserId ( scope , userId ) ;
97- }
98- } else {
99- await Users . removeRolesByUserId ( userId , [ roleId ] ) ;
100- }
101- }
102- return true ;
103- }
104-
10549 async findOneByIdOrName ( _idOrName : IRole [ '_id' ] | IRole [ 'name' ] , options ?: undefined ) : Promise < IRole | null > ;
10650
10751 async findOneByIdOrName ( _idOrName : IRole [ '_id' ] | IRole [ 'name' ] , options : FindOptions < IRole > ) : Promise < IRole | null > ;
0 commit comments