ban and delete user admin actions takes an array of uids now.
v1.18.x
barisusakli 11 years ago
parent 590082709f
commit 967b486ce1

@ -6,135 +6,136 @@ define(function() {
Users.init = function() { Users.init = function() {
var yourid = ajaxify.variables.get('yourid'); var yourid = ajaxify.variables.get('yourid');
function isUserAdmin(element) { $('#users-container').on('click', '.select', function() {
var parent = $(element).parents('.users-box'); var userBox = $(this).parents('.users-box');
return (parent.attr('data-admin') !== "0"); var isSelected = userBox.hasClass('selected');
} userBox.toggleClass('selected', !isSelected);
$(this).toggleClass('fa-square-o', isSelected);
function isUserBanned(element) { $(this).toggleClass('fa-check-square-o', !isSelected);
var parent = $(element).parents('.users-box'); });
return (parent.attr('data-banned') !== "" && parent.attr('data-banned') !== "0");
}
function getUID(element) { function getSelectedUids() {
var parent = $(element).parents('.users-box'); var uids = [];
return parent.attr('data-uid'); $('#users-container .users-box.selected').each(function() {
uids.push($(this).attr('data-uid'));
});
return uids;
} }
function updateUserBanButtons(elements) { function update(className, state) {
elements.each(function(index, element) { $('#users-container .users-box.selected ' + className +'.label').each(function() {
var banBtn = $(element); $(this).toggleClass('hide', !state);
var uid = getUID(banBtn);
banBtn.toggleClass('disabled', isUserAdmin(banBtn) || uid === yourid);
banBtn.toggleClass('btn-warning', isUserBanned(banBtn));
}); });
} }
function updateUserAdminButtons(elements) { function unselectAll() {
elements.each(function(index, element) { $('#users-container .users-box.selected').removeClass('selected')
var adminBtn = $(element); .find('.select').toggleClass('fa-square-o', true).toggleClass('fa-check-square-o', false);
var uid = getUID(adminBtn);
adminBtn.toggleClass('disabled', (isUserAdmin(adminBtn) && uid === yourid) || isUserBanned(adminBtn));
adminBtn.toggleClass('btn-success', isUserAdmin(adminBtn));
});
} }
function updateButtons() { function removeSelected() {
updateUserBanButtons($('.ban-btn')); $('#users-container .users-box.selected').remove();
updateUserAdminButtons($('.admin-btn'));
} }
$('#users-container').on('click', '.ban-btn', function() { $('.ban-user').on('click', function() {
var banBtn = $(this); var uids = getSelectedUids();
var parent = banBtn.parents('.users-box'); if (!uids.length) {
var uid = getUID(banBtn); return;
}
if (!isUserAdmin(banBtn)) { bootbox.confirm('Do you really want to ban?', function(confirm) {
if (isUserBanned(banBtn)) { if (confirm) {
socket.emit('admin.user.unbanUser', uid, function(err) { socket.emit('admin.user.banUsers', uids, function(err) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
app.alertSuccess('This user is unbanned!'); app.alertSuccess('User(s) banned!');
});
banBtn.removeClass('btn-warning');
parent.attr('data-banned', 0);
updateUserAdminButtons($('.admin-btn'));
} else {
bootbox.confirm('Do you really want to ban "' + parent.attr('data-username') + '"?', function(confirm) {
if (confirm) {
socket.emit('admin.user.banUser', uid, function(err) {
if (err) {
return app.alertError(err.message);
}
app.alertSuccess('This user is banned!');
});
banBtn.addClass('btn-warning');
parent.attr('data-banned', 1);
updateUserAdminButtons($('.admin-btn'));
}
}); });
update('.ban', true);
unselectAll();
} }
});
});
$('.unban-user').on('click', function() {
var uids = getSelectedUids();
if (!uids.length) {
return;
} }
return false; socket.emit('admin.user.unbanUsers', uids, function(err) {
if (err) {
return app.alertError(err.message);
}
app.alertSuccess('User(s) unbanned!');
});
update('.ban', false);
unselectAll();
}); });
$('#users-container').on('click', '.admin-btn', function() { $('.admin-user').on('click', function() {
var adminBtn = $(this); var uids = getSelectedUids();
var parent = adminBtn.parents('.users-box'); if (!uids.length) {
var uid = getUID(adminBtn); return;
}
if(uid === yourid) {
app.alert({ if (uids.indexOf(yourid) !== -1) {
title: 'Error', app.alertError('You can\'t remove yourself as Administrator!');
message: 'You can\'t remove yourself as Administrator!', } else {
type: 'danger', socket.emit('admin.user.makeAdmins', uids, function(err) {
timeout: 5000
});
} else if (!isUserAdmin(adminBtn)) {
socket.emit('admin.user.makeAdmin', uid, function(err) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
app.alertSuccess('This user is now an administrator.'); app.alertSuccess('User(s) are now administrators.');
}); });
parent.attr('data-admin', 1);
updateUserBanButtons($('.ban-btn')); update('.administrator', true);
updateUserAdminButtons($('.admin-btn')); unselectAll();
} else if(uid !== yourid) { }
bootbox.confirm('Do you really want to remove this user as admin "' + parent.attr('data-username') + '"?', function(confirm) { });
$('.remove-admin-user').on('click', function() {
var uids = getSelectedUids();
if (!uids.length) {
return;
}
if (uids.indexOf(yourid) !== -1) {
app.alertError('You can\'t remove yourself as Administrator!');
} else {
bootbox.confirm('Do you really want to remove admins?', function(confirm) {
if (confirm) { if (confirm) {
socket.emit('admin.user.removeAdmin', uid, function(err) { socket.emit('admin.user.removeAdmins', uids, function(err) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
app.alertSuccess('This user is no longer an administrator.'); app.alertSuccess('User(s) are no longer administrators.');
}); });
parent.attr('data-admin', 0);
updateUserBanButtons($('.ban-btn')); update('.administrator', false);
updateUserAdminButtons($('.admin-btn')); unselectAll();
} }
}); });
} }
return false;
}); });
$('#users-container').on('click', '.delete-btn', function() { $('.delete-user').on('click', function() {
var deleteBtn = $(this); var uids = getSelectedUids();
var parent = deleteBtn.parents('.users-box'); if (!uids.length) {
var uid = getUID(deleteBtn); return;
bootbox.confirm('<b>Warning!</b><br/>Do you really want to delete this user "' + parent.attr('data-username') + '"?<br/> This action is not reversable, all user data and content will be erased!', function(confirm) { }
bootbox.confirm('<b>Warning!</b><br/>Do you really want to delete user(s)?<br/> This action is not reversable, all user data and content will be erased!', function(confirm) {
if (confirm) { if (confirm) {
socket.emit('admin.user.deleteUser', uid, function(err) { socket.emit('admin.user.deleteUsers', uids, function(err) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
parent.remove();
app.alertSuccess('User Deleted!'); app.alertSuccess('User(s) Deleted!');
removeSelected();
unselectAll();
}); });
} }
}); });
@ -179,7 +180,6 @@ define(function() {
}); });
} }
$('document').ready(function() { $('document').ready(function() {
var timeoutId = 0, var timeoutId = 0,
@ -230,23 +230,17 @@ define(function() {
.addClass('label-success') .addClass('label-success')
.removeClass('label-danger'); .removeClass('label-danger');
} }
updateButtons();
}); });
}); });
}, 250); }, 250);
}); });
updateButtons();
handleUserCreate(); handleUserCreate();
function onUsersLoaded(users) { function onUsersLoaded(users) {
ajaxify.loadTemplate('admin/users', function(adminUsers) { ajaxify.loadTemplate('admin/users', function(adminUsers) {
var html = $(templates.parse(templates.getBlock(adminUsers, 'users'), {users: users})); var html = $(templates.parse(templates.getBlock(adminUsers, 'users'), {users: users}));
$('#users-container').append(html); $('#users-container').append(html);
updateUserBanButtons(html.find('.ban-btn'));
updateUserAdminButtons(html.find('.admin-btn'));
}); });
} }

