-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathpermissions.helper.ts
More file actions
122 lines (101 loc) · 3.7 KB
/
permissions.helper.ts
File metadata and controls
122 lines (101 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
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);
};