Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apps/meteor/app/authorization/client/hasPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 11 additions & 12 deletions apps/meteor/app/models/client/models/Roles.ts
Original file line number Diff line number Diff line change
@@ -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<IRole> {
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<IRole>(),
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) {
Expand All @@ -25,8 +27,5 @@ class RolesCollection extends MinimongoCollection<IRole> {
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();
},
};
6 changes: 2 additions & 4 deletions apps/meteor/client/startup/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ 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';

const events: Record<string, ((role: IRole & { type?: ClientAction }) => 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);
},
};

Expand Down
Loading