forked from nishant8BITS/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteUser.js
More file actions
39 lines (31 loc) · 950 Bytes
/
deleteUser.js
File metadata and controls
39 lines (31 loc) · 950 Bytes
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
Meteor.methods({
deleteUser(userId) {
check(userId, String);
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'deleteUser'
});
}
if (RocketChat.authz.hasPermission(Meteor.userId(), 'delete-user') !== true) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'deleteUser'
});
}
const user = RocketChat.models.Users.findOneById(userId);
if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'deleteUser'
});
}
const adminCount = Meteor.users.find({roles: 'admin'}).count();
const userIsAdmin = user.roles.indexOf('admin') > -1;
if (adminCount === 1 && userIsAdmin) {
throw new Meteor.Error('error-action-not-allowed', 'Leaving the app without admins is not allowed', {
method: 'deleteUser',
action: 'Remove_last_admin'
});
}
RocketChat.deleteUser(userId);
return true;
}
});