@ -9,14 +9,24 @@ var groups = require('../../groups'),
User = {}; User = {};
User.makeAdmin = function(socket, theirid, callback) { User.makeAdmins = function(socket, uids, callback) {
groups.join('administrators', theirid, callback); toggleAdmin(uids, true, callback);
}; };
User.removeAdmin = function(socket, theirid, callback) { User.removeAdmins = function(socket, uids, callback) {
groups.leave('administrators', theirid, callback); toggleAdmin(uids, false, callback);
}; };
function toggleAdmin(uids, isAdmin, 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);
}, callback);
}
User.createUser = function(socket, userData, callback) { User.createUser = function(socket, userData, callback) {
if (!userData) { if (!userData) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
@ -24,44 +34,61 @@ User.createUser = function(socket, userData, callback) {
user.create(userData, callback); user.create(userData, callback);
}; };
User.banUser = function(socket, theirid, callback) { User.banUsers = function(socket, uids, callback) {
user.isAdministrator(theirid, function(err, isAdmin) { if(!Array.isArray(uids)) {
return callback(new Error('[[error:invalid-data]]'));
}
async.each(uids, banUser, callback);
};
function banUser(uid, callback) {
user.isAdministrator(uid, function(err, isAdmin) {
if (err || isAdmin) { if (err || isAdmin) {
return callback(err || new Error('[[error:cant-ban-other-admins]]')); return callback(err || new Error('[[error:cant-ban-other-admins]]'));
} }
user.ban(theirid, function(err) { user.ban(uid, function(err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var sockets = websockets.getUserSockets(theirid); var sockets = websockets.getUserSockets(uid);
for(var i=0; i<sockets.length; ++i) { for(var i=0; i<sockets.length; ++i) {
sockets[i].emit('event:banned'); sockets[i].emit('event:banned');
} }
websockets.logoutUser(theirid); websockets.logoutUser(uid);
callback(); callback();
}); });
}); });
}; }
User.unbanUser = function(socket, theirid, callback) { User.unbanUsers = function(socket, uids, callback) {
user.unban(theirid, callback); if(!Array.isArray(uids)) {
return callback(new Error('[[error:invalid-data]]'));
}
async.each(uids, user.unban, callback);
}; };
User.deleteUser = function(socket, theirid, callback) { User.deleteUsers = function(socket, uids, callback) {
user.delete(theirid, function(err) { if(!Array.isArray(uids)) {
if (err) { return callback(new Error('[[error:invalid-data]]'));
return callback(err); }
}
events.logAdminUserDelete(socket.uid, theirid); async.each(uids, function(uid, next) {
user.delete(uid, function(err) {
if (err) {
return next(err);
}
websockets.logoutUser(theirid); events.logAdminUserDelete(socket.uid, uid);
callback();
}); websockets.logoutUser(uid);
next();
});
}, callback);
}; };
User.search = function(socket, username, callback) { User.search = function(socket, username, callback) {

@ -206,8 +206,6 @@ var winston = require('winston'),
}); });
}; };
ThreadTools.toggleFollow = function(tid, uid, callback) { ThreadTools.toggleFollow = function(tid, uid, callback) {
topics.isFollowing(tid, uid, function(err, following) { topics.isFollowing(tid, uid, function(err, following) {
if (err) { if (err) {

@ -250,7 +250,8 @@ var bcrypt = require('bcryptjs'),
}, },
function(isAdmin, next) { function(isAdmin, next) {
user.status = !user.status ? 'online' : user.status; user.status = !user.status ? 'online' : user.status;
user.administrator = isAdmin ? '1':'0'; user.administrator = isAdmin;
user.banned = parseInt(user.banned, 10) === 1;
db.isSortedSetMember('users:online', user.uid, next); db.isSortedSetMember('users:online', user.uid, next);
}, },
function(isMember, next) { function(isMember, next) {

Loading…
Cancel
Save