diff --git a/apps/meteor/app/authorization/client/hasPermission.ts b/apps/meteor/app/authorization/client/hasPermission.ts index 4c48ead3bd1fb..9b87ce363a43f 100644 --- a/apps/meteor/app/authorization/client/hasPermission.ts +++ b/apps/meteor/app/authorization/client/hasPermission.ts @@ -32,8 +32,7 @@ const createPermissionValidator = const roles = permission?.roles ?? []; return roles.some((roleId) => { - const role = Models.Roles.findOne(roleId, { fields: { scope: 1 } }); - const roleScope = role?.scope; + const roleScope = Models.Roles.state.get(roleId)?.scope; if (!isValidScope(roleScope)) { return false; diff --git a/apps/meteor/app/models/client/models/Roles.ts b/apps/meteor/app/models/client/models/Roles.ts index aeeb5a5c7eb70..ae9f84fc9a909 100644 --- a/apps/meteor/app/models/client/models/Roles.ts +++ b/apps/meteor/app/models/client/models/Roles.ts @@ -1,17 +1,19 @@ import type { IRole, IUser } from '@rocket.chat/core-typings'; -import { ReactiveVar } from 'meteor/reactive-var'; import { Subscriptions } from './Subscriptions'; import { Users } from './Users'; -import { MinimongoCollection } from '../../../../client/lib/cachedCollections/MinimongoCollection'; +import { createDocumentMapStore } from '../../../../client/lib/cachedCollections/DocumentMapStore'; -class RolesCollection extends MinimongoCollection { - ready = new ReactiveVar(false); - - isUserInRoles(userId: IUser['_id'], roles: IRole['_id'][] | IRole['_id'], scope?: string, ignoreSubscriptions = false) { +/** @deprecated prefer fetching data from the REST API, listening to changes via streamer events, and storing the state in a Tanstack Query */ +export const Roles = { + use: createDocumentMapStore(), + get state() { + return Roles.use.getState(); + }, + isUserInRoles: (userId: IUser['_id'], roles: IRole['_id'][] | IRole['_id'], scope?: string, ignoreSubscriptions = false) => { roles = Array.isArray(roles) ? roles : [roles]; return roles.some((roleId) => { - const role = this.findOne(roleId); + const role = Roles.state.get(roleId); const roleScope = ignoreSubscriptions ? 'Users' : role?.scope || 'Users'; switch (roleScope) { @@ -25,8 +27,5 @@ class RolesCollection extends MinimongoCollection { return false; } }); - } -} - -/** @deprecated new code refer to Minimongo collections like this one; prefer fetching data from the REST API, listening to changes via streamer events, and storing the state in a Tanstack Query */ -export const Roles = new RolesCollection(); + }, +}; diff --git a/apps/meteor/client/startup/roles.ts b/apps/meteor/client/startup/roles.ts index 6ea36a3475f61..cd6a1e2be5e49 100644 --- a/apps/meteor/client/startup/roles.ts +++ b/apps/meteor/client/startup/roles.ts @@ -11,8 +11,6 @@ Meteor.startup(() => { const { roles } = await sdk.rest.get('/v1/roles.list'); // if a role is checked before this collection is populated, it will return undefined Roles.state.replaceAll(roles); - - Roles.ready.set(true); }); type ClientAction = 'inserted' | 'updated' | 'removed' | 'changed'; @@ -20,10 +18,10 @@ Meteor.startup(() => { const events: Record void) | undefined> = { changed: (role) => { delete role.type; - Roles.upsert({ _id: role._id }, role); + Roles.state.store(role); }, removed: (role) => { - Roles.remove({ _id: role._id }); + Roles.state.delete(role._id); }, };