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/server/methods/deleteUser.js

52 lines
1.3 KiB

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Users } from '../../app/models';
import { hasPermission } from '../../app/authorization';
import { deleteUser } from '../../app/lib/server';
Meteor.methods({
deleteUser(userId, confirmRelinquish = false) {
check(userId, String);
if (!Meteor.userId()) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'deleteUser',
});
}
if (hasPermission(Meteor.userId(), 'delete-user') !== true) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
method: 'deleteUser',
});
}
const user = Users.findOneById(userId);
if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user to delete', {
method: 'deleteUser',
});
}
if (user.type === 'app') {
throw new Meteor.Error('error-cannot-delete-app-user', 'Deleting app user is not allowed', {
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',
});
}
deleteUser(userId, confirmRelinquish);
return true;
},
});