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

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

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

@ -250,7 +250,8 @@ var bcrypt = require('bcryptjs'),
},
function(isAdmin, next) {
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);
},
function(isMember, next) {

Loading…
Cancel
Save