admin cleanup

removed unnecessary admin checks in src/admin/user then realized they
are just one liners so moved them to src/socket.io.admin.js moved
categories update to categories folder
v1.18.x
barisusakli 11 years ago
parent ac88b5fc25
commit 10f56faf9d

@ -53,14 +53,25 @@ define(function() {
if (!isUserAdmin(banBtn)) { if (!isUserAdmin(banBtn)) {
if (isUserBanned(banBtn)) { if (isUserBanned(banBtn)) {
socket.emit('admin.user.unbanUser', uid); socket.emit('admin.user.unbanUser', uid, function(err) {
if (err) {
return app.alertError(err.message);
}
app.alertSuccess('This user is unbanned!');
});
banBtn.removeClass('btn-warning'); banBtn.removeClass('btn-warning');
parent.attr('data-banned', 0); parent.attr('data-banned', 0);
updateUserAdminButtons($('.admin-btn')); updateUserAdminButtons($('.admin-btn'));
} else { } else {
bootbox.confirm('Do you really want to ban "' + parent.attr('data-username') + '"?', function(confirm) { bootbox.confirm('Do you really want to ban "' + parent.attr('data-username') + '"?', function(confirm) {
if (confirm) { if (confirm) {
socket.emit('admin.user.banUser', uid); 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'); banBtn.addClass('btn-warning');
parent.attr('data-banned', 1); parent.attr('data-banned', 1);
updateUserAdminButtons($('.admin-btn')); updateUserAdminButtons($('.admin-btn'));
@ -85,14 +96,24 @@ define(function() {
timeout: 5000 timeout: 5000
}); });
} else if (!isUserAdmin(adminBtn)) { } else if (!isUserAdmin(adminBtn)) {
socket.emit('admin.user.makeAdmin', uid); socket.emit('admin.user.makeAdmin', uid, function(err) {
if (err) {
return app.alertError(err.message);
}
app.alertSuccess('This user is now an administrator.');
});
parent.attr('data-admin', 1); parent.attr('data-admin', 1);
updateUserBanButtons($('.ban-btn')); updateUserBanButtons($('.ban-btn'));
updateUserAdminButtons($('.admin-btn')); updateUserAdminButtons($('.admin-btn'));
} else if(uid !== yourid) { } else if(uid !== yourid) {
bootbox.confirm('Do you really want to remove this user as admin "' + parent.attr('data-username') + '"?', function(confirm) { bootbox.confirm('Do you really want to remove this user as admin "' + parent.attr('data-username') + '"?', function(confirm) {
if (confirm) { if (confirm) {
socket.emit('admin.user.removeAdmin', uid); socket.emit('admin.user.removeAdmin', uid, function(err) {
if (err) {
return app.alertError(err.message);
}
app.alertSuccess('This user is no longer an administrator.');
});
parent.attr('data-admin', 0); parent.attr('data-admin', 0);
updateUserBanButtons($('.ban-btn')); updateUserBanButtons($('.ban-btn'));
updateUserAdminButtons($('.admin-btn')); updateUserAdminButtons($('.admin-btn'));

@ -1,112 +0,0 @@
'use strict';
var async = require('async'),
utils = require('../../public/src/utils'),
user = require('../user'),
groups = require('../groups');
(function(UserAdmin) {
UserAdmin.createUser = function(uid, userData, callback) {
user.isAdministrator(uid, function(err, isAdmin) {
if(err || !isAdmin) {
return callback(err || new Error('You are not an administrator'));
}
user.create(userData, callback);
});
};
UserAdmin.makeAdmin = function(uid, theirid, socket) {
user.isAdministrator(uid, function(err, isAdmin) {
if (isAdmin) {
groups.join('administrators', theirid, function(err) {
if (!err) {
socket.emit('event:alert', {
title: 'User Modified',
message: 'This user is now an administrator!',
type: 'success',
timeout: 2000
});
}
});
} else {
socket.emit('event:alert', {
title: 'Warning',
message: 'You need to be an administrator to make someone else an administrator!',
type: 'warning',
timeout: 2000
});
}
});
};
UserAdmin.removeAdmin = function(uid, theirid, socket) {
user.isAdministrator(uid, function(err, isAdmin) {
if (isAdmin) {
groups.leave('administrators', theirid, function(err) {
if (!err) {
socket.emit('event:alert', {
title: 'User Modified',
message: 'This user is no longer an administrator!',
type: 'success',
timeout: 2000
});
}
});
}
});
};
UserAdmin.banUser = function(uid, theirid, socket, callback) {
user.isAdministrator(uid, function(err, amIAdmin) {
user.isAdministrator(theirid, function(err, areTheyAdmin) {
if (amIAdmin && !areTheyAdmin) {
user.ban(theirid, function(err, result) {
callback(true);
socket.emit('event:alert', {
alert_id: 'ban_user',
title: 'User Banned',
message: 'This user is banned!',
type: 'success',
timeout: 2000
});
});
}
});
});
};
UserAdmin.unbanUser = function(uid, theirid, socket) {
user.isAdministrator(uid, function(err, amIAdmin) {
if (amIAdmin) {
user.unban(theirid, function(err, result) {
socket.emit('event:alert', {
alert_id: 'ban_user',
title: 'User Unbanned',
message: 'This user is unbanned!',
type: 'success',
timeout: 2000
});
});
}
});
};
UserAdmin.deleteUser = function(uid, theirid, callback) {
async.waterfall([
function(next) {
user.isAdministrator(uid, next);
},
function(isAdmin, next) {
if(!isAdmin) {
return next(new Error('You are not an administrator'));
}
user.delete(uid, theirid, next);
}
], callback);
};
}(exports));

@ -21,6 +21,7 @@ var db = require('./database'),
require('./categories/activeusers')(Categories); require('./categories/activeusers')(Categories);
require('./categories/recentreplies')(Categories); require('./categories/recentreplies')(Categories);
require('./categories/update')(Categories);
Categories.create = function(data, callback) { Categories.create = function(data, callback) {
db.incrObjectField('global', 'nextCid', function(err, cid) { db.incrObjectField('global', 'nextCid', function(err, cid) {

@ -3,12 +3,12 @@
var async = require('async'), var async = require('async'),
db = require('./../database'), db = require('./../database'),
utils = require('./../../public/src/utils'), utils = require('./../../public/src/utils');
categories = require('./../categories');
(function(CategoriesAdmin) {
CategoriesAdmin.update = function(modified, socket, callback) { module.exports = function(Categories) {
Categories.update = function(modified, callback) {
function updateCategory(cid, next) { function updateCategory(cid, next) {
var category = modified[cid]; var category = modified[cid];
@ -43,4 +43,4 @@ var async = require('async'),
}); });
}; };
}(exports)); };

@ -9,12 +9,8 @@ var groups = require('../groups'),
categories = require('../categories'), categories = require('../categories'),
CategoryTools = require('../categoryTools'), CategoryTools = require('../categoryTools'),
logger = require('../logger'), logger = require('../logger'),
events = require('../events'),
db = require('../database'), db = require('../database'),
admin = {
user: require('../admin/user'),
categories: require('../admin/categories')
},
async = require('async'), async = require('async'),
winston = require('winston'), winston = require('winston'),
index = require('./index'), index = require('./index'),
@ -32,9 +28,8 @@ var groups = require('../groups'),
}; };
SocketAdmin.before = function(socket, next) { SocketAdmin.before = function(socket, next) {
// Verify administrative privileges
user.isAdministrator(socket.uid, function(err, isAdmin) { user.isAdministrator(socket.uid, function(err, isAdmin) {
if (isAdmin) { if (!err && isAdmin) {
next(); next();
} else { } else {
winston.warn('[socket.io] Call to admin method blocked (accessed by uid ' + socket.uid + ')'); winston.warn('[socket.io] Call to admin method blocked (accessed by uid ' + socket.uid + ')');
@ -74,24 +69,32 @@ SocketAdmin.fireEvent = function(socket, data, callback) {
}; };
/* User */ /* User */
SocketAdmin.user.makeAdmin = function(socket, theirid) { SocketAdmin.user.makeAdmin = function(socket, theirid, callback) {
admin.user.makeAdmin(socket.uid, theirid, socket); groups.join('administrators', theirid, callback);
}; };
SocketAdmin.user.removeAdmin = function(socket, theirid) { SocketAdmin.user.removeAdmin = function(socket, theirid, callback) {
admin.user.removeAdmin(socket.uid, theirid, socket); groups.leave('administrators', theirid, callback);
}; };
SocketAdmin.user.createUser = function(socket, user, callback) { SocketAdmin.user.createUser = function(socket, userData, callback) {
if(!user) { if (!userData) {
return callback(new Error('invalid data')); return callback(new Error('invalid data'));
} }
admin.user.createUser(socket.uid, user, callback); user.create(userData, callback);
}; };
SocketAdmin.user.banUser = function(socket, theirid) { SocketAdmin.user.banUser = function(socket, theirid, callback) {
admin.user.banUser(socket.uid, theirid, socket, function(isBanned) { user.isAdministrator(theirid, function(err, isAdmin) {
if(isBanned) { if (err || isAdmin) {
return callback(err || new Error('You can\'t ban other admins!'));
}
user.ban(theirid, function(err) {
if (err) {
return callback(err);
}
var sockets = index.getUserSockets(theirid); var sockets = index.getUserSockets(theirid);
for(var i=0; i<sockets.length; ++i) { for(var i=0; i<sockets.length; ++i) {
@ -99,16 +102,26 @@ SocketAdmin.user.banUser = function(socket, theirid) {
} }
module.parent.exports.logoutUser(theirid); module.parent.exports.logoutUser(theirid);
} callback();
});
}); });
}; };
SocketAdmin.user.unbanUser = function(socket, theirid) { SocketAdmin.user.unbanUser = function(socket, theirid, callback) {
admin.user.unbanUser(socket.uid, theirid, socket); user.unban(theirid, callback);
}; };
SocketAdmin.user.deleteUser = function(socket, theirid, callback) { SocketAdmin.user.deleteUser = function(socket, theirid, callback) {
admin.user.deleteUser(socket.uid, theirid, callback); user.delete(theirid, function(err) {
if (err) {
return callback(err);
}
events.logAdminUserDelete(socket.uid, theirid);
module.parent.exports.logoutUser(theirid);
callback();
});
}; };
SocketAdmin.user.search = function(socket, username, callback) { SocketAdmin.user.search = function(socket, username, callback) {
@ -144,7 +157,7 @@ SocketAdmin.categories.update = function(socket, data, callback) {
return callback(new Error('invalid data')); return callback(new Error('invalid data'));
} }
admin.categories.update(data, socket, callback); categories.update(data, callback);
}; };
SocketAdmin.categories.search = function(socket, data, callback) { SocketAdmin.categories.search = function(socket, data, callback) {

@ -7,22 +7,18 @@ var async = require('async'),
topics = require('./../topics'), topics = require('./../topics'),
categories = require('./../categories'), categories = require('./../categories'),
plugins = require('./../plugins'), plugins = require('./../plugins'),
events = require('./../events'),
groups = require('./../groups'); groups = require('./../groups');
module.exports = function(User) { module.exports = function(User) {
User.delete = function(adminUid, uid, callback) { User.delete = function(uid, callback) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
deletePosts(uid, next); deletePosts(uid, next);
}, },
function(next) { function(next) {
deleteTopics(uid, next); deleteTopics(uid, next);
},
function(next) {
events.logAdminUserDelete(adminUid, uid, next);
} }
], function(err) { ], function(err) {
if (err) { if (err) {

Loading…
Cancel
Save