controllers/users refactor

v1.18.x
barisusakli 8 years ago
parent 566ff95d3c
commit 2188036148

@ -8,9 +8,7 @@ var pagination = require('../pagination');
var db = require('../database'); var db = require('../database');
var helpers = require('./helpers'); var helpers = require('./helpers');
var usersController = module.exports;
var usersController = {};
usersController.index = function (req, res, next) { usersController.index = function (req, res, next) {
var section = req.query.section || 'joindate'; var section = req.query.section || 'joindate';
@ -33,6 +31,8 @@ usersController.index = function (req, res, next) {
}; };
usersController.search = function (req, res, next) { usersController.search = function (req, res, next) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
search: function (next) { search: function (next) {
user.search({ user.search({
@ -48,21 +48,22 @@ usersController.search = function (req, res, next) {
isAdminOrGlobalMod: function (next) { isAdminOrGlobalMod: function (next) {
user.isAdminOrGlobalMod(req.uid, next); user.isAdminOrGlobalMod(req.uid, next);
}, },
}, function (err, results) { }, next);
if (err) { },
return next(err); function (results, next) {
}
var section = req.query.section || 'joindate'; var section = req.query.section || 'joindate';
results.search.isAdminOrGlobalMod = results.isAdminOrGlobalMod; results.search.isAdminOrGlobalMod = results.isAdminOrGlobalMod;
results.search.pagination = pagination.create(req.query.page, results.search.pageCount, req.query); results.search.pagination = pagination.create(req.query.page, results.search.pageCount, req.query);
results.search['section_' + section] = true; results.search['section_' + section] = true;
render(req, res, results.search, next); render(req, res, results.search, next);
}); },
], next);
}; };
usersController.getOnlineUsers = function (req, res, next) { usersController.getOnlineUsers = function (req, res, next) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
users: function (next) { users: function (next) {
usersController.getUsers('users:online', req.uid, req.query, next); usersController.getUsers('users:online', req.uid, req.query, next);
@ -70,10 +71,9 @@ usersController.getOnlineUsers = function (req, res, next) {
guests: function (next) { guests: function (next) {
require('../socket.io/admin/rooms').getTotalGuestCount(next); require('../socket.io/admin/rooms').getTotalGuestCount(next);
}, },
}, function (err, results) { }, next);
if (err) { },
return next(err); function (results, next) {
}
var userData = results.users; var userData = results.users;
var hiddenCount = 0; var hiddenCount = 0;
if (!userData.isAdminOrGlobalMod) { if (!userData.isAdminOrGlobalMod) {
@ -88,7 +88,8 @@ usersController.getOnlineUsers = function (req, res, next) {
userData.anonymousUserCount = results.guests + hiddenCount; userData.anonymousUserCount = results.guests + hiddenCount;
render(req, res, userData, next); render(req, res, userData, next);
}); },
], next);
}; };
usersController.getUsersSortedByPosts = function (req, res, next) { usersController.getUsersSortedByPosts = function (req, res, next) {
@ -107,41 +108,36 @@ usersController.getUsersSortedByJoinDate = function (req, res, next) {
}; };
usersController.getBannedUsers = function (req, res, next) { usersController.getBannedUsers = function (req, res, next) {
usersController.getUsers('users:banned', req.uid, req.query, function (err, userData) { renderIfAdminOrGlobalMod('users:banned', req, res, next);
if (err) {
return next(err);
}
if (!userData.isAdminOrGlobalMod) {
return next();
}
render(req, res, userData, next);
});
}; };
usersController.getFlaggedUsers = function (req, res, next) { usersController.getFlaggedUsers = function (req, res, next) {
usersController.getUsers('users:flags', req.uid, req.query, function (err, userData) { renderIfAdminOrGlobalMod('users:flags', req, res, next);
if (err) {
return next(err);
}
if (!userData.isAdminOrGlobalMod) {
return next();
}
render(req, res, userData, next);
});
}; };
usersController.renderUsersPage = function (set, req, res, next) { function renderIfAdminOrGlobalMod(set, req, res, next) {
usersController.getUsers(set, req.uid, req.query, function (err, userData) { async.waterfall([
if (err) { function (next) {
return next(err); user.isAdminOrGlobalMod(req.uid, next);
},
function (isAdminOrGlobalMod, next) {
if (!isAdminOrGlobalMod) {
return helpers.notAllowed(req, res);
}
usersController.renderUsersPage(set, req, res, next);
},
], next);
} }
usersController.renderUsersPage = function (set, req, res, next) {
async.waterfall([
function (next) {
usersController.getUsers(set, req.uid, req.query, next);
},
function (userData, next) {
render(req, res, userData, next); render(req, res, userData, next);
}); },
], next);
}; };
usersController.getUsers = function (set, uid, query, callback) { usersController.getUsers = function (set, uid, query, callback) {
@ -169,6 +165,8 @@ usersController.getUsers = function (set, uid, query, callback) {
var start = Math.max(0, page - 1) * resultsPerPage; var start = Math.max(0, page - 1) * resultsPerPage;
var stop = start + resultsPerPage - 1; var stop = start + resultsPerPage - 1;
async.waterfall([
function (next) {
async.parallel({ async.parallel({
isAdminOrGlobalMod: function (next) { isAdminOrGlobalMod: function (next) {
user.isAdminOrGlobalMod(uid, next); user.isAdminOrGlobalMod(uid, next);
@ -176,11 +174,9 @@ usersController.getUsers = function (set, uid, query, callback) {
usersData: function (next) { usersData: function (next) {
usersController.getUsersAndCount(set, uid, start, stop, next); usersController.getUsersAndCount(set, uid, start, stop, next);
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
var pageCount = Math.ceil(results.usersData.count / resultsPerPage); var pageCount = Math.ceil(results.usersData.count / resultsPerPage);
var userData = { var userData = {
users: results.usersData.users, users: results.usersData.users,
@ -191,11 +187,14 @@ usersController.getUsers = function (set, uid, query, callback) {
isAdminOrGlobalMod: results.isAdminOrGlobalMod, isAdminOrGlobalMod: results.isAdminOrGlobalMod,
}; };
userData['section_' + (query.section || 'joindate')] = true; userData['section_' + (query.section || 'joindate')] = true;
callback(null, userData); next(null, userData);
}); },
], callback);
}; };
usersController.getUsersAndCount = function (set, uid, start, stop, callback) { usersController.getUsersAndCount = function (set, uid, start, stop, callback) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
users: function (next) { users: function (next) {
user.getUsersFromSet(set, uid, start, stop, next); user.getUsersFromSet(set, uid, start, stop, next);
@ -212,16 +211,16 @@ usersController.getUsersAndCount = function (set, uid, start, stop, callback) {
db.getObjectField('global', 'userCount', next); db.getObjectField('global', 'userCount', next);
} }
}, },
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
results.users = results.users.filter(function (user) { results.users = results.users.filter(function (user) {
return user && parseInt(user.uid, 10); return user && parseInt(user.uid, 10);
}); });
callback(null, results); next(null, results);
}); },
], callback);
}; };
function render(req, res, data, next) { function render(req, res, data, next) {
@ -232,16 +231,15 @@ function render(req, res, data, next) {
data.adminInviteOnly = registrationType === 'admin-invite-only'; data.adminInviteOnly = registrationType === 'admin-invite-only';
data['reputation:disabled'] = parseInt(meta.config['reputation:disabled'], 10) === 1; data['reputation:disabled'] = parseInt(meta.config['reputation:disabled'], 10) === 1;
user.getInvitesNumber(req.uid, function (err, numInvites) { async.waterfall([
if (err) { function (next) {
return next(err); user.getInvitesNumber(req.uid, next);
} },
function (numInvites) {
res.append('X-Total-Count', data.userCount); res.append('X-Total-Count', data.userCount);
data.invites = numInvites; data.invites = numInvites;
res.render('users', data); res.render('users', data);
}); },
], next);
} }
module.exports = usersController;

@ -53,22 +53,24 @@ middleware.ensureSelfOrGlobalPrivilege = function (req, res, next) {
The "self" part of this middleware hinges on you having used The "self" part of this middleware hinges on you having used
middleware.exposeUid prior to invoking this middleware. middleware.exposeUid prior to invoking this middleware.
*/ */
if (req.user) { async.waterfall([
if (req.user.uid === res.locals.uid) { function (next) {
return next(); if (!req.uid) {
return setImmediate(next, null, false);
} }
user.isAdminOrGlobalMod(req.uid, function (err, ok) { if (req.uid === parseInt(res.locals.uid, 10)) {
if (err) { return setImmediate(next, null, true);
return next(err);
} else if (ok) {
return next();
} }
controllers.helpers.notAllowed(req, res); user.isAdminOrGlobalMod(req.uid, next);
}); },
} else { function (isAdminOrGlobalMod, next) {
controllers.helpers.notAllowed(req, res); if (!isAdminOrGlobalMod) {
return controllers.helpers.notAllowed(req, res);
} }
next();
},
], next);
}; };
middleware.pageView = function (req, res, next) { middleware.pageView = function (req, res, next) {

Loading…
Cancel
Save