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
10 changes: 10 additions & 0 deletions app/lib/server/functions/setUserActiveStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ export function setUserActiveStatus(userId, active, confirmRelinquish = false) {
return false;
}


// Users without username can't do anything, so there is no need to check for owned rooms
if (user.username != null && !active) {
const userAdmin = Users.findOneAdmin(userId.count);
const adminsCount = Users.findActiveUsersInRoles(['admin']).count();
if (userAdmin && adminsCount === 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',
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add this check to removing role if role is admin. Some people seem to keep removing the last admin. 🙈


const subscribedRooms = getSubscribedRoomsForUserWithDetails(userId);
// give omnichannel rooms a special treatment :)
const chatSubscribedRooms = subscribedRooms.filter(({ t }) => t !== 'l');
Expand Down
11 changes: 11 additions & 0 deletions app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,17 @@ export class Users extends Base {
return this.find(query, options);
}

findActiveUsersInRoles(roles, scope, options) {
roles = [].concat(roles);

const query = {
roles: { $in: roles },
active: true,
};

return this.find(query, options);
}

findOneByAppId(appId, options) {
const query = { appId };

Expand Down