v1.18.x
Julian Lam 8 years ago
parent b9fd62cb6b
commit e80400665e

@ -24,14 +24,12 @@ flagsController.get = function(req, res, next) {
categories.buildForSelect(req.uid, next); categories.buildForSelect(req.uid, next);
}, },
flagData: function(next) { flagData: function(next) {
getFlagData(req, next); getFlagData(req, res, next);
}, },
analytics: function(next) { analytics: function(next) {
analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30, next); analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30, next);
}, },
assignees: function(next) { assignees: async.apply(user.getAdminsandGlobalModsandModerators)
user.getAdminsandGlobalMods(next);
}
}, function (err, results) { }, function (err, results) {
if (err) { if (err) {
return next(err); return next(err);
@ -45,6 +43,13 @@ flagsController.get = function(req, res, next) {
}; };
}); });
// If res.locals.cids is populated, then slim down the categories list
if (res.locals.cids) {
results.categories = results.categories.filter(function(category) {
return res.locals.cids.indexOf(String(category.cid)) !== -1;
});
}
var pageCount = Math.max(1, Math.ceil(results.flagData.count / itemsPerPage)); var pageCount = Math.max(1, Math.ceil(results.flagData.count / itemsPerPage));
results.categories.forEach(function(category) { results.categories.forEach(function(category) {
@ -66,10 +71,10 @@ flagsController.get = function(req, res, next) {
}); });
}; };
function getFlagData(req, callback) { function getFlagData(req, res, callback) {
var sortBy = req.query.sortBy || 'count'; var sortBy = req.query.sortBy || 'count';
var byUsername = req.query.byUsername || ''; var byUsername = req.query.byUsername || '';
var cid = req.query.cid || 0; var cid = req.query.cid || res.locals.cids || 0;
var page = parseInt(req.query.page, 10) || 1; var page = parseInt(req.query.page, 10) || 1;
var start = (page - 1) * itemsPerPage; var start = (page - 1) * itemsPerPage;
var stop = start + itemsPerPage - 1; var stop = start + itemsPerPage - 1;

@ -1,21 +1,10 @@
"use strict"; "use strict";
var user = require('../user'); var user = require('../user');
var adminFlagsController = require('./admin/flags');
var adminBlacklistController = require('./admin/blacklist'); var adminBlacklistController = require('./admin/blacklist');
var globalModsController = {}; var globalModsController = {};
globalModsController.flagged = function(req, res, next) {
user.isAdminOrGlobalMod(req.uid, function(err, isAdminOrGlobalMod) {
if (err || !isAdminOrGlobalMod) {
return next(err);
}
adminFlagsController.get(req, res, next);
});
};
globalModsController.ipBlacklist = function(req, res, next) { globalModsController.ipBlacklist = function(req, res, next) {
user.isAdminOrGlobalMod(req.uid, function(err, isAdminOrGlobalMod) { user.isAdminOrGlobalMod(req.uid, function(err, isAdminOrGlobalMod) {
if (err || !isAdminOrGlobalMod) { if (err || !isAdminOrGlobalMod) {

@ -27,6 +27,7 @@ var Controllers = {
api: require('./api'), api: require('./api'),
admin: require('./admin'), admin: require('./admin'),
globalMods: require('./globalmods'), globalMods: require('./globalmods'),
mods: require('./mods'),
sitemap: require('./sitemap') sitemap: require('./sitemap')
}; };

@ -0,0 +1,27 @@
"use strict";
var async = require('async');
var user = require('../user');
var adminFlagsController = require('./admin/flags');
var modsController = {};
modsController.flagged = function(req, res, next) {
async.parallel([
async.apply(user.isAdminOrGlobalMod, req.uid),
async.apply(user.isModeratorOfAnyCategory, req.uid)
], function(err, results) {
if (err || !(results[0] || results[1])) {
return next(err);
}
if (!results[0] && results[1]) {
res.locals.cids = results[1];
}
adminFlagsController.get(req, res, next);
});
};
module.exports = modsController;

@ -2,6 +2,7 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var _ = require('underscore');
var db = require('../database'); var db = require('../database');
var topics = require('../topics'); var topics = require('../topics');
@ -56,14 +57,29 @@ module.exports = function(Posts) {
if (!cid) { if (!cid) {
return callback(null, pids); return callback(null, pids);
} }
db.isSortedSetMembers('cid:' + cid + ':pids', pids, function(err, isMembers) {
if (err) { if (!Array.isArray(cid) || cid.length === 1) {
return callback(err); // Single cid
} db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids, function(err, isMembers) {
pids = pids.filter(function(pid, index) { if (err) {
return pid && isMembers[index]; return callback(err);
}
pids = pids.filter(function(pid, index) {
return pid && isMembers[index];
});
callback(null, pids);
}); });
callback(null, pids); } else {
}); // Multiple cids
async.map(cid, function(cid, next) {
Posts.filterPidsByCid(pids, cid, next);
}, function(err, pidsArr) {
if (err) {
return callback(err);
}
callback(null, _.union.apply(_, pidsArr));
});
}
}; };
}; };

@ -38,9 +38,12 @@ function mainRoutes(app, middleware, controllers) {
app.get('/sping', controllers.ping); app.get('/sping', controllers.ping);
} }
function modRoutes(app, middleware, controllers) {
setupPageRoute(app, '/posts/flags', middleware, [], controllers.mods.flagged);
}
function globalModRoutes(app, middleware, controllers) { function globalModRoutes(app, middleware, controllers) {
setupPageRoute(app, '/ip-blacklist', middleware, [], controllers.globalMods.ipBlacklist); setupPageRoute(app, '/ip-blacklist', middleware, [], controllers.globalMods.ipBlacklist);
setupPageRoute(app, '/posts/flags', middleware, [], controllers.globalMods.flagged);
} }
function topicRoutes(app, middleware, controllers) { function topicRoutes(app, middleware, controllers) {
@ -123,6 +126,7 @@ module.exports = function(app, middleware, hotswapIds) {
mainRoutes(router, middleware, controllers); mainRoutes(router, middleware, controllers);
topicRoutes(router, middleware, controllers); topicRoutes(router, middleware, controllers);
postRoutes(router, middleware, controllers); postRoutes(router, middleware, controllers);
modRoutes(router, middleware, controllers);
globalModRoutes(router, middleware, controllers); globalModRoutes(router, middleware, controllers);
tagRoutes(router, middleware, controllers); tagRoutes(router, middleware, controllers);
categoryRoutes(router, middleware, controllers); categoryRoutes(router, middleware, controllers);

@ -145,10 +145,15 @@ module.exports = function(SocketPosts) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
user.isAdminOrGlobalMod(socket.uid, next); async.parallel([
async.apply(user.isAdminOrGlobalMod, socket.uid),
async.apply(user.isModeratorOfAnyCategory, socket.uid)
], function(err, results) {
next(err, results[0] || results[1]);
});
}, },
function (isAdminOrGlobalModerator, next) { function (allowed, next) {
if (!isAdminOrGlobalModerator) { if (!allowed) {
return next(new Error('[[no-privileges]]')); return next(new Error('[[no-privileges]]'));
} }

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var _ = require('underscore');
var groups = require('./groups'); var groups = require('./groups');
var plugins = require('./plugins'); var plugins = require('./plugins');
@ -233,6 +234,19 @@ var meta = require('./meta');
privileges.users.isModerator(uid, cid, callback); privileges.users.isModerator(uid, cid, callback);
}; };
User.isModeratorOfAnyCategory = function(uid, callback) {
// Checks all active categories and determines whether passed-in uid is a mod of any of them
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
async.filter(cids, function(cid, next) {
User.isModerator(uid, cid, function(err, isMod) {
next(!!isMod);
});
}, function(result) {
callback(err, result);
});
});
};
User.isAdministrator = function(uid, callback) { User.isAdministrator = function(uid, callback) {
privileges.users.isAdministrator(uid, callback); privileges.users.isAdministrator(uid, callback);
}; };
@ -277,6 +291,39 @@ var meta = require('./meta');
}); });
}; };
User.getAdminsandGlobalModsandModerators = function(callback) {
async.parallel([
async.apply(groups.getMembers, 'administrators', 0, -1),
async.apply(groups.getMembers, 'Global Moderators', 0, -1),
async.apply(User.getModeratorUids)
], function(err, results) {
if (err) {
return callback(err);
}
User.getUsersData(_.union.apply(_, results), callback);
});
};
User.getModeratorUids = function(callback) {
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
function(cids, next) {
var groupNames = cids.map(function(cid) {
return 'cid:' + cid + ':privileges:mods';
});
groups.getMembersOfGroups(groupNames, function(err, memberSets) {
if (err) {
return next(err);
}
next(null, _.union.apply(_, memberSets));
});
}
], callback);
};
User.addInterstitials = function(callback) { User.addInterstitials = function(callback) {
plugins.registerHook('core', { plugins.registerHook('core', {
hook: 'filter:register.interstitial', hook: 'filter:register.interstitial',

Loading…
Cancel
Save