diff --git a/src/privileges/categories.js b/src/privileges/categories.js index 972b79f1d5..98d94c3aff 100644 --- a/src/privileges/categories.js +++ b/src/privileges/categories.js @@ -157,15 +157,10 @@ module.exports = function(privileges) { }; privileges.categories.get = function(cid, uid, callback) { + var privs = ['topics:create', 'topics:read', 'read']; async.parallel({ - 'topics:create': function(next) { - helpers.isUserAllowedTo('topics:create', uid, [cid], next); - }, - 'topics:read': function(next) { - helpers.isUserAllowedTo('topics:read', uid, [cid], next); - }, - read: function(next) { - helpers.isUserAllowedTo('read', uid, [cid], next); + privileges: function(next) { + helpers.isUserAllowedTo(privs, uid, cid, next); }, isAdministrator: function(next) { user.isAdministrator(uid, next); @@ -177,17 +172,17 @@ module.exports = function(privileges) { if (err) { return callback(err); } - + var privData = _.object(privs, results.privileges); var isAdminOrMod = results.isAdministrator || results.isModerator; plugins.fireHook('filter:privileges.categories.get', { + 'topics:create': privData['topics:create'] || isAdminOrMod, + 'topics:read': privData['topics:read'] || isAdminOrMod, + read: privData.read || isAdminOrMod, cid: cid, uid: uid, - 'topics:create': results['topics:create'][0] || isAdminOrMod, - 'topics:read': results['topics:read'][0] || isAdminOrMod, editable: isAdminOrMod, view_deleted: isAdminOrMod, - read: results.read[0] || isAdminOrMod, isAdminOrMod: isAdminOrMod }, callback); }); diff --git a/src/privileges/helpers.js b/src/privileges/helpers.js index 7b5cb0c561..15853fde2c 100644 --- a/src/privileges/helpers.js +++ b/src/privileges/helpers.js @@ -16,9 +16,19 @@ helpers.some = function(tasks, callback) { }); }; -helpers.isUserAllowedTo = function(privilege, uid, cids, callback) { +helpers.isUserAllowedTo = function(privilege, uid, cid, callback) { + if (Array.isArray(privilege) && !Array.isArray(cid)) { + isUserAllowedToPrivileges(privilege, uid, cid, callback); + } else if (Array.isArray(cid) && !Array.isArray(privilege)) { + isUserAllowedToCids(privilege, uid, cid, callback); + } else { + return callback(new Error('[[error:invalid-data]]')); + } +}; + +function isUserAllowedToCids(privilege, uid, cids, callback) { if (parseInt(uid, 10) === 0) { - return isGuestAllowedTo(privilege, cids, callback); + return isGuestAllowedToCids(privilege, cids, callback); } var userKeys = [], groupKeys = []; @@ -46,7 +56,40 @@ helpers.isUserAllowedTo = function(privilege, uid, cids, callback) { callback(null, result); }); -}; +} + +function isUserAllowedToPrivileges(privileges, uid, cid, callback) { + if (parseInt(uid, 10) === 0) { + return isGuestAllowedToPrivileges(privileges, cid, callback); + } + + var userKeys = [], groupKeys = []; + for (var i=0; i