|
|
|
@ -10,21 +10,60 @@ const helpers = require('./helpers');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const utils = require('../utils');
|
|
|
|
|
|
|
|
|
|
module.exports = function (privileges) {
|
|
|
|
|
privileges.categories = {};
|
|
|
|
|
|
|
|
|
|
// Method used in admin/category controller to show all users/groups with privs in that given cid
|
|
|
|
|
privileges.categories.list = async function (cid) {
|
|
|
|
|
const privsCategories = module.exports;
|
|
|
|
|
|
|
|
|
|
privsCategories.privilegeLabels = [
|
|
|
|
|
{ name: '[[admin/manage/privileges:find-category]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:access-category]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:access-topics]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:create-topics]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:reply-to-topics]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:tag-topics]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:edit-posts]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:view-edit-history]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:delete-posts]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:upvote-posts]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:downvote-posts]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:delete-topics]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:view_deleted]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:purge]]' },
|
|
|
|
|
{ name: '[[admin/manage/privileges:moderate]]' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
privsCategories.userPrivilegeList = [
|
|
|
|
|
'find',
|
|
|
|
|
'read',
|
|
|
|
|
'topics:read',
|
|
|
|
|
'topics:create',
|
|
|
|
|
'topics:reply',
|
|
|
|
|
'topics:tag',
|
|
|
|
|
'posts:edit',
|
|
|
|
|
'posts:history',
|
|
|
|
|
'posts:delete',
|
|
|
|
|
'posts:upvote',
|
|
|
|
|
'posts:downvote',
|
|
|
|
|
'topics:delete',
|
|
|
|
|
'posts:view_deleted',
|
|
|
|
|
'purge',
|
|
|
|
|
'moderate',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
privsCategories.groupPrivilegeList = privsCategories.userPrivilegeList.map(privilege => `groups:${privilege}`);
|
|
|
|
|
|
|
|
|
|
privsCategories.privilegeList = privsCategories.userPrivilegeList.concat(privsCategories.groupPrivilegeList);
|
|
|
|
|
|
|
|
|
|
// Method used in admin/category controller to show all users/groups with privs in that given cid
|
|
|
|
|
privsCategories.list = async function (cid) {
|
|
|
|
|
async function getLabels() {
|
|
|
|
|
return await utils.promiseParallel({
|
|
|
|
|
users: plugins.hooks.fire('filter:privileges.list_human', privileges.privilegeLabels.slice()),
|
|
|
|
|
groups: plugins.hooks.fire('filter:privileges.groups.list_human', privileges.privilegeLabels.slice()),
|
|
|
|
|
users: plugins.hooks.fire('filter:privileges.list_human', privsCategories.privilegeLabels.slice()),
|
|
|
|
|
groups: plugins.hooks.fire('filter:privileges.groups.list_human', privsCategories.privilegeLabels.slice()),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const keys = await utils.promiseParallel({
|
|
|
|
|
users: plugins.hooks.fire('filter:privileges.list', privileges.userPrivilegeList.slice()),
|
|
|
|
|
groups: plugins.hooks.fire('filter:privileges.groups.list', privileges.groupPrivilegeList.slice()),
|
|
|
|
|
users: plugins.hooks.fire('filter:privileges.list', privsCategories.userPrivilegeList.slice()),
|
|
|
|
|
groups: plugins.hooks.fire('filter:privileges.groups.list', privsCategories.groupPrivilegeList.slice()),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const payload = await utils.promiseParallel({
|
|
|
|
@ -36,13 +75,13 @@ module.exports = function (privileges) {
|
|
|
|
|
|
|
|
|
|
// This is a hack because I can't do {labels.users.length} to echo the count in templates.js
|
|
|
|
|
payload.columnCountUser = payload.labels.users.length + 3;
|
|
|
|
|
payload.columnCountUserOther = payload.labels.users.length - privileges.privilegeLabels.length;
|
|
|
|
|
payload.columnCountUserOther = payload.labels.users.length - privsCategories.privilegeLabels.length;
|
|
|
|
|
payload.columnCountGroup = payload.labels.groups.length + 3;
|
|
|
|
|
payload.columnCountGroupOther = payload.labels.groups.length - privileges.privilegeLabels.length;
|
|
|
|
|
payload.columnCountGroupOther = payload.labels.groups.length - privsCategories.privilegeLabels.length;
|
|
|
|
|
return payload;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.get = async function (cid, uid) {
|
|
|
|
|
privsCategories.get = async function (cid, uid) {
|
|
|
|
|
const privs = ['topics:create', 'topics:read', 'topics:tag', 'read'];
|
|
|
|
|
|
|
|
|
|
const [userPrivileges, isAdministrator, isModerator] = await Promise.all([
|
|
|
|
@ -63,9 +102,9 @@ module.exports = function (privileges) {
|
|
|
|
|
view_deleted: isAdminOrMod,
|
|
|
|
|
isAdminOrMod: isAdminOrMod,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.isAdminOrMod = async function (cid, uid) {
|
|
|
|
|
privsCategories.isAdminOrMod = async function (cid, uid) {
|
|
|
|
|
if (parseInt(uid, 10) <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -74,9 +113,9 @@ module.exports = function (privileges) {
|
|
|
|
|
user.isModerator(uid, cid),
|
|
|
|
|
]);
|
|
|
|
|
return isAdmin || isMod;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.isUserAllowedTo = async function (privilege, cid, uid) {
|
|
|
|
|
privsCategories.isUserAllowedTo = async function (privilege, cid, uid) {
|
|
|
|
|
if ((Array.isArray(privilege) && !privilege.length) || (Array.isArray(cid) && !cid.length)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
@ -89,21 +128,21 @@ module.exports = function (privileges) {
|
|
|
|
|
return Array.isArray(cid) ? results : results[0];
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.can = async function (privilege, cid, uid) {
|
|
|
|
|
privsCategories.can = async function (privilege, cid, uid) {
|
|
|
|
|
if (!cid) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const [disabled, isAdmin, isAllowed] = await Promise.all([
|
|
|
|
|
categories.getCategoryField(cid, 'disabled'),
|
|
|
|
|
user.isAdministrator(uid),
|
|
|
|
|
privileges.categories.isUserAllowedTo(privilege, cid, uid),
|
|
|
|
|
privsCategories.isUserAllowedTo(privilege, cid, uid),
|
|
|
|
|
]);
|
|
|
|
|
return !disabled && (isAllowed || isAdmin);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.filterCids = async function (privilege, cids, uid) {
|
|
|
|
|
privsCategories.filterCids = async function (privilege, cids, uid) {
|
|
|
|
|
if (!Array.isArray(cids) || !cids.length) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
@ -117,18 +156,18 @@ module.exports = function (privileges) {
|
|
|
|
|
return cids.filter(
|
|
|
|
|
(cid, index) => !!cid && !categoryData[index].disabled && (allowedTo[index] || isAdmin)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.getBase = async function (privilege, cids, uid) {
|
|
|
|
|
privsCategories.getBase = async function (privilege, cids, uid) {
|
|
|
|
|
return await utils.promiseParallel({
|
|
|
|
|
categories: categories.getCategoriesFields(cids, ['disabled']),
|
|
|
|
|
allowedTo: helpers.isAllowedTo(privilege, uid, cids),
|
|
|
|
|
view_deleted: helpers.isAllowedTo('posts:view_deleted', uid, cids),
|
|
|
|
|
isAdmin: user.isAdministrator(uid),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.filterUids = async function (privilege, cid, uids) {
|
|
|
|
|
privsCategories.filterUids = async function (privilege, cid, uids) {
|
|
|
|
|
if (!uids.length) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
@ -140,47 +179,38 @@ module.exports = function (privileges) {
|
|
|
|
|
user.isAdministrator(uids),
|
|
|
|
|
]);
|
|
|
|
|
return uids.filter((uid, index) => allowedTo[index] || isAdmins[index]);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.give = async function (privileges, cid, members) {
|
|
|
|
|
privsCategories.give = async function (privileges, cid, members) {
|
|
|
|
|
await helpers.giveOrRescind(groups.join, privileges, cid, members);
|
|
|
|
|
plugins.hooks.fire('action:privileges.categories.give', {
|
|
|
|
|
privileges: privileges,
|
|
|
|
|
cids: Array.isArray(cid) ? cid : [cid],
|
|
|
|
|
members: Array.isArray(members) ? members : [members],
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.rescind = async function (privileges, cid, members) {
|
|
|
|
|
privsCategories.rescind = async function (privileges, cid, members) {
|
|
|
|
|
await helpers.giveOrRescind(groups.leave, privileges, cid, members);
|
|
|
|
|
plugins.hooks.fire('action:privileges.categories.rescind', {
|
|
|
|
|
privileges: privileges,
|
|
|
|
|
cids: Array.isArray(cid) ? cid : [cid],
|
|
|
|
|
members: Array.isArray(members) ? members : [members],
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.canMoveAllTopics = async function (currentCid, targetCid, uid) {
|
|
|
|
|
privsCategories.canMoveAllTopics = async function (currentCid, targetCid, uid) {
|
|
|
|
|
const [isAdmin, isModerators] = await Promise.all([
|
|
|
|
|
user.isAdministrator(uid),
|
|
|
|
|
user.isModerator(uid, [currentCid, targetCid]),
|
|
|
|
|
]);
|
|
|
|
|
return isAdmin || !isModerators.includes(false);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.userPrivileges = async function (cid, uid) {
|
|
|
|
|
const tasks = {};
|
|
|
|
|
privileges.userPrivilegeList.forEach((privilege) => {
|
|
|
|
|
tasks[privilege] = groups.isMember(uid, `cid:${cid}:privileges:${privilege}`);
|
|
|
|
|
});
|
|
|
|
|
return await utils.promiseParallel(tasks);
|
|
|
|
|
};
|
|
|
|
|
privsCategories.userPrivileges = async function (cid, uid) {
|
|
|
|
|
return await helpers.userOrGroupPrivileges(cid, uid, privsCategories.userPrivilegeList);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
privileges.categories.groupPrivileges = async function (cid, groupName) {
|
|
|
|
|
const tasks = {};
|
|
|
|
|
privileges.groupPrivilegeList.forEach((privilege) => {
|
|
|
|
|
tasks[privilege] = groups.isMember(groupName, `cid:${cid}:privileges:${privilege}`);
|
|
|
|
|
});
|
|
|
|
|
return await utils.promiseParallel(tasks);
|
|
|
|
|
};
|
|
|
|
|
privsCategories.groupPrivileges = async function (cid, groupName) {
|
|
|
|
|
return await helpers.userOrGroupPrivileges(cid, groupName, privsCategories.groupPrivilegeList);
|
|
|
|
|
};
|
|
|
|
|