Skip to content

Commit

Permalink
[FIX] Avoid last admin deactivate itself (#22949)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostjen authored Oct 18, 2021
1 parent 877be6f commit 65cc04e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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',
});
}

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

0 comments on commit 65cc04e

Please sign in to comment.