The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/models/server/raw/Permissions.ts

42 lines
1.3 KiB

import { BaseRaw } from './BaseRaw';
import { IPermission } from '../../../../definition/IPermission';
export class PermissionsRaw extends BaseRaw<IPermission> {
async createOrUpdate(name: string, roles: string[]): Promise<IPermission['_id']> {
const exists = await this.findOne<Pick<IPermission, '_id'>>(
{
_id: name,
roles,
},
{ fields: { _id: 1 } },
);
if (exists) {
return exists._id;
}
return this.update({ _id: name }, { $set: { roles } }, { upsert: true }).then((result) => result.result._id);
}
async create(id: string, roles: string[]): Promise<IPermission['_id']> {
const exists = await this.findOneById<Pick<IPermission, '_id'>>(id, { fields: { _id: 1 } });
if (exists) {
return exists._id;
}
return this.update({ _id: id }, { $set: { roles } }, { upsert: true }).then((result) => result.result._id);
}
async addRole(permission: string, role: string): Promise<void> {
await this.update({ _id: permission, roles: { $ne: role } }, { $addToSet: { roles: role } });
}
async setRoles(permission: string, roles: string[]): Promise<void> {
await this.update({ _id: permission }, { $set: { roles } });
}
async removeRole(permission: string, role: string): Promise<void> {
await this.update({ _id: permission, roles: role }, { $pull: { roles: role } });
}
}