mirror of
https://github.com/RocketChat/Rocket.Chat.git
synced 2025-12-28 06:47:25 +00:00
Some checks are pending
Deploy GitHub Pages / deploy-preview (push) Waiting to run
CI / ⚙️ Variables Setup (push) Waiting to run
CI / 🚀 Notify external services - draft (push) Blocked by required conditions
CI / 📦 Build Packages (push) Blocked by required conditions
CI / deploy-preview (push) Blocked by required conditions
CI / 📦 Meteor Build - coverage (push) Blocked by required conditions
CI / 📦 Meteor Build - official (push) Blocked by required conditions
CI / Builds matrix rust bindings against alpine (push) Waiting to run
CI / 🚢 Build Docker Images for Testing (alpine) (push) Blocked by required conditions
CI / 🚢 Build Docker Images for Production (alpine) (push) Blocked by required conditions
CI / 🔎 Code Check (push) Blocked by required conditions
CI / 🔨 Test Unit (push) Blocked by required conditions
CI / 🔨 Test API (CE) (push) Blocked by required conditions
CI / 🔨 Test UI (CE) (push) Blocked by required conditions
CI / 🔨 Test API (EE) (push) Blocked by required conditions
CI / 🔨 Test UI (EE) (push) Blocked by required conditions
CI / ✅ Tests Done (push) Blocked by required conditions
CI / 🚀 Publish build assets (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (main) (alpine) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (account) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (authorization) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (ddp-streamer) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (omnichannel-transcript) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (presence) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (queue-worker) (push) Blocked by required conditions
CI / 🚀 Publish Docker Image (services) (stream-hub) (push) Blocked by required conditions
CI / 🚀 Notify external services (push) Blocked by required conditions
CI / trigger-dependent-workflows (push) Blocked by required conditions
CI / Update Version Durability (push) Blocked by required conditions
Code scanning - action / CodeQL-Build (push) Waiting to run
116 lines
3.7 KiB
TypeScript
116 lines
3.7 KiB
TypeScript
import type { ISetting } from '@rocket.chat/core-typings';
|
|
|
|
import { api, credentials, request } from './api-data';
|
|
import { permissions } from '../../app/authorization/server/constant/permissions';
|
|
import { omnichannelEEPermissions } from '../../ee/app/livechat-enterprise/server/permissions';
|
|
import { IS_EE } from '../e2e/config/constants';
|
|
|
|
export const updatePermission = (permission: string, roles: string[]): Promise<void | Error> =>
|
|
new Promise((resolve, reject) => {
|
|
void request
|
|
.post(api('permissions.update'))
|
|
.set(credentials)
|
|
.send({ permissions: [{ _id: permission, roles }] })
|
|
.expect('Content-Type', 'application/json')
|
|
.expect(200)
|
|
.end((err?: Error) => setTimeout(() => (!err && resolve()) || reject(err), 100));
|
|
});
|
|
|
|
export const updateEEPermission = (permission: string, roles: string[]): Promise<void | Error> =>
|
|
IS_EE ? updatePermission(permission, roles) : Promise.resolve();
|
|
|
|
const updateManyPermissions = (permissions: { [key: string]: string[] }): Promise<void | Error> =>
|
|
new Promise((resolve, reject) => {
|
|
void request
|
|
.post(api('permissions.update'))
|
|
.set(credentials)
|
|
.send({ permissions: Object.keys(permissions).map((k) => ({ _id: k, roles: permissions[k] })) })
|
|
.expect('Content-Type', 'application/json')
|
|
.expect(200)
|
|
.end((err?: Error) => setTimeout(() => (!err && resolve()) || reject(err), 100));
|
|
});
|
|
|
|
export const updateSetting = (setting: string, value: ISetting['value'], debounce = true): Promise<void | Error> =>
|
|
new Promise((resolve, reject) => {
|
|
void request
|
|
.post(`/api/v1/settings/${setting}`)
|
|
.set(credentials)
|
|
.send({ value })
|
|
.expect('Content-Type', 'application/json')
|
|
.expect(200)
|
|
.end((err?: Error) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
if (debounce) {
|
|
setTimeout(resolve, 100);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
});
|
|
|
|
export const getSettingValueById = async (setting: string): Promise<ISetting['value']> => {
|
|
const response = await request.get(`/api/v1/settings/${setting}`).set(credentials).expect('Content-Type', 'application/json').expect(200);
|
|
|
|
return response.body.value;
|
|
};
|
|
|
|
export const updateEESetting = (setting: string, value: ISetting['value']): Promise<void | Error> =>
|
|
IS_EE
|
|
? new Promise((resolve, reject) => {
|
|
void request
|
|
.post(`/api/v1/settings/${setting}`)
|
|
.set(credentials)
|
|
.send({ value })
|
|
.expect('Content-Type', 'application/json')
|
|
.expect(200)
|
|
.end((err?: Error) => setTimeout(() => (!err && resolve()) || reject(err), 100));
|
|
})
|
|
: Promise.resolve();
|
|
|
|
export const removePermissions = async (perms: string[]) => {
|
|
await updateManyPermissions(Object.fromEntries(perms.map((name) => [name, []])));
|
|
};
|
|
|
|
export const addPermissions = async (perms: { [key: string]: string[] }) => {
|
|
await updateManyPermissions(perms);
|
|
};
|
|
|
|
type Permission = (typeof permissions)[number]['_id'];
|
|
|
|
export const removePermissionFromAllRoles = async (permission: Permission) => {
|
|
await updatePermission(permission, []);
|
|
};
|
|
|
|
const getPermissions = () => {
|
|
if (!IS_EE) {
|
|
return permissions;
|
|
}
|
|
|
|
return [...permissions, ...omnichannelEEPermissions];
|
|
};
|
|
|
|
export const restorePermissionToRoles = async (permission: Permission) => {
|
|
const defaultPermission = getPermissions().find((p) => p._id === permission);
|
|
if (!defaultPermission) {
|
|
throw new Error(`No default roles found for permission ${permission}`);
|
|
}
|
|
|
|
const mutableDefaultRoles: string[] = defaultPermission.roles.map((r) => r);
|
|
|
|
if (!IS_EE) {
|
|
const eeOnlyRoles = ['livechat-monitor'];
|
|
eeOnlyRoles.forEach((role) => {
|
|
const index = mutableDefaultRoles.indexOf(role);
|
|
if (index !== -1) {
|
|
mutableDefaultRoles.splice(index, 1);
|
|
}
|
|
});
|
|
}
|
|
|
|
await updatePermission(permission, mutableDefaultRoles);
|
|
};
|