mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 17:00:55 +00:00
Rename fieldsGranularity to fieldsRestriction, various bug fixes
Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
parent
310a0d16f3
commit
72aaa16d3d
@ -15,7 +15,7 @@ const actionFields = [
|
|||||||
|
|
||||||
const defaultAction = {
|
const defaultAction = {
|
||||||
options: {
|
options: {
|
||||||
fieldsGranularity: true,
|
fieldsRestriction: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ const getActionId = ({ pluginName, uid }) => {
|
|||||||
* Create a permission action
|
* Create a permission action
|
||||||
* @param {Object} attributes - action attributes
|
* @param {Object} attributes - action attributes
|
||||||
*/
|
*/
|
||||||
function createAction(attributes) {
|
const createAction = attributes => {
|
||||||
const action = _.cloneDeep(_.pick(attributes, actionFields));
|
const action = _.cloneDeep(_.pick(attributes, actionFields));
|
||||||
action.actionId = getActionId(attributes);
|
action.actionId = getActionId(attributes);
|
||||||
|
|
||||||
@ -47,10 +47,13 @@ function createAction(attributes) {
|
|||||||
action.subCategory = attributes.subCategory || 'general';
|
action.subCategory = attributes.subCategory || 'general';
|
||||||
}
|
}
|
||||||
|
|
||||||
return _.merge(action, defaultAction);
|
return _.merge({}, defaultAction, action);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
const hasFieldsRestriction = _.matchesProperty('options.fieldsRestriction', true);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getActionId,
|
getActionId,
|
||||||
createAction,
|
createAction,
|
||||||
|
hasFieldsRestriction,
|
||||||
};
|
};
|
||||||
|
@ -220,7 +220,7 @@ describe('Content-Type', () => {
|
|||||||
describe('getPermissionsWithNestedFields', () => {
|
describe('getPermissionsWithNestedFields', () => {
|
||||||
test('1 action (no nesting)', async () => {
|
test('1 action (no nesting)', async () => {
|
||||||
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields([
|
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields([
|
||||||
{ actionId: 'action-1', subjects: ['country'] },
|
{ actionId: 'action-1', subjects: ['country'], options: { fieldsRestriction: true } },
|
||||||
]);
|
]);
|
||||||
expect(resultLevel1).toEqual([
|
expect(resultLevel1).toEqual([
|
||||||
{
|
{
|
||||||
@ -234,7 +234,13 @@ describe('Content-Type', () => {
|
|||||||
|
|
||||||
test('2 actions (with nesting level 1)', async () => {
|
test('2 actions (with nesting level 1)', async () => {
|
||||||
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields(
|
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields(
|
||||||
[{ actionId: 'action-1', subjects: ['country', 'user'] }],
|
[
|
||||||
|
{
|
||||||
|
actionId: 'action-1',
|
||||||
|
subjects: ['country', 'user'],
|
||||||
|
options: { fieldsRestriction: true },
|
||||||
|
},
|
||||||
|
],
|
||||||
{ nestingLevel: 1 }
|
{ nestingLevel: 1 }
|
||||||
);
|
);
|
||||||
expect(resultLevel1).toEqual([
|
expect(resultLevel1).toEqual([
|
||||||
@ -255,7 +261,13 @@ describe('Content-Type', () => {
|
|||||||
|
|
||||||
test('2 actions (with nesting level 2)', async () => {
|
test('2 actions (with nesting level 2)', async () => {
|
||||||
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields(
|
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields(
|
||||||
[{ actionId: 'action-1', subjects: ['country', 'user'] }],
|
[
|
||||||
|
{
|
||||||
|
actionId: 'action-1',
|
||||||
|
subjects: ['country', 'user'],
|
||||||
|
options: { fieldsRestriction: true },
|
||||||
|
},
|
||||||
|
],
|
||||||
{ nestingLevel: 2 }
|
{ nestingLevel: 2 }
|
||||||
);
|
);
|
||||||
expect(resultLevel1).toEqual([
|
expect(resultLevel1).toEqual([
|
||||||
@ -282,7 +294,11 @@ describe('Content-Type', () => {
|
|||||||
|
|
||||||
test('2 actions (with nesting level 100)', async () => {
|
test('2 actions (with nesting level 100)', async () => {
|
||||||
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields([
|
const resultLevel1 = contentTypeService.getPermissionsWithNestedFields([
|
||||||
{ actionId: 'action-1', subjects: ['country', 'user'] },
|
{
|
||||||
|
actionId: 'action-1',
|
||||||
|
subjects: ['country', 'user'],
|
||||||
|
options: { fieldsRestriction: true },
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
expect(resultLevel1).toEqual([
|
expect(resultLevel1).toEqual([
|
||||||
{
|
{
|
||||||
@ -311,6 +327,21 @@ describe('Content-Type', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('cleanPermissionFields', () => {
|
describe('cleanPermissionFields', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
global.strapi = {
|
||||||
|
...global.strapi,
|
||||||
|
admin: {
|
||||||
|
services: {
|
||||||
|
permission: {
|
||||||
|
actionProvider: {
|
||||||
|
getByActionId: () => ({ options: { fieldsRestriction: true } }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const tests = [
|
const tests = [
|
||||||
[undefined, ['firstname', 'car']],
|
[undefined, ['firstname', 'car']],
|
||||||
[null, ['firstname', 'car']],
|
[null, ['firstname', 'car']],
|
||||||
@ -336,6 +367,7 @@ describe('Content-Type', () => {
|
|||||||
{
|
{
|
||||||
subject: 'user',
|
subject: 'user',
|
||||||
fields,
|
fields,
|
||||||
|
options: { fieldsRestriction: true },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
@ -346,6 +378,7 @@ describe('Content-Type', () => {
|
|||||||
{
|
{
|
||||||
subject: 'user',
|
subject: 'user',
|
||||||
fields: expectedFields,
|
fields: expectedFields,
|
||||||
|
options: { fieldsRestriction: true },
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const fp = require('lodash/fp');
|
const fp = require('lodash/fp');
|
||||||
|
const actionDomain = require('../domain/action');
|
||||||
|
|
||||||
const EXCLUDE_FIELDS = ['created_by', 'updated_by'];
|
const EXCLUDE_FIELDS = ['created_by', 'updated_by'];
|
||||||
|
|
||||||
@ -118,7 +119,7 @@ const getPermissionsWithNestedFields = (actions, { nestingLevel, restrictedSubje
|
|||||||
action.subjects
|
action.subjects
|
||||||
.filter(subject => !restrictedSubjects.includes(subject))
|
.filter(subject => !restrictedSubjects.includes(subject))
|
||||||
.forEach(contentTypeUid => {
|
.forEach(contentTypeUid => {
|
||||||
const fields = action.options.fieldsGranularity
|
const fields = actionDomain.hasFieldsRestriction(action)
|
||||||
? getNestedFields(strapi.contentTypes[contentTypeUid], {
|
? getNestedFields(strapi.contentTypes[contentTypeUid], {
|
||||||
components: strapi.components,
|
components: strapi.components,
|
||||||
nestingLevel,
|
nestingLevel,
|
||||||
@ -141,13 +142,13 @@ const getPermissionsWithNestedFields = (actions, { nestingLevel, restrictedSubje
|
|||||||
* @param {number} options.nestingLevel level of nesting
|
* @param {number} options.nestingLevel level of nesting
|
||||||
* @returns {array<permissions>}
|
* @returns {array<permissions>}
|
||||||
*/
|
*/
|
||||||
const cleanPermissionFields = (permissions, { nestingLevel }) =>
|
const cleanPermissionFields = (permissions, { nestingLevel } = {}) =>
|
||||||
permissions.map(perm => {
|
permissions.map(perm => {
|
||||||
const { action: actionId, fields, subject } = perm;
|
const { action: actionId, fields, subject } = perm;
|
||||||
const action = strapi.admin.services.permission.actionProvider.getByActionId(actionId);
|
const action = strapi.admin.services.permission.actionProvider.getByActionId(actionId);
|
||||||
let newFields = fields;
|
let newFields = fields;
|
||||||
|
|
||||||
if (!action.options.fieldsGranularity) {
|
if (!actionDomain.hasFieldsRestriction(action)) {
|
||||||
newFields = null;
|
newFields = null;
|
||||||
} else if (subject && strapi.contentTypes[subject]) {
|
} else if (subject && strapi.contentTypes[subject]) {
|
||||||
const possibleFields = getNestedFieldsWithIntermediate(strapi.contentTypes[subject], {
|
const possibleFields = getNestedFieldsWithIntermediate(strapi.contentTypes[subject], {
|
||||||
|
@ -62,6 +62,8 @@ const createActionProvider = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actions.set(actionId, createAction(newAction));
|
actions.set(actionId, createAction(newAction));
|
||||||
|
|
||||||
|
return this;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -63,7 +63,7 @@ const registerProviderActionSchema = yup
|
|||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
options: yup.object({
|
options: yup.object({
|
||||||
fieldsGranularity: yup.boolean(),
|
fieldsRestriction: yup.boolean(),
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.noUnknown()
|
.noUnknown()
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const { yup } = require('strapi-utils');
|
const { yup } = require('strapi-utils');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const actionDomain = require('../domain/action');
|
||||||
const {
|
const {
|
||||||
checkFieldsAreCorrectlyNested,
|
checkFieldsAreCorrectlyNested,
|
||||||
checkFieldsDontHaveDuplicates,
|
checkFieldsDontHaveDuplicates,
|
||||||
@ -45,11 +46,18 @@ const arrayOfConditionNames = yup
|
|||||||
: this.createError({ path: this.path, message: `contains conditions that don't exist` });
|
: this.createError({ path: this.path, message: `contains conditions that don't exist` });
|
||||||
});
|
});
|
||||||
|
|
||||||
const checkCTPermsDeleteHaveFieldsToNull = permissions =>
|
const checkContentTypesFieldsRestriction = permissions => {
|
||||||
!Array.isArray(permissions) ||
|
const { actionProvider } = strapi.admin.services.permission;
|
||||||
permissions.every(
|
|
||||||
perm => perm.action !== 'plugins::content-manager.explorer.delete' || _.isNil(perm.fields)
|
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) =>
|
const permissionsAreEquals = (a, b) =>
|
||||||
a.action === b.action && (a.subject === b.subject || (_.isNil(a.subject) && _.isNil(b.subject)));
|
a.action === b.action && (a.subject === b.subject || (_.isNil(a.subject) && _.isNil(b.subject)));
|
||||||
@ -94,9 +102,9 @@ const updatePermissions = yup
|
|||||||
checkNoDuplicatedPermissions
|
checkNoDuplicatedPermissions
|
||||||
)
|
)
|
||||||
.test(
|
.test(
|
||||||
'delete-fields-are-null',
|
'fields-restriction',
|
||||||
'The action "plugins::content-manager.explorer.delete" must have fields set to null or undefined',
|
'The actions must have fields set to null or undefined',
|
||||||
checkCTPermsDeleteHaveFieldsToNull
|
checkContentTypesFieldsRestriction
|
||||||
)
|
)
|
||||||
.noUnknown()
|
.noUnknown()
|
||||||
),
|
),
|
||||||
|
@ -102,9 +102,9 @@ const checkPermissionsExist = function(permissions) {
|
|||||||
const failIndex = permissions.findIndex(
|
const failIndex = permissions.findIndex(
|
||||||
permission =>
|
permission =>
|
||||||
!existingActions.some(
|
!existingActions.some(
|
||||||
ea =>
|
action =>
|
||||||
ea.actionId === permission.action &&
|
action.actionId === permission.action &&
|
||||||
(ea.section !== 'contentTypes' || ea.subjects.includes(permission.subject))
|
(action.section !== 'contentTypes' || action.subjects.includes(permission.subject))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ const syncComponentsSchemas = async () => {
|
|||||||
const componentsToAdd = _.difference(realUIDs, DBUIDs);
|
const componentsToAdd = _.difference(realUIDs, DBUIDs);
|
||||||
const componentsToDelete = _.difference(DBUIDs, realUIDs);
|
const componentsToDelete = _.difference(DBUIDs, realUIDs);
|
||||||
|
|
||||||
// delette old schemas
|
// delete old schemas
|
||||||
await Promise.all(componentsToDelete.map(uid => componentService.deleteConfiguration(uid)));
|
await Promise.all(componentsToDelete.map(uid => componentService.deleteConfiguration(uid)));
|
||||||
|
|
||||||
// create new schemas
|
// create new schemas
|
||||||
@ -125,7 +125,7 @@ const registerPermissions = () => {
|
|||||||
pluginName: 'content-manager',
|
pluginName: 'content-manager',
|
||||||
subjects: contentTypesUids,
|
subjects: contentTypesUids,
|
||||||
options: {
|
options: {
|
||||||
fieldsGranularity: false,
|
fieldsRestriction: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -135,7 +135,7 @@ const registerPermissions = () => {
|
|||||||
pluginName: 'content-manager',
|
pluginName: 'content-manager',
|
||||||
subjects: contentTypesUids.filter(hasDraftAndPublish),
|
subjects: contentTypesUids.filter(hasDraftAndPublish),
|
||||||
options: {
|
options: {
|
||||||
fieldsGranularity: false,
|
fieldsRestriction: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user