Skip to content

Node.js based forum software built for the modern web

feat: allow array of privileges for categories.can

Barış Soner Uşaklı7599a1257214
diff --git a/src/privileges/categories.js b/src/privileges/categories.js
index 61fda1317a..d85f891d95 100644
--- a/src/privileges/categories.js
+++ b/src/privileges/categories.js
@@ -131,18 +131,21 @@ privsCategories.isAdminOrMod = async function (cid, uid) {
};
privsCategories.isUserAllowedTo = async function (privilege, cid, uid) {
- if ((Array.isArray(privilege) && !privilege.length) || (Array.isArray(cid) && !cid.length)) {
+ const arrayOfPrivileges = Array.isArray(privilege);
+ const arrayOfCids = Array.isArray(cid);
+ const returnAsArray = arrayOfPrivileges || arrayOfCids;
+ if ((arrayOfPrivileges && !privilege.length) || (arrayOfCids && !cid.length)) {
return [];
}
if (!cid) {
return false;
}
- const results = await helpers.isAllowedTo(privilege, uid, Array.isArray(cid) ? cid : [cid]);
- if (Array.isArray(results) && results.length) {
- return Array.isArray(cid) ? results : results[0];
+ if (!arrayOfCids && !arrayOfPrivileges) {
+ cid = [cid];
}
- return false;
+ const results = await helpers.isAllowedTo(privilege, uid, cid);
+ return returnAsArray ? results : results[0];
};
privsCategories.can = async function (privilege, cid, uid) {
@@ -155,6 +158,10 @@ privsCategories.can = async function (privilege, cid, uid) {
user.isAdministrator(uid),
privsCategories.isUserAllowedTo(privilege, cid, uid),
]);
+
+ if (Array.isArray(privilege)) {
+ return isAllowed.map(allowed => !disabled && (allowed || isAdmin));
+ }
return !disabled && (isAllowed || isAdmin);
};
diff --git a/test/categories.js b/test/categories.js
index c204a00768..bf70205089 100644
--- a/test/categories.js
+++ b/test/categories.js
@@ -483,7 +483,19 @@ describe('Categories', () => {
it('should remove privilege', async () => {
await apiCategories.setPrivilege({ uid: adminUid }, { cid: categoryObj.cid, privilege: 'groups:topics:delete', set: false, member: 'registered-users' });
const canDeleteTopics = await privileges.categories.can('topics:delete', categoryObj.cid, posterUid);
- assert(!canDeleteTopics);
+ assert.strictEqual(canDeleteTopics, false);
+ });
+
+ it('should get an array of privileges for a category', async () => {
+ const privilegesArray = await privileges.categories.can(['topics:create', 'topics:delete'], categoryObj.cid, posterUid);
+ assert.deepStrictEqual(privilegesArray, [true, false]);
+ });
+
+ it('should error if both cid and privilege are arrays', async () => {
+ assert.rejects(
+ privileges.categories.can(['topics:create', 'topics:delete'], [categoryObj.cid], posterUid),
+ { message: '[[error:invalid-data]]' },
+ );
});
it('should get privilege settings', async () => {