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-06-18 11:40:50 +02:00
|
|
|
const { checkFieldsAreCorrectlyNested } = require('./common-functions');
|
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 checkBoundActionsHaveFields = function(permissions) {
|
|
|
|
const haveFields = permissions
|
|
|
|
.filter(perm => BOUND_ACTIONS.includes(perm.action))
|
|
|
|
.every(perm => typeof perm.subject === 'string' && Array.isArray(perm.fields));
|
|
|
|
|
|
|
|
return haveFields
|
|
|
|
? true
|
|
|
|
: this.createError({
|
|
|
|
message: 'Your permissions are missing fields "subject" and/or "fields"',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkPermissionsAreBound = function(permissions) {
|
2020-06-09 17:45:53 +02:00
|
|
|
const subjectMap = {};
|
|
|
|
let areBond = true;
|
|
|
|
permissions
|
2020-06-15 19:11:36 +02:00
|
|
|
.filter(perm => BOUND_ACTIONS.includes(perm.action))
|
2020-06-09 17:45:53 +02:00
|
|
|
.forEach(perm => {
|
|
|
|
subjectMap[perm.subject] = subjectMap[perm.subject] || {};
|
|
|
|
perm.fields.forEach(field => {
|
|
|
|
subjectMap[perm.subject][field] = subjectMap[perm.subject][field] || new Set();
|
|
|
|
subjectMap[perm.subject][field].add(perm.action);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
_.forIn(subjectMap, subject => {
|
|
|
|
_.forIn(subject, field => {
|
2020-06-15 19:11:36 +02:00
|
|
|
if (field.size !== BOUND_ACTIONS.length) {
|
2020-06-09 17:45:53 +02:00
|
|
|
areBond = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!areBond) return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return areBond;
|
|
|
|
};
|
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
const updatePermissionsSchemaArray = [
|
|
|
|
yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
permissions: yup
|
|
|
|
.array()
|
|
|
|
.requiredAllowEmpty()
|
|
|
|
.of(
|
|
|
|
yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
action: yup.string().required(),
|
|
|
|
subject: yup.string(),
|
2020-06-18 11:40:50 +02:00
|
|
|
fields: yup
|
|
|
|
.array()
|
|
|
|
.of(yup.string())
|
|
|
|
.test(
|
|
|
|
'field-nested',
|
|
|
|
'Fields format are incorrect (duplicates or bad nesting).',
|
|
|
|
checkFieldsAreCorrectlyNested
|
|
|
|
),
|
|
|
|
conditions: validators.arrayOfConditionNames,
|
2020-06-15 19:11:36 +02:00
|
|
|
})
|
|
|
|
.noUnknown()
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.required()
|
|
|
|
.noUnknown(),
|
2020-06-16 18:49:49 +02:00
|
|
|
yup.object().shape({
|
|
|
|
permissions: yup
|
|
|
|
.array()
|
|
|
|
.test(
|
|
|
|
'contentTypes-have-fields',
|
|
|
|
'Your permissions are missing fields "subject" and/or "fields"',
|
|
|
|
checkBoundActionsHaveFields
|
|
|
|
),
|
|
|
|
}),
|
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 {
|
|
|
|
for (const schema of updatePermissionsSchemaArray) {
|
|
|
|
await schema.validate(data, { strict: true, abortEarly: false });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
await handleReject(e);
|
|
|
|
}
|
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-15 19:11:36 +02:00
|
|
|
(ea.section !== 'contentTypes' ||
|
|
|
|
(ea.subjects.includes(permission.subject) && Array.isArray(permission.fields)))
|
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
|
|
|
};
|