mirror of
https://github.com/strapi/strapi.git
synced 2025-08-06 15:53:11 +00:00

* Add a domain layer for the permission, rework the engine handling of the permissions Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add permissions-fields-to-properties migration for the admin Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removes useless console.log Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove debug logLevel from provider-login.test.e2e.js Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Adds the new layout for the GET permissions, allow to subscribe to actionRegistered events, adds i18n handlers Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix typo Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update permissions validators Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update unit tests Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update integrations test + fix some validation issues Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Change plugins & settings section format for the permissions layout * only return locales property to localized subjects for the permission's layout * Do not send the locales property to the permission's layout when there is no locales created * Add the 'locales' property to publish & delete routes * Fix unwanted mutation of the sections builder states on multiple builds * Fix units tests with (new engine) * Fix admin-role e2e test - Add locales property to the update payload * fix e2e testsé * Update e2e snapshots * Fix unit test for i18n bootstrap * Add mocks for i18n/bootstrap test * Fix has-locale condition & updatePermission validator * Avoid mutation in migration, always authorize super admin for has-locales condition * Rework rbac domain objects, add a hook module and a provider factory * Remove old providers * Update the admin services & tests for the new rbac domain & providers * Fix tests, bootstrap functions & services following rbac domain rework * Update migration runner * PR comments Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove useless console.log * Fix sanitizeCondition bug * Section builder rework * Add test for the section-builder section & add jsdoc for the permission domain * pr comments (without the migrations) * fix fields-to-properties migration * Add jsdoc for the sections-builder * Moves createBoundAbstractDomain from permission domain to the engine service * Remove debug logLevel for admin role test (e2e) * Fix core-store * Fix hooks & move business logic from i18n bootstrap to dedicated services * add route get-non-localized-fields * use write and read permission * refacto * add input validator * add route doc * handle ST Co-authored-by: Pierre Noël <petersg83@gmail.com> Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
154 lines
4.2 KiB
JavaScript
154 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
|
const { getService } = require('../utils');
|
|
const { AUTHOR_CODE, PUBLISH_ACTION } = require('../services/constants');
|
|
const {
|
|
BOUND_ACTIONS_FOR_FIELDS,
|
|
BOUND_ACTIONS,
|
|
getBoundActionsBySubject,
|
|
} = require('../domain/role');
|
|
const validators = require('./common-validators');
|
|
|
|
const handleReject = error => Promise.reject(formatYupErrors(error));
|
|
|
|
// validatedUpdatePermissionsInput
|
|
|
|
const actionFieldsAreEqual = (a, b) => {
|
|
const aFields = a.properties.fields || [];
|
|
const bFields = b.properties.fields || [];
|
|
|
|
return _.isEqual(aFields.sort(), bFields.sort());
|
|
};
|
|
|
|
const haveSameFieldsAsOtherActions = (a, i, allActions) =>
|
|
allActions.slice(i + 1).every(b => actionFieldsAreEqual(a, b));
|
|
|
|
const checkPermissionsAreBound = role =>
|
|
function(permissions) {
|
|
const permsBySubject = _.groupBy(
|
|
permissions.filter(perm => BOUND_ACTIONS.includes(perm.action)),
|
|
'subject'
|
|
);
|
|
|
|
for (const [subject, perms] of Object.entries(permsBySubject)) {
|
|
const boundActions = getBoundActionsBySubject(role, subject);
|
|
const missingActions =
|
|
_.xor(
|
|
perms.map(p => p.action),
|
|
boundActions
|
|
).length !== 0;
|
|
if (missingActions) return false;
|
|
|
|
const permsBoundByFields = perms.filter(p => BOUND_ACTIONS_FOR_FIELDS.includes(p.action));
|
|
const everyActionsHaveSameFields = _.every(permsBoundByFields, haveSameFieldsAsOtherActions);
|
|
if (!everyActionsHaveSameFields) return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
const noPublishPermissionForAuthorRole = role =>
|
|
function(permissions) {
|
|
const isAuthor = role.code === AUTHOR_CODE;
|
|
const hasPublishPermission = permissions.some(perm => perm.action === PUBLISH_ACTION);
|
|
|
|
return !(isAuthor && hasPublishPermission);
|
|
};
|
|
|
|
const getUpdatePermissionsSchemas = role => [
|
|
validators.updatePermissions,
|
|
yup.object().shape({ permissions: actionsExistSchema.clone() }),
|
|
yup.object().shape({
|
|
permissions: yup
|
|
.array()
|
|
.test(
|
|
'author-no-publish',
|
|
'The author role cannot have the publish permission.',
|
|
noPublishPermissionForAuthorRole(role)
|
|
),
|
|
}),
|
|
yup.object().shape({
|
|
permissions: yup
|
|
.array()
|
|
.test(
|
|
'are-bond',
|
|
'Permissions have to be defined all together for a subject field or not at all',
|
|
checkPermissionsAreBound(role)
|
|
),
|
|
}),
|
|
];
|
|
|
|
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);
|
|
};
|
|
|
|
const validatedUpdatePermissionsInput = async (permissions, role) => {
|
|
try {
|
|
const schemas = getUpdatePermissionsSchemas(role);
|
|
for (const schema of schemas) {
|
|
await schema.validate(permissions, { strict: true, abortEarly: false });
|
|
}
|
|
} catch (e) {
|
|
return handleReject(e);
|
|
}
|
|
};
|
|
|
|
// validatePermissionsExist
|
|
|
|
const checkPermissionsExist = function(permissions) {
|
|
const existingActions = getService('permission').actionProvider.values();
|
|
const failIndex = permissions.findIndex(
|
|
permission =>
|
|
!existingActions.some(
|
|
action =>
|
|
action.actionId === permission.action &&
|
|
(action.section !== 'contentTypes' || action.subjects.includes(permission.subject))
|
|
)
|
|
);
|
|
|
|
return failIndex === -1
|
|
? true
|
|
: this.createError({
|
|
path: 'permissions',
|
|
message: `[${failIndex}] is not an existing permission action`,
|
|
});
|
|
};
|
|
|
|
const actionsExistSchema = yup
|
|
.array()
|
|
.of(
|
|
yup.object().shape({
|
|
conditions: yup.array().of(yup.string()),
|
|
})
|
|
)
|
|
.test('actions-exist', '', checkPermissionsExist);
|
|
|
|
const validatePermissionsExist = data => {
|
|
return actionsExistSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
};
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
|
validatedUpdatePermissionsInput,
|
|
validatePermissionsExist,
|
|
validateCheckPermissionsInput,
|
|
};
|