Files
NodeBB/src/privileges/categories.js

187 lines
6.1 KiB
JavaScript
Raw Normal View History

'use strict';
2019-07-20 22:12:22 -04:00
const _ = require('lodash');
2019-07-20 22:12:22 -04:00
const categories = require('../categories');
const user = require('../user');
const groups = require('../groups');
const helpers = require('./helpers');
const plugins = require('../plugins');
const utils = require('../utils');
module.exports = function (privileges) {
privileges.categories = {};
2019-07-20 22:12:22 -04:00
// Method used in admin/category controller to show all users/groups with privs in that given cid
privileges.categories.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()),
2019-07-20 22:12:22 -04:00
});
}
feat: more discrete commit-on-save instead of commit-on-change w/ confirm modals (#8541) * feat: privileges save button, #8537, WIP * fix: disable firefox autocomplete on privilege form fields * feat: closes #8537 privilege changes commit on save - new language strings for confirmation and success modals/toasts - indeterminate privilege handling (/cc @psychobunny) - added new discard button - both discard and save buttons now have confirmation dialogs * fix(tests): remove duplicate template helper test * fix(tests): broken template helper test * feat: confirm dialogs for all privilege copy actions Also, ability to add user to a privilege table without needing to refresh the privilege table. * feat: group row addition w/o table refresh breaking: helpers.getUserPrivileges and helpers.getGroupPrivileges no longer make socket calls to the following hooks: - filter:privileges.list, filter:privileges.admin.list, filter:privileges.global.list, filter:privileges.groups.list, filter:privileges.admin.groups.list, filter:privileges.gloval.groups.list The filters are still called, but done before the helper method is called, and the results are passed in instead. This change should only affect you if you directly call the helper methods, otherwise the change is transparent. * fix: stale ajaxify data on privilege category switch * fix: implicit privileges not showing for user privs * fix: groups, not group, also fix tests * fix(tests): again * fix: wrong tpl rendered when adding group to global priv table
2020-08-03 20:42:45 -04:00
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()),
feat: more discrete commit-on-save instead of commit-on-change w/ confirm modals (#8541) * feat: privileges save button, #8537, WIP * fix: disable firefox autocomplete on privilege form fields * feat: closes #8537 privilege changes commit on save - new language strings for confirmation and success modals/toasts - indeterminate privilege handling (/cc @psychobunny) - added new discard button - both discard and save buttons now have confirmation dialogs * fix(tests): remove duplicate template helper test * fix(tests): broken template helper test * feat: confirm dialogs for all privilege copy actions Also, ability to add user to a privilege table without needing to refresh the privilege table. * feat: group row addition w/o table refresh breaking: helpers.getUserPrivileges and helpers.getGroupPrivileges no longer make socket calls to the following hooks: - filter:privileges.list, filter:privileges.admin.list, filter:privileges.global.list, filter:privileges.groups.list, filter:privileges.admin.groups.list, filter:privileges.gloval.groups.list The filters are still called, but done before the helper method is called, and the results are passed in instead. This change should only affect you if you directly call the helper methods, otherwise the change is transparent. * fix: stale ajaxify data on privilege category switch * fix: implicit privileges not showing for user privs * fix: groups, not group, also fix tests * fix(tests): again * fix: wrong tpl rendered when adding group to global priv table
2020-08-03 20:42:45 -04:00
});
2019-07-20 22:12:22 -04:00
const payload = await utils.promiseParallel({
labels: getLabels(),
feat: more discrete commit-on-save instead of commit-on-change w/ confirm modals (#8541) * feat: privileges save button, #8537, WIP * fix: disable firefox autocomplete on privilege form fields * feat: closes #8537 privilege changes commit on save - new language strings for confirmation and success modals/toasts - indeterminate privilege handling (/cc @psychobunny) - added new discard button - both discard and save buttons now have confirmation dialogs * fix(tests): remove duplicate template helper test * fix(tests): broken template helper test * feat: confirm dialogs for all privilege copy actions Also, ability to add user to a privilege table without needing to refresh the privilege table. * feat: group row addition w/o table refresh breaking: helpers.getUserPrivileges and helpers.getGroupPrivileges no longer make socket calls to the following hooks: - filter:privileges.list, filter:privileges.admin.list, filter:privileges.global.list, filter:privileges.groups.list, filter:privileges.admin.groups.list, filter:privileges.gloval.groups.list The filters are still called, but done before the helper method is called, and the results are passed in instead. This change should only affect you if you directly call the helper methods, otherwise the change is transparent. * fix: stale ajaxify data on privilege category switch * fix: implicit privileges not showing for user privs * fix: groups, not group, also fix tests * fix(tests): again * fix: wrong tpl rendered when adding group to global priv table
2020-08-03 20:42:45 -04:00
users: helpers.getUserPrivileges(cid, keys.users),
groups: helpers.getGroupPrivileges(cid, keys.groups),
2019-07-20 22:12:22 -04:00
});
feat: more discrete commit-on-save instead of commit-on-change w/ confirm modals (#8541) * feat: privileges save button, #8537, WIP * fix: disable firefox autocomplete on privilege form fields * feat: closes #8537 privilege changes commit on save - new language strings for confirmation and success modals/toasts - indeterminate privilege handling (/cc @psychobunny) - added new discard button - both discard and save buttons now have confirmation dialogs * fix(tests): remove duplicate template helper test * fix(tests): broken template helper test * feat: confirm dialogs for all privilege copy actions Also, ability to add user to a privilege table without needing to refresh the privilege table. * feat: group row addition w/o table refresh breaking: helpers.getUserPrivileges and helpers.getGroupPrivileges no longer make socket calls to the following hooks: - filter:privileges.list, filter:privileges.admin.list, filter:privileges.global.list, filter:privileges.groups.list, filter:privileges.admin.groups.list, filter:privileges.gloval.groups.list The filters are still called, but done before the helper method is called, and the results are passed in instead. This change should only affect you if you directly call the helper methods, otherwise the change is transparent. * fix: stale ajaxify data on privilege category switch * fix: implicit privileges not showing for user privs * fix: groups, not group, also fix tests * fix(tests): again * fix: wrong tpl rendered when adding group to global priv table
2020-08-03 20:42:45 -04:00
payload.keys = keys;
2019-07-20 22:12:22 -04:00
// This is a hack because I can't do {labels.users.length} to echo the count in templates.js
2020-11-30 01:12:34 -05:00
payload.columnCountUser = payload.labels.users.length + 3;
2019-07-20 22:12:22 -04:00
payload.columnCountUserOther = payload.labels.users.length - privileges.privilegeLabels.length;
2020-11-30 01:12:34 -05:00
payload.columnCountGroup = payload.labels.groups.length + 3;
2019-07-20 22:12:22 -04:00
payload.columnCountGroupOther = payload.labels.groups.length - privileges.privilegeLabels.length;
return payload;
};
2019-07-20 22:12:22 -04:00
privileges.categories.get = async function (cid, uid) {
const privs = ['topics:create', 'topics:read', 'topics:tag', 'read'];
const [userPrivileges, isAdministrator, isModerator] = await Promise.all([
helpers.isAllowedTo(privs, uid, cid),
2019-07-20 22:12:22 -04:00
user.isAdministrator(uid),
user.isModerator(uid, cid),
]);
const combined = userPrivileges.map(allowed => allowed || isAdministrator);
const privData = _.zipObject(privs, combined);
2019-07-20 22:12:22 -04:00
const isAdminOrMod = isAdministrator || isModerator;
return await plugins.hooks.fire('filter:privileges.categories.get', {
...privData,
2019-07-20 22:12:22 -04:00
cid: cid,
uid: uid,
editable: isAdminOrMod,
view_deleted: isAdminOrMod,
isAdminOrMod: isAdminOrMod,
});
2014-05-15 20:49:47 -04:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.isAdminOrMod = async function (cid, uid) {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
2019-07-20 22:12:22 -04:00
return false;
}
2019-07-20 22:12:22 -04:00
const [isAdmin, isMod] = await Promise.all([
user.isAdministrator(uid),
user.isModerator(uid, cid),
]);
return isAdmin || isMod;
2015-09-15 18:21:17 -04:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.isUserAllowedTo = async function (privilege, cid, uid) {
Categories refactor (#9257) * feat: wip categories pagination * feat: add subCategoriesPerPage setting * feat: add load more sub categories button to category page * fix: openapi spec * feat: show sub categories left on category page hide button when no more categories left * breaking: rename categories to allCategories on /search categories contains the search results * fix: spec * refactor: remove cidsPerPage * fix: tests * feat: use component for subcategories * fix: prevent negative subCategoriesLeft * feat: new category filter/search WIP * feat: remove categories from /tag * fix: dont load all categories when showing move modal * feat: allow adding custom categories to list * breaking: dont load entire category tree on post queue removed unused code add hooks to filter/selector add options to filter/selector * feat: make selector modal work again * feat: replace old search module * fix: topic move selector * feat: dont load all categories on create category modal * fix: fix more categorySelectors * feat: dont load entire category tree on group details page * feat: dont load all categories on home page and user settings page * feat: add pagination to /user/:userslug/categories * fix: update schemas * fix: more tests * fix: test * feat: flags page, dont return entire category tree * fix: flag test * feat: categories manage page dont load all categories allow changing root category clear caches properly * fix: spec * feat: admins&mods page dont load all categories * fix: spec * fix: dont load all children when opening dropdown * fix: on search results dont return all children * refactor: pass all options, rename options.cids to options.selectedCids * fix: #9266 * fix: index 0 * fix: spec * feat: #9265, add setObjectBulk * refactor: shoter updateOrder * feat: selectors on categories/category * fix: tests and search filter * fix: category update test * feat: pagination on acp categories page show order in set order modal * fix: allow drag&drop on pages > 1 in /admin/manage/categories * fix: teasers for deep nested categories fix sub category display on /category page * fix: spec * refactor: use eslint-disable-next-line * refactor: shorter
2021-02-07 15:09:52 -05:00
if ((Array.isArray(privilege) && !privilege.length) || (Array.isArray(cid) && !cid.length)) {
return [];
}
2015-09-16 08:35:40 -04:00
if (!cid) {
2019-07-20 22:12:22 -04:00
return false;
2015-09-16 08:35:40 -04:00
}
const results = await helpers.isAllowedTo(privilege, uid, Array.isArray(cid) ? cid : [cid]);
2019-07-20 22:12:22 -04:00
if (Array.isArray(results) && results.length) {
return Array.isArray(cid) ? results : results[0];
2018-12-04 15:29:50 -05:00
}
2019-07-20 22:12:22 -04:00
return false;
2015-09-16 08:35:40 -04:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.can = async function (privilege, cid, uid) {
2015-02-24 13:02:58 -05:00
if (!cid) {
2019-07-20 22:12:22 -04:00
return false;
2015-02-24 13:02:58 -05:00
}
2019-07-20 22:12:22 -04:00
const [disabled, isAdmin, isAllowed] = await Promise.all([
categories.getCategoryField(cid, 'disabled'),
user.isAdministrator(uid),
privileges.categories.isUserAllowedTo(privilege, cid, uid),
]);
return !disabled && (isAllowed || isAdmin);
};
2019-07-20 22:12:22 -04:00
privileges.categories.filterCids = async function (privilege, cids, uid) {
2014-11-09 00:33:26 -05:00
if (!Array.isArray(cids) || !cids.length) {
2019-07-20 22:12:22 -04:00
return [];
}
2017-06-25 20:00:05 -04:00
cids = _.uniq(cids);
2021-01-06 21:25:32 -05:00
const [categoryData, allowedTo, isAdmin] = await Promise.all([
categories.getCategoriesFields(cids, ['disabled']),
helpers.isAllowedTo(privilege, uid, cids),
user.isAdministrator(uid),
]);
return cids.filter(
(cid, index) => !!cid && !categoryData[index].disabled && (allowedTo[index] || isAdmin)
);
2016-04-29 20:35:49 +03:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.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),
2019-07-20 22:12:22 -04:00
isAdmin: user.isAdministrator(uid),
});
};
2019-07-20 22:12:22 -04:00
privileges.categories.filterUids = async function (privilege, cid, uids) {
if (!uids.length) {
2019-07-20 22:12:22 -04:00
return [];
}
2017-06-25 19:05:13 -04:00
uids = _.uniq(uids);
2019-07-20 22:12:22 -04:00
const [allowedTo, isAdmins] = await Promise.all([
helpers.isUsersAllowedTo(privilege, uids, cid),
user.isAdministrator(uids),
]);
return uids.filter((uid, index) => allowedTo[index] || isAdmins[index]);
};
privileges.categories.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) {
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],
});
2015-09-27 15:02:04 -04:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.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);
2014-05-15 20:49:47 -04:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.userPrivileges = async function (cid, uid) {
const tasks = {};
2021-02-04 00:01:39 -07:00
privileges.userPrivilegeList.forEach((privilege) => {
2021-02-03 23:59:08 -07:00
tasks[privilege] = groups.isMember(uid, `cid:${cid}:privileges:${privilege}`);
});
2019-07-20 22:12:22 -04:00
return await utils.promiseParallel(tasks);
2014-05-15 20:49:47 -04:00
};
2019-07-20 22:12:22 -04:00
privileges.categories.groupPrivileges = async function (cid, groupName) {
const tasks = {};
2021-02-04 00:01:39 -07:00
privileges.groupPrivilegeList.forEach((privilege) => {
2021-02-03 23:59:08 -07:00
tasks[privilege] = groups.isMember(groupName, `cid:${cid}:privileges:${privilege}`);
});
2019-07-20 22:12:22 -04:00
return await utils.promiseParallel(tasks);
2014-05-15 20:49:47 -04:00
};
2017-02-18 02:30:48 -07:00
};