2014-05-14 17:53:23 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
const _ = require('lodash');
|
2019-10-02 22:20:09 -04:00
|
|
|
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');
|
2023-01-30 12:26:08 -05:00
|
|
|
const categories = require('../categories');
|
2019-07-20 22:12:22 -04:00
|
|
|
const plugins = require('../plugins');
|
2019-10-02 22:20:09 -04:00
|
|
|
const translator = require('../translator');
|
2025-03-14 15:26:59 -04:00
|
|
|
const utils = require('../utils');
|
2014-05-14 17:53:23 -04:00
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
const helpers = module.exports;
|
2014-05-14 17:53:23 -04:00
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
const uidToSystemGroup = {
|
2018-01-31 15:20:17 -05:00
|
|
|
0: 'guests',
|
|
|
|
|
'-1': 'spiders',
|
2024-02-26 11:39:32 -05:00
|
|
|
'-2': 'fediverse',
|
2018-01-31 15:20:17 -05:00
|
|
|
};
|
|
|
|
|
|
2020-03-12 12:25:51 -04:00
|
|
|
helpers.isUsersAllowedTo = async function (privilege, uids, cid) {
|
2025-03-17 14:52:52 -04:00
|
|
|
// Remote categories inherit world pseudo-category privileges
|
|
|
|
|
if (!utils.isNumber(cid)) {
|
|
|
|
|
cid = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 12:25:51 -04:00
|
|
|
const [hasUserPrivilege, hasGroupPrivilege] = await Promise.all([
|
2021-02-03 23:59:08 -07:00
|
|
|
groups.isMembers(uids, `cid:${cid}:privileges:${privilege}`),
|
|
|
|
|
groups.isMembersOfGroupList(uids, `cid:${cid}:privileges:groups:${privilege}`),
|
2020-03-12 12:25:51 -04:00
|
|
|
]);
|
|
|
|
|
const allowed = uids.map((uid, index) => hasUserPrivilege[index] || hasGroupPrivilege[index]);
|
2020-11-20 16:06:26 -05:00
|
|
|
const result = await plugins.hooks.fire('filter:privileges:isUsersAllowedTo', { allowed: allowed, privilege: privilege, uids: uids, cid: cid });
|
2020-03-12 12:25:51 -04:00
|
|
|
return result.allowed;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-13 14:15:37 -05:00
|
|
|
helpers.isAllowedTo = async function (privilege, uidOrGroupName, cid) {
|
2025-03-14 15:26:59 -04:00
|
|
|
// Remote categories (non-numeric) inherit world privileges
|
|
|
|
|
if (Array.isArray(cid)) {
|
|
|
|
|
cid = cid.map(cid => (utils.isNumber(cid) ? cid : -1));
|
|
|
|
|
} else {
|
|
|
|
|
cid = utils.isNumber(cid) ? cid : -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 12:25:51 -04:00
|
|
|
let allowed;
|
2016-09-15 14:01:56 +03:00
|
|
|
if (Array.isArray(privilege) && !Array.isArray(cid)) {
|
2020-11-13 14:15:37 -05:00
|
|
|
allowed = await isAllowedToPrivileges(privilege, uidOrGroupName, cid);
|
2016-09-15 14:01:56 +03:00
|
|
|
} else if (Array.isArray(cid) && !Array.isArray(privilege)) {
|
2020-11-13 14:15:37 -05:00
|
|
|
allowed = await isAllowedToCids(privilege, uidOrGroupName, cid);
|
2020-03-12 12:25:51 -04:00
|
|
|
}
|
|
|
|
|
if (allowed) {
|
2020-11-20 16:06:26 -05:00
|
|
|
({ allowed } = await plugins.hooks.fire('filter:privileges:isAllowedTo', { allowed: allowed, privilege: privilege, uid: uidOrGroupName, cid: cid }));
|
2020-11-13 14:15:37 -05:00
|
|
|
return allowed;
|
2016-09-15 14:01:56 +03:00
|
|
|
}
|
2019-07-20 22:12:22 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2016-09-15 14:01:56 +03:00
|
|
|
};
|
|
|
|
|
|
2020-11-13 14:15:37 -05:00
|
|
|
async function isAllowedToCids(privilege, uidOrGroupName, cids) {
|
2020-11-06 23:13:12 -05:00
|
|
|
if (!privilege) {
|
|
|
|
|
return cids.map(() => false);
|
|
|
|
|
}
|
2020-11-13 14:15:37 -05:00
|
|
|
|
2021-02-03 23:59:08 -07:00
|
|
|
const groupKeys = cids.map(cid => `cid:${cid}:privileges:groups:${privilege}`);
|
2020-11-13 14:42:44 -05:00
|
|
|
|
2020-11-13 14:15:37 -05:00
|
|
|
// Group handling
|
|
|
|
|
if (isNaN(parseInt(uidOrGroupName, 10)) && (uidOrGroupName || '').length) {
|
2020-11-13 14:42:44 -05:00
|
|
|
return await checkIfAllowedGroup(uidOrGroupName, groupKeys);
|
2020-11-13 14:15:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// User handling
|
|
|
|
|
if (parseInt(uidOrGroupName, 10) <= 0) {
|
|
|
|
|
return await isSystemGroupAllowedToCids(privilege, uidOrGroupName, cids);
|
2014-07-29 21:51:46 -04:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 23:59:08 -07:00
|
|
|
const userKeys = cids.map(cid => `cid:${cid}:privileges:${privilege}`);
|
2020-11-13 14:42:44 -05:00
|
|
|
return await checkIfAllowedUser(uidOrGroupName, userKeys, groupKeys);
|
2016-09-15 14:01:56 +03:00
|
|
|
}
|
|
|
|
|
|
2020-11-13 14:15:37 -05:00
|
|
|
async function isAllowedToPrivileges(privileges, uidOrGroupName, cid) {
|
2021-02-03 23:59:08 -07:00
|
|
|
const groupKeys = privileges.map(privilege => `cid:${cid}:privileges:groups:${privilege}`);
|
2020-11-13 14:15:37 -05:00
|
|
|
// Group handling
|
|
|
|
|
if (isNaN(parseInt(uidOrGroupName, 10)) && (uidOrGroupName || '').length) {
|
2020-11-13 14:42:44 -05:00
|
|
|
return await checkIfAllowedGroup(uidOrGroupName, groupKeys);
|
2020-11-13 14:15:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// User handling
|
|
|
|
|
if (parseInt(uidOrGroupName, 10) <= 0) {
|
|
|
|
|
return await isSystemGroupAllowedToPrivileges(privileges, uidOrGroupName, cid);
|
2016-09-15 14:01:56 +03:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 23:59:08 -07:00
|
|
|
const userKeys = privileges.map(privilege => `cid:${cid}:privileges:${privilege}`);
|
2020-11-13 14:42:44 -05:00
|
|
|
return await checkIfAllowedUser(uidOrGroupName, userKeys, groupKeys);
|
2017-05-25 21:17:20 -04:00
|
|
|
}
|
2016-09-15 14:01:56 +03: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]);
|
2016-09-15 14:01:56 +03:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2021-02-03 23:59:08 -07:00
|
|
|
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);
|
2014-07-29 21:51:46 -04:00
|
|
|
}
|
2014-05-14 17:53:23 -04:00
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
async function isSystemGroupAllowedToPrivileges(privileges, uid, cid) {
|
2021-02-03 23:59:08 -07:00
|
|
|
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);
|
2016-09-15 14:01:56 +03:00
|
|
|
}
|
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) {
|
2021-02-03 23:59:08 -07:00
|
|
|
let memberSets = await groups.getMembersOfGroups(userPrivileges.map(privilege => `cid:${cid}:privileges:${privilege}`));
|
2021-02-04 00:01:39 -07:00
|
|
|
memberSets = memberSets.map(set => 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
|
|
|
|
2021-02-04 00:01:39 -07:00
|
|
|
memberData.forEach((member) => {
|
2019-07-20 22:12:22 -04:00
|
|
|
member.privileges = {};
|
2021-02-04 00:06:15 -07:00
|
|
|
for (let x = 0, numPrivs = userPrivileges.length; x < numPrivs; x += 1) {
|
2019-07-20 22:12:22 -04:00
|
|
|
member.privileges[userPrivileges[x]] = memberSets[x].includes(parseInt(member.uid, 10));
|
|
|
|
|
}
|
2023-09-25 20:42:18 -04:00
|
|
|
const types = {};
|
|
|
|
|
for (const [key] of Object.entries(member.privileges)) {
|
|
|
|
|
types[key] = getType(key);
|
|
|
|
|
}
|
|
|
|
|
member.types = types;
|
2019-07-20 22:12:22 -04:00
|
|
|
});
|
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([
|
2021-02-03 23:59:08 -07:00
|
|
|
groups.getMembersOfGroups(groupPrivileges.map(privilege => `cid:${cid}:privileges:${privilege}`)),
|
2019-07-20 22:12:22 -04:00
|
|
|
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');
|
2020-10-13 22:42:50 -04:00
|
|
|
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);
|
|
|
|
|
}
|
2020-10-13 22:42:50 -04:00
|
|
|
const groupData = await groups.getGroupsFields(groupNames, ['private', 'system']);
|
2021-02-04 00:01:39 -07:00
|
|
|
const memberData = groupNames.map((member, index) => {
|
2019-07-20 22:12:22 -04:00
|
|
|
const memberPrivs = {};
|
|
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
for (let x = 0, numPrivs = groupPrivileges.length; x < numPrivs; x += 1) {
|
2019-07-20 22:12:22 -04:00
|
|
|
memberPrivs[groupPrivileges[x]] = memberSets[x].includes(member);
|
|
|
|
|
}
|
2023-09-25 20:42:18 -04:00
|
|
|
const types = {};
|
|
|
|
|
for (const [key] of Object.entries(memberPrivs)) {
|
|
|
|
|
types[key] = getType(key);
|
|
|
|
|
}
|
2019-07-20 22:12:22 -04:00
|
|
|
return {
|
2019-10-02 22:20:09 -04:00
|
|
|
name: validator.escape(member),
|
|
|
|
|
nameEscaped: translator.escape(validator.escape(member)),
|
2019-07-20 22:12:22 -04:00
|
|
|
privileges: memberPrivs,
|
2023-09-25 20:42:18 -04:00
|
|
|
types: types,
|
2019-07-20 22:12:22 -04:00
|
|
|
isPrivate: groupData[index] && !!groupData[index].private,
|
2020-10-13 22:42:50 -04:00
|
|
|
isSystem: groupData[index] && !!groupData[index].system,
|
2019-07-20 22:12:22 -04:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
return memberData;
|
2017-12-20 15:19:22 -05:00
|
|
|
};
|
2018-01-03 13:27:30 -05:00
|
|
|
|
2023-09-25 20:42:18 -04:00
|
|
|
|
|
|
|
|
function getType(privilege) {
|
|
|
|
|
privilege = privilege.replace(/^groups:/, '');
|
|
|
|
|
const global = require('./global');
|
|
|
|
|
const categories = require('./categories');
|
|
|
|
|
return global.getType(privilege) || categories.getType(privilege) || 'other';
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 19:30:47 -04: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 21:57:38 -04:00
|
|
|
helpers.giveOrRescind = async function (method, privileges, cids, members) {
|
|
|
|
|
members = Array.isArray(members) ? members : [members];
|
2018-10-15 13:45:55 -04:00
|
|
|
cids = Array.isArray(cids) ? cids : [cids];
|
2020-05-26 21:57:38 -04:00
|
|
|
for (const member of members) {
|
2019-07-20 22:12:22 -04:00
|
|
|
const groupKeys = [];
|
2019-08-13 14:36:15 -04:00
|
|
|
cids.forEach((cid) => {
|
|
|
|
|
privileges.forEach((privilege) => {
|
2021-02-03 23:59:08 -07:00
|
|
|
groupKeys.push(`cid:${cid}:privileges:${privilege}`);
|
2018-10-15 13:45:55 -04:00
|
|
|
});
|
|
|
|
|
});
|
2019-07-20 22:12:22 -04:00
|
|
|
/* eslint-disable no-await-in-loop */
|
2020-05-26 21:57:38 -04:00
|
|
|
await method(groupKeys, member);
|
2019-07-20 22:12:22 -04:00
|
|
|
}
|
2018-01-03 13:27:30 -05:00
|
|
|
};
|
2019-07-20 22:12:22 -04:00
|
|
|
|
2021-02-24 18:10:34 -05:00
|
|
|
helpers.userOrGroupPrivileges = async function (cid, uidOrGroup, privilegeList) {
|
2021-11-28 20:18:36 -05:00
|
|
|
const groupNames = privilegeList.map(privilege => `cid:${cid}:privileges:${privilege}`);
|
|
|
|
|
const isMembers = await groups.isMemberOfGroups(uidOrGroup, groupNames);
|
|
|
|
|
return _.zipObject(privilegeList, isMembers);
|
2021-02-24 18:10:34 -05:00
|
|
|
};
|
|
|
|
|
|
2023-01-30 12:26:08 -05:00
|
|
|
helpers.getUidsWithPrivilege = async (cids, privilege) => {
|
|
|
|
|
const disabled = (await categories.getCategoriesFields(cids, ['disabled'])).map(obj => obj.disabled);
|
|
|
|
|
|
|
|
|
|
const groupNames = cids.reduce((memo, cid) => {
|
|
|
|
|
memo.push(`cid:${cid}:privileges:${privilege}`);
|
|
|
|
|
memo.push(`cid:${cid}:privileges:groups:${privilege}`);
|
|
|
|
|
return memo;
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const memberSets = await groups.getMembersOfGroups(groupNames);
|
|
|
|
|
// Every other set is actually a list of user groups, not uids, so convert those to members
|
|
|
|
|
const sets = memberSets.reduce((memo, set, idx) => {
|
|
|
|
|
if (idx % 2) {
|
|
|
|
|
memo.groupNames.push(set);
|
|
|
|
|
} else {
|
|
|
|
|
memo.uids.push(set);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return memo;
|
|
|
|
|
}, { groupNames: [], uids: [] });
|
|
|
|
|
|
|
|
|
|
const uniqGroups = _.uniq(_.flatten(sets.groupNames));
|
|
|
|
|
const groupUids = await groups.getMembersOfGroups(uniqGroups);
|
|
|
|
|
const map = _.zipObject(uniqGroups, groupUids);
|
|
|
|
|
const uidsByCid = cids.map((cid, index) => {
|
|
|
|
|
if (disabled[index]) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
|
|
|
|
|
});
|
|
|
|
|
return uidsByCid;
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-20 22:12:22 -04:00
|
|
|
require('../promisify')(helpers);
|