From ce126b11fb4bda4e9bc202c6ae1db02987e53a59 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 7 Sep 2013 15:49:23 -0400 Subject: [PATCH] fixes to administrator methods --- src/admin/user.js | 47 +++++++++++++++++++++++++++++------------------ src/install.js | 38 +++++++++++++++++++++++--------------- src/user.js | 34 +++++----------------------------- 3 files changed, 57 insertions(+), 62 deletions(-) diff --git a/src/admin/user.js b/src/admin/user.js index 88ee02f467..4da5a06360 100644 --- a/src/admin/user.js +++ b/src/admin/user.js @@ -1,22 +1,28 @@ -var RDB = require('./../redis.js'), - utils = require('./../../public/src/utils.js'), - user = require('./../user.js'); +var RDB = require('../redis'), + utils = require('../../public/src/utils'), + user = require('../user'), + Groups = require('../groups'); (function(UserAdmin) { UserAdmin.makeAdmin = function(uid, theirid, socket) { user.isAdministrator(uid, function(isAdmin) { if(isAdmin) { - user.makeAdministrator(theirid, function(data) { - socket.emit('event:alert', { - title: 'User Modified', - message: 'This user is now an administrator!', - type: 'success', - timeout: 2000 - }); + Groups.getGidFromName('Administrators', function(err, gid) { + Groups.join(gid, theirid, function(err) { + if (!err) { + user.setUserField(theirid, 'administrator', 1); + + socket.emit('event:alert', { + title: 'User Modified', + message: 'This user is now an administrator!', + type: 'success', + timeout: 2000 + }); + } + }); }); - } - else { + } else { socket.emit('event:alert', { title: 'Warning', message: 'You need to be an administrator to make someone else an administrator!', @@ -30,13 +36,18 @@ var RDB = require('./../redis.js'), UserAdmin.removeAdmin = function(uid, theirid, socket) { user.isAdministrator(uid, function(isAdmin) { if(isAdmin) { - user.removeAdministrator(theirid, function(data) { + Groups.getGidFromName('Administrators', function(err, gid) { + Groups.leave(gid, theirid, function(err) { + if (!err) { + user.setUserField(theirid, 'administrator', 0); - socket.emit('event:alert', { - title: 'User Modified', - message: 'This user is no longer an administrator!', - type: 'success', - timeout: 2000 + socket.emit('event:alert', { + title: 'User Modified', + message: 'This user is no longer an administrator!', + type: 'success', + timeout: 2000 + }); + } }); }); } diff --git a/src/install.js b/src/install.js index 75eb60b9aa..90dc170a50 100644 --- a/src/install.js +++ b/src/install.js @@ -192,23 +192,31 @@ var async = require('async'), hidden: true, type: 'string' } - ]; - - prompt.get(questions, function(err, results) { - if (!results) return callback(new Error('aborted')); - - nconf.set('bcrypt_rounds', 12); - User.create(results.username, results.password, results.email, function(err, uid) { - Groups.getGidFromName('Administrators', function(err, gid) { - if (gid) Groups.join(gid, uid, callback); - else { - Groups.create('Administrators', 'Forum Administrators', function(err, groupObj) { - Groups.join(groupObj.gid, uid, callback); + ], + getAdminInfo = function(callback) { + prompt.get(questions, function(err, results) { + if (!results) return callback(new Error('aborted')); + + nconf.set('bcrypt_rounds', 12); + User.create(results.username, results.password, results.email, function(err, uid) { + if (err) { + winston.warn(err.message + ' Please try again.'); + return getAdminInfo(); + } + + Groups.getGidFromName('Administrators', function(err, gid) { + if (gid) Groups.join(gid, uid, callback); + else { + Groups.create('Administrators', 'Forum Administrators', function(err, groupObj) { + Groups.join(groupObj.gid, uid, callback); + }); + } }); - } + }); }); - }); - }); + }; + + getAdminInfo(callback); }, save: function(server_conf, client_conf, callback) { // Server Config diff --git a/src/user.js b/src/user.js index 271b6874e0..ac40e37703 100644 --- a/src/user.js +++ b/src/user.js @@ -5,6 +5,7 @@ var utils = require('./../public/src/utils.js'), meta = require('./meta.js'), emailjsServer = emailjs.server.connect(meta.config.mailer || '127.0.0.1'), bcrypt = require('bcrypt'), + Groups = require('./groups'), notifications = require('./notifications.js'), topics = require('./topics.js'), async = require('async'), @@ -769,35 +770,10 @@ var utils = require('./../public/src/utils.js'), } User.isAdministrator = function(uid, callback) { - RDB.sismember('administrators', uid, function(err, exists) { - RDB.handle(err); - callback(!!exists); - }); - } - - User.makeAdministrator = function(uid, callback) { - RDB.sadd('administrators', uid, function(err, data){ - if(!err) { - User.setUserField(uid, 'administrator', 1); - } - - if(callback) { - // @todo address why we're only sending back a boolean in the callback and not an error if it occurred - callback(err === null); - } - }); - } - - User.removeAdministrator = function(uid, callback) { - RDB.srem('administrators', uid, function(err, data){ - if(!err) { - User.setUserField(uid, 'administrator', 0); - } - - if(callback) { - // @todo address why we're only sending back a boolean in the callback and not an error if it occurred - callback(err === null); - } + Groups.getGidFromName('Administrators', function(err, gid) { + Groups.isMember(uid, gid, function(err, isAdmin) { + callback(isAdmin); + }); }); }