2014-05-15 10:38:02 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
const _ = require('lodash');
|
2014-05-15 10:38:02 -04:00
|
|
|
|
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');
|
2014-05-15 10:38:02 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (privileges) {
|
2014-05-15 10:38:02 -04:00
|
|
|
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.fireHook('filter:privileges.list_human', privileges.privilegeLabels.slice()),
|
|
|
|
|
groups: plugins.fireHook('filter:privileges.groups.list_human', privileges.privilegeLabels.slice()),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = await utils.promiseParallel({
|
|
|
|
|
labels: getLabels(),
|
|
|
|
|
users: helpers.getUserPrivileges(cid, 'filter:privileges.list', privileges.userPrivilegeList),
|
|
|
|
|
groups: helpers.getGroupPrivileges(cid, 'filter:privileges.groups.list', privileges.groupPrivilegeList),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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 + 2;
|
|
|
|
|
payload.columnCountUserOther = payload.labels.users.length - privileges.privilegeLabels.length;
|
|
|
|
|
payload.columnCountGroup = payload.labels.groups.length + 2;
|
|
|
|
|
payload.columnCountGroupOther = payload.labels.groups.length - privileges.privilegeLabels.length;
|
|
|
|
|
return payload;
|
2015-03-25 15:42:15 -04:00
|
|
|
};
|
|
|
|
|
|
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.isUserAllowedTo(privs, uid, cid),
|
|
|
|
|
user.isAdministrator(uid),
|
|
|
|
|
user.isModerator(uid, cid),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const privData = _.zipObject(privs, userPrivileges);
|
|
|
|
|
const isAdminOrMod = isAdministrator || isModerator;
|
|
|
|
|
|
|
|
|
|
return await plugins.fireHook('filter:privileges.categories.get', {
|
|
|
|
|
'topics:create': privData['topics:create'] || isAdministrator,
|
|
|
|
|
'topics:read': privData['topics:read'] || isAdministrator,
|
|
|
|
|
'topics:tag': privData['topics:tag'] || isAdministrator,
|
|
|
|
|
read: privData.read || isAdministrator,
|
|
|
|
|
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;
|
2015-10-28 15:10:27 -04:00
|
|
|
}
|
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) {
|
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
|
|
|
}
|
2019-07-20 22:12:22 -04:00
|
|
|
const results = await helpers.isUserAllowedTo(privilege, uid, Array.isArray(cid) ? cid : [cid]);
|
|
|
|
|
|
|
|
|
|
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);
|
2014-07-29 21:51:46 -04:00
|
|
|
};
|
|
|
|
|
|
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 [];
|
2014-07-31 17:29:20 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 20:00:05 -04:00
|
|
|
cids = _.uniq(cids);
|
2019-07-20 22:12:22 -04:00
|
|
|
const results = await privileges.categories.getBase(privilege, cids, uid);
|
2019-09-26 21:55:49 -04:00
|
|
|
return cids.filter((cid, index) => !!cid && !results.categories[index].disabled && (results.allowedTo[index] || results.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.isUserAllowedTo(privilege, uid, cids),
|
|
|
|
|
isAdmin: user.isAdministrator(uid),
|
|
|
|
|
});
|
2014-05-16 15:47:04 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
privileges.categories.filterUids = async function (privilege, cid, uids) {
|
2014-09-09 15:19:57 -04:00
|
|
|
if (!uids.length) {
|
2019-07-20 22:12:22 -04:00
|
|
|
return [];
|
2014-09-09 15:19:57 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 19:05:13 -04:00
|
|
|
uids = _.uniq(uids);
|
2016-08-27 12:58:08 +03:00
|
|
|
|
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]);
|
2014-09-09 15:19:57 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
privileges.categories.give = async function (privileges, cid, groupName) {
|
|
|
|
|
await helpers.giveOrRescind(groups.join, privileges, cid, groupName);
|
2014-11-11 22:58:34 -05:00
|
|
|
};
|
|
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
privileges.categories.rescind = async function (privileges, cid, groupName) {
|
|
|
|
|
await helpers.giveOrRescind(groups.leave, privileges, cid, groupName);
|
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 = {};
|
2017-06-15 14:02:51 -04:00
|
|
|
privileges.userPrivilegeList.forEach(function (privilege) {
|
2019-07-20 22:12:22 -04:00
|
|
|
tasks[privilege] = groups.isMember(uid, 'cid:' + cid + ':privileges:' + privilege);
|
2017-06-15 14:02:51 -04:00
|
|
|
});
|
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 = {};
|
2017-06-15 14:02:51 -04:00
|
|
|
privileges.groupPrivilegeList.forEach(function (privilege) {
|
2019-07-20 22:12:22 -04:00
|
|
|
tasks[privilege] = groups.isMember(groupName, 'cid:' + cid + ':privileges:' + privilege);
|
2017-06-15 14:02:51 -04:00
|
|
|
});
|
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
|
|
|
};
|