mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 15:44:59 +00:00
fix: excludeNotCreatableFields
This commit is contained in:
parent
b62df89641
commit
ec434498d1
@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const { set } = require('lodash/fp');
|
||||
const strapiUtils = require('@strapi/utils');
|
||||
|
||||
const { isVisibleAttribute } = strapiUtils.contentTypes;
|
||||
@ -34,6 +35,51 @@ const hasProhibitedCloningFields = (uid) => {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Iterates all attributes of the content type, and removes the ones that are not creatable.
|
||||
* - If it's a relation, it sets the value to [] or null.
|
||||
* - If it's a regular attribute, it sets the value to null.
|
||||
* When cloning, if you don't set a field it will be copied from the original entry. So we need to
|
||||
* remove the fields that the user can't create.
|
||||
*/
|
||||
const excludeNotCreatableFields =
|
||||
(uid, permissionChecker) =>
|
||||
(body, path = []) => {
|
||||
const model = strapi.getModel(uid);
|
||||
const canCreate = (path) => permissionChecker.can.create(null, path);
|
||||
|
||||
return Object.keys(model.attributes).reduce((body, attributeName) => {
|
||||
const attribute = model.attributes[attributeName];
|
||||
const attributePath = [...path, attributeName].join('.');
|
||||
|
||||
// Ignore the attribute if it's not visible
|
||||
if (!isVisibleAttribute(model, attributeName)) {
|
||||
return body;
|
||||
}
|
||||
|
||||
switch (attribute.type) {
|
||||
// Relation should be empty if the user can't create it
|
||||
case 'relation': {
|
||||
if (canCreate(attributePath)) return body;
|
||||
return set(attributePath, { set: [] }, body);
|
||||
}
|
||||
// Go deeper into the component
|
||||
case 'component': {
|
||||
return excludeNotCreatableFields(attribute.component, permissionChecker)(body, [
|
||||
...path,
|
||||
attributeName,
|
||||
]);
|
||||
}
|
||||
// Attribute should be null if the user can't create it
|
||||
default: {
|
||||
if (canCreate(attributePath)) return body;
|
||||
return set(attributePath, null, body);
|
||||
}
|
||||
}
|
||||
}, body);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
hasProhibitedCloningFields,
|
||||
excludeNotCreatableFields,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user