updating privilege handling to fire two new hooks, filter:privileges.topics.get and filter:category.topics.get

v1.18.x
Julian Lam 11 years ago
parent eada3daf9f
commit c0fc7e9ca4

@ -2,14 +2,16 @@
var async = require('async'), var async = require('async'),
db = require('../database'), db = require('../database'),
topics = require('../topics'); topics = require('../topics'),
plugins = require('../plugins');
module.exports = function(Categories) { module.exports = function(Categories) {
Categories.getCategoryTopics = function(data, callback) { Categories.getCategoryTopics = function(data, callback) {
var tids; var tids;
async.waterfall([ async.waterfall([
function(next) { async.apply(plugins.fireHook, 'filter:category.topics.get', data),
function(data, next) {
Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next); Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
}, },
function(topicIds, next) { function(topicIds, next) {

@ -8,60 +8,48 @@ var async = require('async'),
user = require('../user'), user = require('../user'),
helpers = require('./helpers'), helpers = require('./helpers'),
groups = require('../groups'), groups = require('../groups'),
categories = require('../categories'); categories = require('../categories'),
plugins = require('../plugins');
module.exports = function(privileges) { module.exports = function(privileges) {
privileges.topics = {}; privileges.topics = {};
privileges.topics.get = function(tid, uid, callback) { privileges.topics.get = function(tid, uid, callback) {
async.waterfall([
topics.getTopicField(tid, 'cid', function(err, cid) { async.apply(topics.getTopicField, tid, 'cid'),
if (err) { function(cid, next) {
return callback(err);
}
async.parallel({ async.parallel({
'topics:reply': function(next) { 'topics:reply': async.apply(helpers.isUserAllowedTo, 'topics:reply', uid, [cid]),
helpers.isUserAllowedTo('topics:reply', uid, [cid], next); read: async.apply(helpers.isUserAllowedTo, 'read', uid, [cid]),
}, isOwner: async.apply(topics.isOwner, tid, uid),
read: function(next) { manage_topic: async.apply(helpers.hasEnoughReputationFor, 'privileges:manage_topic', uid),
helpers.isUserAllowedTo('read', uid, [cid], next); isAdministrator: async.apply(user.isAdministrator, uid),
}, isModerator: async.apply(user.isModerator, uid, cid),
isOwner: function(next) { disabled: async.apply(categories.getCategoryField, cid, 'disabled')
topics.isOwner(tid, uid, next); }, next);
},
manage_topic: function(next) {
helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next);
},
isAdministrator: function(next) {
user.isAdministrator(uid, next);
},
isModerator: function(next) {
user.isModerator(uid, cid, next);
},
disabled: function(next) {
categories.getCategoryField(cid, 'disabled', next);
} }
}, function(err, results) { ], function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var disabled = parseInt(results.disabled, 10) === 1; var disabled = parseInt(results.disabled, 10) === 1;
var isAdminOrMod = results.isAdministrator || results.isModerator; var isAdminOrMod = results.isAdministrator || results.isModerator;
var editable = isAdminOrMod || results.manage_topic; var editable = isAdminOrMod || results.manage_topic;
var deletable = isAdminOrMod || results.isOwner; var deletable = isAdminOrMod || results.isOwner;
callback(null, { plugins.fireHook('filter:privileges.topics.get', {
'topics:reply': results['topics:reply'][0] || isAdminOrMod, 'topics:reply': results['topics:reply'][0] || isAdminOrMod,
read: results.read[0] || isAdminOrMod, read: results.read[0] || isAdminOrMod,
view_thread_tools: editable || deletable, view_thread_tools: editable || deletable,
editable: editable, editable: editable,
deletable: deletable, deletable: deletable,
view_deleted: isAdminOrMod || results.manage_topic || results.isOwner, view_deleted: isAdminOrMod || results.manage_topic || results.isOwner,
disabled: disabled disabled: disabled,
}); tid: tid,
}); uid: uid
}, callback);
}); });
}; };

Loading…
Cancel
Save