diff --git a/public/src/forum/admin/users.js b/public/src/forum/admin/users.js index ec6ecb5579..4bd33c8657 100644 --- a/public/src/forum/admin/users.js +++ b/public/src/forum/admin/users.js @@ -100,7 +100,7 @@ define('forum/admin/users', function() { return; } - if (uids.indexOf(yourid) !== -1) { + if (uids.indexOf(yourid.toString()) !== -1) { app.alertError('You can\'t remove yourself as Administrator!'); } else { bootbox.confirm('Do you really want to remove admins?', function(confirm) { diff --git a/src/groups.js b/src/groups.js index f50fbae6c7..a9aa6902a7 100644 --- a/src/groups.js +++ b/src/groups.js @@ -150,6 +150,10 @@ db.isSetMember('group:' + groupName + ':members', uid, callback); }; + Groups.getMemberCount = function(groupName, callback) { + db.setCount('group:' + groupName + ':members', callback); + }; + Groups.isMemberOfGroupList = function(uid, groupListKey, callback) { db.getSetMembers('group:' + groupListKey + ':members', function(err, groupNames) { groupNames = internals.removeEphemeralGroups(groupNames); diff --git a/src/socket.io/admin/user.js b/src/socket.io/admin/user.js index 768d451a58..bdd3d2566d 100644 --- a/src/socket.io/admin/user.js +++ b/src/socket.io/admin/user.js @@ -10,22 +10,38 @@ var groups = require('../../groups'), User.makeAdmins = function(socket, uids, callback) { - toggleAdmin(uids, true, callback); -}; + if(!Array.isArray(uids)) { + return callback(new Error('[[error:invalid-data]]')); + } -User.removeAdmins = function(socket, uids, callback) { - toggleAdmin(uids, false, callback); + async.each(uids, function(uid, next) { + groups.join('administrators', uid, next); + }, callback); }; -function toggleAdmin(uids, isAdmin, callback) { +User.removeAdmins = function(socket, uids, callback) { if(!Array.isArray(uids)) { return callback(new Error('[[error:invalid-data]]')); } - async.each(uids, function(uid, next) { - groups[isAdmin ? 'join' : 'leave']('administrators', uid, next); + if (uids.indexOf(socket.uid.toString()) !== -1) { + return callback(new Error('[[error:cant-remove-self-as-admin]]')); + } + + async.eachSeries(uids, function(uid, next) { + groups.getMemberCount('administrators', function(err, count) { + if (err) { + return next(err); + } + + if (count === 1) { + return next(new Error('[[error:cant-remove-last-admin]]')); + } + + groups.leave('administrators', uid, next); + }); }, callback); -} +}; User.createUser = function(socket, userData, callback) { if (!userData) {