From 10f56faf9dc421a7b70307238dbbe674eedf675e Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 1 Apr 2014 17:12:03 -0400 Subject: [PATCH] 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 --- public/src/forum/admin/users.js | 29 ++++- src/admin/user.js | 112 ------------------ src/categories.js | 1 + .../categories.js => categories/update.js} | 10 +- src/socket.io/admin.js | 57 +++++---- src/user/delete.js | 6 +- 6 files changed, 67 insertions(+), 148 deletions(-) delete mode 100644 src/admin/user.js rename src/{admin/categories.js => categories/update.js} (81%) diff --git a/public/src/forum/admin/users.js b/public/src/forum/admin/users.js index b25fa7d435..229c45cfb1 100644 --- a/public/src/forum/admin/users.js +++ b/public/src/forum/admin/users.js @@ -53,14 +53,25 @@ define(function() { if (!isUserAdmin(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'); 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); + 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')); @@ -85,14 +96,24 @@ define(function() { timeout: 5000 }); } 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); 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) { 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); updateUserBanButtons($('.ban-btn')); updateUserAdminButtons($('.admin-btn')); diff --git a/src/admin/user.js b/src/admin/user.js deleted file mode 100644 index daf6685aca..0000000000 --- a/src/admin/user.js +++ /dev/null @@ -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)); \ No newline at end of file diff --git a/src/categories.js b/src/categories.js index b9118aeb34..809c77c8e5 100644 --- a/src/categories.js +++ b/src/categories.js @@ -21,6 +21,7 @@ var db = require('./database'), require('./categories/activeusers')(Categories); require('./categories/recentreplies')(Categories); + require('./categories/update')(Categories); Categories.create = function(data, callback) { db.incrObjectField('global', 'nextCid', function(err, cid) { diff --git a/src/admin/categories.js b/src/categories/update.js similarity index 81% rename from src/admin/categories.js rename to src/categories/update.js index 0980032734..d52bd6904e 100644 --- a/src/admin/categories.js +++ b/src/categories/update.js @@ -3,12 +3,12 @@ var async = require('async'), db = require('./../database'), - utils = require('./../../public/src/utils'), - categories = require('./../categories'); + utils = require('./../../public/src/utils'); -(function(CategoriesAdmin) { - CategoriesAdmin.update = function(modified, socket, callback) { +module.exports = function(Categories) { + + Categories.update = function(modified, callback) { function updateCategory(cid, next) { var category = modified[cid]; @@ -43,4 +43,4 @@ var async = require('async'), }); }; -}(exports)); \ No newline at end of file +}; \ No newline at end of file diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 54537fd273..5888fd636d 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -9,12 +9,8 @@ var groups = require('../groups'), categories = require('../categories'), CategoryTools = require('../categoryTools'), logger = require('../logger'), + events = require('../events'), db = require('../database'), - admin = { - user: require('../admin/user'), - categories: require('../admin/categories') - }, - async = require('async'), winston = require('winston'), index = require('./index'), @@ -32,9 +28,8 @@ var groups = require('../groups'), }; SocketAdmin.before = function(socket, next) { - // Verify administrative privileges user.isAdministrator(socket.uid, function(err, isAdmin) { - if (isAdmin) { + if (!err && isAdmin) { next(); } else { 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 */ -SocketAdmin.user.makeAdmin = function(socket, theirid) { - admin.user.makeAdmin(socket.uid, theirid, socket); +SocketAdmin.user.makeAdmin = function(socket, theirid, callback) { + groups.join('administrators', theirid, callback); }; -SocketAdmin.user.removeAdmin = function(socket, theirid) { - admin.user.removeAdmin(socket.uid, theirid, socket); +SocketAdmin.user.removeAdmin = function(socket, theirid, callback) { + groups.leave('administrators', theirid, callback); }; -SocketAdmin.user.createUser = function(socket, user, callback) { - if(!user) { +SocketAdmin.user.createUser = function(socket, userData, callback) { + if (!userData) { return callback(new Error('invalid data')); } - admin.user.createUser(socket.uid, user, callback); + user.create(userData, callback); }; -SocketAdmin.user.banUser = function(socket, theirid) { - admin.user.banUser(socket.uid, theirid, socket, function(isBanned) { - if(isBanned) { +SocketAdmin.user.banUser = function(socket, theirid, callback) { + user.isAdministrator(theirid, function(err, isAdmin) { + 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); for(var i=0; i