Rename fieldsGranularity to fieldsRestriction, various bug fixes

Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
Convly 2020-08-11 18:16:39 +02:00 committed by Pierre Noël
parent 310a0d16f3
commit 72aaa16d3d
8 changed files with 73 additions and 26 deletions

View File

@ -15,7 +15,7 @@ const actionFields = [
const defaultAction = {
options: {
fieldsGranularity: true,
fieldsRestriction: true,
},
};
@ -39,7 +39,7 @@ const getActionId = ({ pluginName, uid }) => {
* Create a permission action
* @param {Object} attributes - action attributes
*/
function createAction(attributes) {
const createAction = attributes => {
const action = _.cloneDeep(_.pick(attributes, actionFields));
action.actionId = getActionId(attributes);
@ -47,10 +47,13 @@ function createAction(attributes) {
action.subCategory = attributes.subCategory || 'general';
}
return _.merge(action, defaultAction);
}
return _.merge({}, defaultAction, action);
};
const hasFieldsRestriction = _.matchesProperty('options.fieldsRestriction', true);
module.exports = {
getActionId,
createAction,
hasFieldsRestriction,
};

View File

@ -220,7 +220,7 @@ describe('Content-Type', () => {
describe('getPermissionsWithNestedFields', () => {
test('1 action (no nesting)', async () => {
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields([
{ actionId: 'action-1', subjects: ['country'] },
{ actionId: 'action-1', subjects: ['country'], options: { fieldsRestriction: true } },
]);
expect(resultLevel1).toEqual([
{
@ -234,7 +234,13 @@ describe('Content-Type', () => {
test('2 actions (with nesting level 1)', async () => {
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields(
[{ actionId: 'action-1', subjects: ['country', 'user'] }],
[
{
actionId: 'action-1',
subjects: ['country', 'user'],
options: { fieldsRestriction: true },
},
],
{ nestingLevel: 1 }
);
expect(resultLevel1).toEqual([
@ -255,7 +261,13 @@ describe('Content-Type', () => {
test('2 actions (with nesting level 2)', async () => {
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields(
[{ actionId: 'action-1', subjects: ['country', 'user'] }],
[
{
actionId: 'action-1',
subjects: ['country', 'user'],
options: { fieldsRestriction: true },
},
],
{ nestingLevel: 2 }
);
expect(resultLevel1).toEqual([
@ -282,7 +294,11 @@ describe('Content-Type', () => {
test('2 actions (with nesting level 100)', async () => {
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields([
{ actionId: 'action-1', subjects: ['country', 'user'] },
{
actionId: 'action-1',
subjects: ['country', 'user'],
options: { fieldsRestriction: true },
},
]);
expect(resultLevel1).toEqual([
{
@ -311,6 +327,21 @@ describe('Content-Type', () => {
});
describe('cleanPermissionFields', () => {
beforeAll(() => {
global.strapi = {
...global.strapi,
admin: {
services: {
permission: {
actionProvider: {
getByActionId: () => ({ options: { fieldsRestriction: true } }),
},
},
},
},
};
});
const tests = [
[undefined, ['firstname', 'car']],
[null, ['firstname', 'car']],
@ -336,6 +367,7 @@ describe('Content-Type', () => {
{
subject: 'user',
fields,
options: { fieldsRestriction: true },
},
],
{
@ -346,6 +378,7 @@ describe('Content-Type', () => {
{
subject: 'user',
fields: expectedFields,
options: { fieldsRestriction: true },
},
]);
});

View File

@ -2,6 +2,7 @@
const _ = require('lodash');
const fp = require('lodash/fp');
const actionDomain = require('../domain/action');
const EXCLUDE_FIELDS = ['created_by', 'updated_by'];
@ -118,7 +119,7 @@ const getPermissionsWithNestedFields = (actions, { nestingLevel, restrictedSubje
action.subjects
.filter(subject => !restrictedSubjects.includes(subject))
.forEach(contentTypeUid => {
const fields = action.options.fieldsGranularity
const fields = actionDomain.hasFieldsRestriction(action)
? getNestedFields(strapi.contentTypes[contentTypeUid], {
components: strapi.components,
nestingLevel,
@ -141,13 +142,13 @@ const getPermissionsWithNestedFields = (actions, { nestingLevel, restrictedSubje
* @param {number} options.nestingLevel level of nesting
* @returns {array<permissions>}
*/
const cleanPermissionFields = (permissions, { nestingLevel }) =>
const cleanPermissionFields = (permissions, { nestingLevel } = {}) =>
permissions.map(perm => {
const { action: actionId, fields, subject } = perm;
const action = strapi.admin.services.permission.actionProvider.getByActionId(actionId);
let newFields = fields;
if (!action.options.fieldsGranularity) {
if (!actionDomain.hasFieldsRestriction(action)) {
newFields = null;
} else if (subject && strapi.contentTypes[subject]) {
const possibleFields = getNestedFieldsWithIntermediate(strapi.contentTypes[subject], {

View File

@ -62,6 +62,8 @@ const createActionProvider = () => {
}
actions.set(actionId, createAction(newAction));
return this;
});
},
};

View File

@ -63,7 +63,7 @@ const registerProviderActionSchema = yup
),
}),
options: yup.object({
fieldsGranularity: yup.boolean(),
fieldsRestriction: yup.boolean(),
}),
})
.noUnknown()

View File

@ -2,6 +2,7 @@
const { yup } = require('strapi-utils');
const _ = require('lodash');
const actionDomain = require('../domain/action');
const {
checkFieldsAreCorrectlyNested,
checkFieldsDontHaveDuplicates,
@ -45,11 +46,18 @@ const arrayOfConditionNames = yup
: this.createError({ path: this.path, message: `contains conditions that don't exist` });
});
const checkCTPermsDeleteHaveFieldsToNull = permissions =>
!Array.isArray(permissions) ||
permissions.every(
perm => perm.action !== 'plugins::content-manager.explorer.delete' || _.isNil(perm.fields)
);
const checkContentTypesFieldsRestriction = permissions => {
const { actionProvider } = strapi.admin.services.permission;
if (!_.isArray(permissions)) {
return true;
}
return permissions.every(({ action: actionId, fields }) => {
const action = actionProvider.getByActionId(actionId);
return actionDomain.hasFieldsRestriction(action) || _.isNil(fields);
});
};
const permissionsAreEquals = (a, b) =>
a.action === b.action && (a.subject === b.subject || (_.isNil(a.subject) && _.isNil(b.subject)));
@ -94,9 +102,9 @@ const updatePermissions = yup
checkNoDuplicatedPermissions
)
.test(
'delete-fields-are-null',
'The action "plugins::content-manager.explorer.delete" must have fields set to null or undefined',
checkCTPermsDeleteHaveFieldsToNull
'fields-restriction',
'The actions must have fields set to null or undefined',
checkContentTypesFieldsRestriction
)
.noUnknown()
),

View File

@ -102,9 +102,9 @@ const checkPermissionsExist = function(permissions) {
const failIndex = permissions.findIndex(
permission =>
!existingActions.some(
ea =>
ea.actionId === permission.action &&
(ea.section !== 'contentTypes' || ea.subjects.includes(permission.subject))
action =>
action.actionId === permission.action &&
(action.section !== 'contentTypes' || action.subjects.includes(permission.subject))
)
);

View File

@ -79,7 +79,7 @@ const syncComponentsSchemas = async () => {
const componentsToAdd = _.difference(realUIDs, DBUIDs);
const componentsToDelete = _.difference(DBUIDs, realUIDs);
// delette old schemas
// delete old schemas
await Promise.all(componentsToDelete.map(uid => componentService.deleteConfiguration(uid)));
// create new schemas
@ -125,7 +125,7 @@ const registerPermissions = () => {
pluginName: 'content-manager',
subjects: contentTypesUids,
options: {
fieldsGranularity: false,
fieldsRestriction: false,
},
},
{
@ -135,7 +135,7 @@ const registerPermissions = () => {
pluginName: 'content-manager',
subjects: contentTypesUids.filter(hasDraftAndPublish),
options: {
fieldsGranularity: false,
fieldsRestriction: false,
},
},
{