Files
NodeBB/src/privileges/helpers.js

189 lines
6.6 KiB
JavaScript
Raw Normal View History

'use strict';
2019-07-20 22:12:22 -04:00
const _ = require('lodash');
const validator = require('validator');
2017-12-20 14:49:20 -05:00
2019-07-20 22:12:22 -04:00
const groups = require('../groups');
const user = require('../user');
const plugins = require('../plugins');
const translator = require('../translator');
2019-07-20 22:12:22 -04:00
const helpers = module.exports;
2019-07-20 22:12:22 -04:00
const uidToSystemGroup = {
2018-01-31 15:20:17 -05:00
0: 'guests',
'-1': 'spiders',
};
helpers.isUsersAllowedTo = async function (privilege, uids, cid) {
const [hasUserPrivilege, hasGroupPrivilege] = await Promise.all([
groups.isMembers(uids, 'cid:' + cid + ':privileges:' + privilege),
groups.isMembersOfGroupList(uids, 'cid:' + cid + ':privileges:groups:' + privilege),
]);
const allowed = uids.map((uid, index) => hasUserPrivilege[index] || hasGroupPrivilege[index]);
const result = await plugins.hooks.fire('filter:privileges:isUsersAllowedTo', { allowed: allowed, privilege: privilege, uids: uids, cid: cid });
return result.allowed;
};
helpers.isAllowedTo = async function (privilege, uidOrGroupName, cid) {
let allowed;
if (Array.isArray(privilege) && !Array.isArray(cid)) {
allowed = await isAllowedToPrivileges(privilege, uidOrGroupName, cid);
} else if (Array.isArray(cid) && !Array.isArray(privilege)) {
allowed = await isAllowedToCids(privilege, uidOrGroupName, cid);
}
if (allowed) {
({ allowed } = await plugins.hooks.fire('filter:privileges:isAllowedTo', { allowed: allowed, privilege: privilege, uid: uidOrGroupName, cid: cid }));
return allowed;
}
2019-07-20 22:12:22 -04:00
throw new Error('[[error:invalid-data]]');
};
async function isAllowedToCids(privilege, uidOrGroupName, cids) {
if (!privilege) {
return cids.map(() => false);
}
2020-11-13 14:42:44 -05:00
const groupKeys = cids.map(cid => 'cid:' + cid + ':privileges:groups:' + privilege);
// Group handling
if (isNaN(parseInt(uidOrGroupName, 10)) && (uidOrGroupName || '').length) {
2020-11-13 14:42:44 -05:00
return await checkIfAllowedGroup(uidOrGroupName, groupKeys);
}
// User handling
if (parseInt(uidOrGroupName, 10) <= 0) {
return await isSystemGroupAllowedToCids(privilege, uidOrGroupName, cids);
}
2020-11-13 14:42:44 -05:00
const userKeys = cids.map(cid => 'cid:' + cid + ':privileges:' + privilege);
return await checkIfAllowedUser(uidOrGroupName, userKeys, groupKeys);
}
async function isAllowedToPrivileges(privileges, uidOrGroupName, cid) {
2020-11-13 14:42:44 -05:00
const groupKeys = privileges.map(privilege => 'cid:' + cid + ':privileges:groups:' + privilege);
// Group handling
if (isNaN(parseInt(uidOrGroupName, 10)) && (uidOrGroupName || '').length) {
2020-11-13 14:42:44 -05:00
return await checkIfAllowedGroup(uidOrGroupName, groupKeys);
}
// User handling
if (parseInt(uidOrGroupName, 10) <= 0) {
return await isSystemGroupAllowedToPrivileges(privileges, uidOrGroupName, cid);
}
2020-11-13 14:42:44 -05:00
const userKeys = privileges.map(privilege => 'cid:' + cid + ':privileges:' + privilege);
return await checkIfAllowedUser(uidOrGroupName, userKeys, groupKeys);
2017-05-25 21:17:20 -04:00
}
2020-11-13 14:42:44 -05:00
async function checkIfAllowedUser(uid, userKeys, groupKeys) {
2019-07-20 22:12:22 -04:00
const [hasUserPrivilege, hasGroupPrivilege] = await Promise.all([
groups.isMemberOfGroups(uid, userKeys),
groups.isMemberOfGroupsList(uid, groupKeys),
]);
return userKeys.map((key, index) => hasUserPrivilege[index] || hasGroupPrivilege[index]);
}
2020-11-13 14:42:44 -05:00
async function checkIfAllowedGroup(groupName, groupKeys) {
const sets = await Promise.all([
groups.isMemberOfGroups(groupName, groupKeys),
groups.isMemberOfGroups('registered-users', groupKeys),
]);
return groupKeys.map((key, index) => sets[0][index] || sets[1][index]);
}
2019-07-20 22:12:22 -04:00
async function isSystemGroupAllowedToCids(privilege, uid, cids) {
const groupKeys = cids.map(cid => 'cid:' + cid + ':privileges:groups:' + privilege);
2019-07-20 22:12:22 -04:00
return await groups.isMemberOfGroups(uidToSystemGroup[uid], groupKeys);
}
2019-07-20 22:12:22 -04:00
async function isSystemGroupAllowedToPrivileges(privileges, uid, cid) {
const groupKeys = privileges.map(privilege => 'cid:' + cid + ':privileges:groups:' + privilege);
2019-07-20 22:12:22 -04:00
return await groups.isMemberOfGroups(uidToSystemGroup[uid], groupKeys);
}
2017-12-20 14:49:20 -05: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
helpers.getUserPrivileges = async function (cid, userPrivileges) {
2019-07-20 22:12:22 -04:00
let memberSets = await groups.getMembersOfGroups(userPrivileges.map(privilege => 'cid:' + cid + ':privileges:' + privilege));
memberSets = memberSets.map(function (set) {
return set.map(uid => parseInt(uid, 10));
});
2017-12-20 14:49:20 -05:00
2019-07-20 22:12:22 -04:00
const members = _.uniq(_.flatten(memberSets));
2020-12-14 09:20:41 +03:00
const memberData = await user.getUsersFields(members, ['picture', 'username', 'banned']);
2017-12-20 14:49:20 -05:00
2019-07-20 22:12:22 -04:00
memberData.forEach(function (member) {
member.privileges = {};
for (var x = 0, numPrivs = userPrivileges.length; x < numPrivs; x += 1) {
member.privileges[userPrivileges[x]] = memberSets[x].includes(parseInt(member.uid, 10));
}
});
2017-12-20 14:49:20 -05:00
2019-07-20 22:12:22 -04:00
return memberData;
2017-12-20 14:49:20 -05: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
helpers.getGroupPrivileges = async function (cid, groupPrivileges) {
2019-07-20 22:12:22 -04:00
const [memberSets, allGroupNames] = await Promise.all([
groups.getMembersOfGroups(groupPrivileges.map(privilege => 'cid:' + cid + ':privileges:' + privilege)),
groups.getGroups('groups:createtime', 0, -1),
]);
const uniqueGroups = _.uniq(_.flatten(memberSets));
let groupNames = allGroupNames.filter(groupName => !groupName.includes(':privileges:') && uniqueGroups.includes(groupName));
groupNames = groups.ephemeralGroups.concat(groupNames);
2020-12-14 09:20:41 +03:00
moveToFront(groupNames, groups.BANNED_USERS);
2019-07-20 22:12:22 -04:00
moveToFront(groupNames, 'Global Moderators');
moveToFront(groupNames, 'unverified-users');
moveToFront(groupNames, 'verified-users');
2019-07-20 22:12:22 -04:00
moveToFront(groupNames, 'registered-users');
2017-12-20 14:49:20 -05:00
2019-07-20 22:12:22 -04:00
const adminIndex = groupNames.indexOf('administrators');
if (adminIndex !== -1) {
groupNames.splice(adminIndex, 1);
}
const groupData = await groups.getGroupsFields(groupNames, ['private', 'system']);
2019-07-20 22:12:22 -04:00
const memberData = groupNames.map(function (member, index) {
const memberPrivs = {};
for (var x = 0, numPrivs = groupPrivileges.length; x < numPrivs; x += 1) {
memberPrivs[groupPrivileges[x]] = memberSets[x].includes(member);
}
return {
name: validator.escape(member),
nameEscaped: translator.escape(validator.escape(member)),
2019-07-20 22:12:22 -04:00
privileges: memberPrivs,
isPrivate: groupData[index] && !!groupData[index].private,
isSystem: groupData[index] && !!groupData[index].system,
2019-07-20 22:12:22 -04:00
};
});
return memberData;
2017-12-20 15:19:22 -05:00
};
function moveToFront(groupNames, groupToMove) {
const index = groupNames.indexOf(groupToMove);
if (index !== -1) {
groupNames.splice(0, 0, groupNames.splice(index, 1)[0]);
} else {
groupNames.unshift(groupToMove);
}
}
helpers.giveOrRescind = async function (method, privileges, cids, members) {
members = Array.isArray(members) ? members : [members];
cids = Array.isArray(cids) ? cids : [cids];
for (const member of members) {
2019-07-20 22:12:22 -04:00
const groupKeys = [];
cids.forEach((cid) => {
privileges.forEach((privilege) => {
groupKeys.push('cid:' + cid + ':privileges:' + privilege);
});
});
2019-07-20 22:12:22 -04:00
/* eslint-disable no-await-in-loop */
await method(groupKeys, member);
2019-07-20 22:12:22 -04:00
}
};
2019-07-20 22:12:22 -04:00
require('../promisify')(helpers);