-
Notifications
You must be signed in to change notification settings - Fork 10k
/
setAdminStatus.ts
32 lines (25 loc) · 1.05 KB
/
setAdminStatus.ts
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
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { isUserFederated } from '@rocket.chat/core-typings';
import { hasPermission } from '../../../authorization/server';
Meteor.methods({
setAdminStatus(userId, admin) {
check(userId, String);
check(admin, Match.Optional(Boolean));
const uid = Meteor.userId();
if (!uid) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setAdminStatus' });
}
if (hasPermission(uid, 'assign-admin-role') !== true) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setAdminStatus' });
}
const user = Meteor.users.findOne({ _id: userId }, { fields: { username: 1, federated: 1 } });
if (!user || isUserFederated(user)) {
throw new Meteor.Error('error-not-allowed', 'Federated Users cant be admins', { method: 'setAdminStatus' });
}
if (admin) {
return Meteor.call('authorization:addUserToRole', 'admin', user?.username);
}
return Meteor.call('authorization:removeUserFromRole', 'admin', user?.username);
},
});