2020-05-28 11:29:59 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-09 17:45:53 +02:00
|
|
|
const _ = require('lodash');
|
2020-05-28 11:29:59 +02:00
|
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
2020-06-15 19:11:36 +02:00
|
|
|
const validators = require('./common-validators');
|
2020-05-28 11:29:59 +02:00
|
|
|
|
|
|
|
const handleReject = error => Promise.reject(formatYupErrors(error));
|
|
|
|
|
2020-06-10 15:42:32 +02:00
|
|
|
// validatedUpdatePermissionsInput
|
2020-06-09 17:45:53 +02:00
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
const BOUND_ACTIONS = [
|
2020-06-18 11:53:35 +02:00
|
|
|
'plugins::content-manager.explorer.read',
|
|
|
|
'plugins::content-manager.explorer.create',
|
|
|
|
'plugins::content-manager.explorer.update',
|
|
|
|
'plugins::content-manager.explorer.delete',
|
2020-06-09 17:45:53 +02:00
|
|
|
];
|
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
const checkPermissionsAreBound = function(permissions) {
|
2020-06-09 17:45:53 +02:00
|
|
|
let areBond = true;
|
2020-07-03 12:28:57 +02:00
|
|
|
const permsBySubject = _.groupBy(
|
|
|
|
permissions.filter(perm => BOUND_ACTIONS.includes(perm.action)),
|
|
|
|
'subject'
|
|
|
|
);
|
|
|
|
_.forIn(permsBySubject, perms => {
|
|
|
|
const uniqPerms = _.uniqBy(perms, 'action');
|
|
|
|
areBond = uniqPerms.length === BOUND_ACTIONS.length;
|
|
|
|
if (!areBond) return false;
|
|
|
|
const permsByAction = _.groupBy(uniqPerms, 'action');
|
|
|
|
areBond =
|
2020-07-03 13:17:54 +02:00
|
|
|
BOUND_ACTIONS.filter(action => action !== 'plugins::content-manager.explorer.delete')
|
2020-07-03 12:28:57 +02:00
|
|
|
.map(action => permsByAction[action][0].fields || [])
|
|
|
|
.map(f => f.sort())
|
2020-07-03 13:17:54 +02:00
|
|
|
.filter((fields, i, arr) => _.isEqual(arr[0], fields)).length ===
|
|
|
|
BOUND_ACTIONS.length - 1;
|
2020-06-09 17:45:53 +02:00
|
|
|
if (!areBond) return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return areBond;
|
|
|
|
};
|
|
|
|
|
2020-06-23 16:31:16 +02:00
|
|
|
const updatePermissionsSchemaWithBoundConstraint = [
|
|
|
|
validators.updatePermissions,
|
2020-06-15 19:11:36 +02:00
|
|
|
yup.object().shape({
|
2020-05-28 13:02:06 +02:00
|
|
|
permissions: yup
|
|
|
|
.array()
|
2020-06-09 17:45:53 +02:00
|
|
|
.test(
|
|
|
|
'are-bond',
|
|
|
|
'Read, Create, Update and Delete have to be defined all together for a subject field or not at all',
|
2020-06-15 19:11:36 +02:00
|
|
|
checkPermissionsAreBound
|
2020-06-09 17:45:53 +02:00
|
|
|
),
|
2020-06-15 19:11:36 +02:00
|
|
|
}),
|
|
|
|
];
|
2020-05-28 11:29:59 +02:00
|
|
|
|
2020-06-10 18:04:47 +02:00
|
|
|
const checkPermissionsSchema = yup.object().shape({
|
|
|
|
permissions: yup.array().of(
|
|
|
|
yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
action: yup.string().required(),
|
|
|
|
subject: yup.string(),
|
|
|
|
field: yup.string(),
|
|
|
|
})
|
|
|
|
.noUnknown()
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
const validateCheckPermissionsInput = data => {
|
|
|
|
return checkPermissionsSchema
|
|
|
|
.validate(data, { strict: true, abortEarly: false })
|
|
|
|
.catch(handleReject);
|
|
|
|
};
|
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
const validatedUpdatePermissionsInput = async data => {
|
|
|
|
try {
|
2020-06-23 16:31:16 +02:00
|
|
|
for (const schema of updatePermissionsSchemaWithBoundConstraint) {
|
2020-06-15 19:11:36 +02:00
|
|
|
await schema.validate(data, { strict: true, abortEarly: false });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-06-18 18:10:12 +02:00
|
|
|
return handleReject(e);
|
2020-06-15 19:11:36 +02:00
|
|
|
}
|
2020-05-28 11:29:59 +02:00
|
|
|
};
|
|
|
|
|
2020-06-10 15:42:32 +02:00
|
|
|
// validatePermissionsExist
|
2020-06-09 17:45:53 +02:00
|
|
|
|
|
|
|
const checkPermissionsExist = function(permissions) {
|
2020-06-09 19:00:57 +02:00
|
|
|
const existingActions = strapi.admin.services.permission.actionProvider.getAll();
|
2020-06-09 17:45:53 +02:00
|
|
|
const failIndex = permissions.findIndex(
|
|
|
|
permission =>
|
2020-06-15 19:11:36 +02:00
|
|
|
!existingActions.some(
|
2020-06-09 17:45:53 +02:00
|
|
|
ea =>
|
|
|
|
ea.actionId === permission.action &&
|
2020-06-23 16:31:16 +02:00
|
|
|
(ea.section !== 'contentTypes' || ea.subjects.includes(permission.subject))
|
2020-06-09 17:45:53 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return failIndex === -1
|
|
|
|
? true
|
|
|
|
: this.createError({
|
|
|
|
path: 'permissions',
|
|
|
|
message: `[${failIndex}] is not an existing permission action`,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const actionsExistSchema = yup
|
|
|
|
.array()
|
2020-06-15 19:11:36 +02:00
|
|
|
.of(
|
|
|
|
yup.object().shape({
|
2020-06-18 11:40:50 +02:00
|
|
|
conditions: validators.arrayOfConditionNames,
|
2020-06-15 19:11:36 +02:00
|
|
|
})
|
|
|
|
)
|
2020-06-09 17:45:53 +02:00
|
|
|
.test('actions-exist', '', checkPermissionsExist);
|
|
|
|
|
|
|
|
const validatePermissionsExist = data => {
|
|
|
|
return actionsExistSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
|
|
};
|
|
|
|
|
|
|
|
// exports
|
|
|
|
|
2020-05-28 11:29:59 +02:00
|
|
|
module.exports = {
|
|
|
|
validatedUpdatePermissionsInput,
|
2020-06-09 17:45:53 +02:00
|
|
|
validatePermissionsExist,
|
2020-06-10 18:04:47 +02:00
|
|
|
validateCheckPermissionsInput,
|
2020-05-28 11:29:59 +02:00
|
|
|
};
|