From 4903585d8823b94886a8a2126f02cb7149e9a592 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Thu, 15 Jun 2017 14:02:51 -0400 Subject: [PATCH] refactor userPrivileges and groupPrivileges --- src/privileges/categories.js | 65 ++++++++---------------------------- test/categories.js | 9 ++++- 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/src/privileges/categories.js b/src/privileges/categories.js index 977485e976..4ec3eacaf3 100644 --- a/src/privileges/categories.js +++ b/src/privileges/categories.js @@ -350,59 +350,22 @@ module.exports = function (privileges) { }; privileges.categories.userPrivileges = function (cid, uid, callback) { - async.parallel({ - find: async.apply(groups.isMember, uid, 'cid:' + cid + ':privileges:find'), - read: function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:read', next); - }, - 'topics:create': function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:topics:create', next); - }, - 'topics:read': function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:topics:read', next); - }, - 'topics:reply': function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:topics:reply', next); - }, - 'posts:edit': function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:posts:edit', next); - }, - 'posts:delete': function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:posts:delete', next); - }, - 'topics:delete': function (next) { - groups.isMember(uid, 'cid:' + cid + ':privileges:topics:delete', next); - }, - mods: function (next) { - user.isModerator(uid, cid, next); - }, - }, callback); + var tasks = {}; + + privileges.userPrivilegeList.forEach(function (privilege) { + tasks[privilege] = async.apply(groups.isMember, uid, 'cid:' + cid + ':privileges:' + privilege); + }); + + async.parallel(tasks, callback); }; privileges.categories.groupPrivileges = function (cid, groupName, callback) { - async.parallel({ - 'groups:find': async.apply(groups.isMember, groupName, 'cid:' + cid + ':privileges:groups:find'), - 'groups:read': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:read', next); - }, - 'groups:topics:create': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:topics:create', next); - }, - 'groups:topics:reply': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:topics:reply', next); - }, - 'groups:posts:edit': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:posts:edit', next); - }, - 'groups:posts:delete': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:posts:delete', next); - }, - 'groups:topics:delete': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:topics:delete', next); - }, - 'groups:topics:read': function (next) { - groups.isMember(groupName, 'cid:' + cid + ':privileges:groups:topics:read', next); - }, - }, callback); + var tasks = {}; + + privileges.groupPrivilegeList.forEach(function (privilege) { + tasks[privilege] = async.apply(groups.isMember, groupName, 'cid:' + cid + ':privileges:' + privilege); + }); + + async.parallel(tasks, callback); }; }; diff --git a/test/categories.js b/test/categories.js index 9c481fc317..18e125f3c7 100644 --- a/test/categories.js +++ b/test/categories.js @@ -640,7 +640,6 @@ describe('Categories', function () { assert.ifError(err); assert.deepEqual(data, { find: false, - mods: false, 'posts:delete': false, read: false, 'topics:reply': false, @@ -648,6 +647,10 @@ describe('Categories', function () { 'topics:create': false, 'topics:delete': false, 'posts:edit': false, + 'upload:post:file': false, + 'upload:post:image': false, + purge: false, + moderate: false, }); done(); @@ -666,6 +669,10 @@ describe('Categories', function () { 'groups:posts:delete': true, 'groups:read': true, 'groups:topics:read': true, + 'groups:upload:post:file': false, + 'groups:upload:post:image': true, + 'groups:purge': false, + 'groups:moderate': false, }); done();