fixes to administrator methods

v1.18.x
Julian Lam 12 years ago
parent 780cec2596
commit ce126b11fb

@ -1,22 +1,28 @@
var RDB = require('./../redis.js'), var RDB = require('../redis'),
utils = require('./../../public/src/utils.js'), utils = require('../../public/src/utils'),
user = require('./../user.js'); user = require('../user'),
Groups = require('../groups');
(function(UserAdmin) { (function(UserAdmin) {
UserAdmin.makeAdmin = function(uid, theirid, socket) { UserAdmin.makeAdmin = function(uid, theirid, socket) {
user.isAdministrator(uid, function(isAdmin) { user.isAdministrator(uid, function(isAdmin) {
if(isAdmin) { if(isAdmin) {
user.makeAdministrator(theirid, function(data) { Groups.getGidFromName('Administrators', function(err, gid) {
Groups.join(gid, theirid, function(err) {
if (!err) {
user.setUserField(theirid, 'administrator', 1);
socket.emit('event:alert', { socket.emit('event:alert', {
title: 'User Modified', title: 'User Modified',
message: 'This user is now an administrator!', message: 'This user is now an administrator!',
type: 'success', type: 'success',
timeout: 2000 timeout: 2000
}); });
});
} }
else { });
});
} else {
socket.emit('event:alert', { socket.emit('event:alert', {
title: 'Warning', title: 'Warning',
message: 'You need to be an administrator to make someone else an administrator!', message: 'You need to be an administrator to make someone else an administrator!',
@ -30,7 +36,10 @@ var RDB = require('./../redis.js'),
UserAdmin.removeAdmin = function(uid, theirid, socket) { UserAdmin.removeAdmin = function(uid, theirid, socket) {
user.isAdministrator(uid, function(isAdmin) { user.isAdministrator(uid, function(isAdmin) {
if(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', { socket.emit('event:alert', {
title: 'User Modified', title: 'User Modified',
@ -38,6 +47,8 @@ var RDB = require('./../redis.js'),
type: 'success', type: 'success',
timeout: 2000 timeout: 2000
}); });
}
});
}); });
} }
}); });

@ -192,13 +192,18 @@ var async = require('async'),
hidden: true, hidden: true,
type: 'string' type: 'string'
} }
]; ],
getAdminInfo = function(callback) {
prompt.get(questions, function(err, results) { prompt.get(questions, function(err, results) {
if (!results) return callback(new Error('aborted')); if (!results) return callback(new Error('aborted'));
nconf.set('bcrypt_rounds', 12); nconf.set('bcrypt_rounds', 12);
User.create(results.username, results.password, results.email, function(err, uid) { 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) { Groups.getGidFromName('Administrators', function(err, gid) {
if (gid) Groups.join(gid, uid, callback); if (gid) Groups.join(gid, uid, callback);
else { else {
@ -209,6 +214,9 @@ var async = require('async'),
}); });
}); });
}); });
};
getAdminInfo(callback);
}, },
save: function(server_conf, client_conf, callback) { save: function(server_conf, client_conf, callback) {
// Server Config // Server Config

@ -5,6 +5,7 @@ var utils = require('./../public/src/utils.js'),
meta = require('./meta.js'), meta = require('./meta.js'),
emailjsServer = emailjs.server.connect(meta.config.mailer || '127.0.0.1'), emailjsServer = emailjs.server.connect(meta.config.mailer || '127.0.0.1'),
bcrypt = require('bcrypt'), bcrypt = require('bcrypt'),
Groups = require('./groups'),
notifications = require('./notifications.js'), notifications = require('./notifications.js'),
topics = require('./topics.js'), topics = require('./topics.js'),
async = require('async'), async = require('async'),
@ -769,35 +770,10 @@ var utils = require('./../public/src/utils.js'),
} }
User.isAdministrator = function(uid, callback) { User.isAdministrator = function(uid, callback) {
RDB.sismember('administrators', uid, function(err, exists) { Groups.getGidFromName('Administrators', function(err, gid) {
RDB.handle(err); Groups.isMember(uid, gid, function(err, isAdmin) {
callback(!!exists); callback(isAdmin);
}); });
}
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);
}
}); });
} }

Loading…
Cancel
Save