Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Avoid last admin deactivate itself #22949

Merged
merged 17 commits into from
Oct 18, 2021
20 changes: 19 additions & 1 deletion server/methods/setUserActiveStatus.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';

import { Users } from '../../app/models/server';
import { hasPermission } from '../../app/authorization';
import { setUserActiveStatus } from '../../app/lib/server/functions/setUserActiveStatus';

const getAdminCount = () => {
Meteor.users.find({
ostjen marked this conversation as resolved.
Show resolved Hide resolved
roles: {
$in: ['admin'],
},
active: true,
}).count();
};

Meteor.methods({
setUserActiveStatus(userId, active, confirmRelenquish) {
check(userId, String);
Expand All @@ -21,8 +31,16 @@ Meteor.methods({
});
}

setUserActiveStatus(userId, active, confirmRelenquish);
const userAdmin = Users.findOneAdmin(userId.count);
ostjen marked this conversation as resolved.
Show resolved Hide resolved

if (userAdmin && getAdminCount() === 1) {
throw new Meteor.Error('error-action-not-allowed', 'Leaving the app without an active admin is not allowed', {
method: 'removeUserFromRole',
action: 'Remove_last_admin',
});
}

setUserActiveStatus(userId, active, confirmRelenquish);
return true;
},
});