strapi/packages/core/utils/lib/sanitize-entity.js

173 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const {
constants,
isPrivateAttribute,
getNonWritableAttributes,
getNonVisibleAttributes,
getWritableAttributes,
} = require('./content-types');
2021-07-08 21:53:30 +02:00
const { ID_ATTRIBUTE, CREATED_AT_ATTRIBUTE, UPDATED_AT_ATTRIBUTE } = constants;
const sanitizeEntity = (dataSource, options) => {
const { model, withPrivate = false, isOutput = true, includeFields = null } = options;
2019-09-20 09:56:00 +02:00
if (typeof dataSource !== 'object' || _.isNil(dataSource)) {
return dataSource;
}
const data = parseOriginalData(dataSource);
if (typeof data !== 'object' || _.isNil(data)) {
return data;
}
if (_.isArray(data)) {
return data.map(entity => sanitizeEntity(entity, options));
}
if (_.isNil(model)) {
if (isOutput) {
return null;
} else {
return data;
}
}
const { attributes } = model;
const allowedFields = getAllowedFields({ includeFields, model, isOutput });
const reducerFn = (acc, value, key) => {
const attribute = attributes[key];
const allowedFieldsHasKey = allowedFields.includes(key);
if (shouldRemoveAttribute(model, key, attribute, { withPrivate, isOutput })) {
return acc;
}
// Relations
const relation = attribute && (attribute.model || attribute.collection || attribute.component);
if (relation) {
if (_.isNil(value)) {
return { ...acc, [key]: value };
}
const [nextFields, isAllowed] = includeFields
? getNextFields(allowedFields, key, { allowedFieldsHasKey })
: [null, true];
if (!isAllowed) {
return acc;
}
const baseOptions = {
withPrivate,
isOutput,
includeFields: nextFields,
};
let sanitizeFn;
if (relation === '*') {
sanitizeFn = entity => {
if (_.isNil(entity) || !_.has(entity, '__contentType')) {
return entity;
}
return sanitizeEntity(entity, {
2021-07-19 12:39:43 +02:00
model: strapi.getModel(entity.__contentType),
...baseOptions,
});
};
} else {
sanitizeFn = entity =>
sanitizeEntity(entity, {
model: strapi.getModel(relation, attribute.plugin),
...baseOptions,
});
}
const nextVal = Array.isArray(value) ? value.map(sanitizeFn) : sanitizeFn(value);
return { ...acc, [key]: nextVal };
}
const isAllowedField = !includeFields || allowedFieldsHasKey;
// Dynamic zones
if (attribute && attribute.type === 'dynamiczone' && value !== null && isAllowedField) {
const nextVal = value.map(elem =>
sanitizeEntity(elem, {
model: strapi.getModel(elem.__component),
withPrivate,
isOutput,
})
);
return { ...acc, [key]: nextVal };
}
// Other fields
if (isAllowedField) {
return { ...acc, [key]: value };
}
return acc;
};
return _.reduce(data, reducerFn, {});
};
const parseOriginalData = data => (_.isFunction(data.toJSON) ? data.toJSON() : data);
const COMPONENT_FIELDS = ['__component'];
2021-06-30 20:00:03 +02:00
const STATIC_FIELDS = [ID_ATTRIBUTE];
const getAllowedFields = ({ includeFields, model, isOutput }) => {
const nonWritableAttributes = getNonWritableAttributes(model);
const nonVisibleAttributes = getNonVisibleAttributes(model);
const writableAttributes = getWritableAttributes(model);
const nonVisibleWritableAttributes = _.intersection(writableAttributes, nonVisibleAttributes);
return _.concat(
includeFields || [],
...(isOutput
? [
STATIC_FIELDS,
2021-07-08 21:53:30 +02:00
CREATED_AT_ATTRIBUTE,
UPDATED_AT_ATTRIBUTE,
COMPONENT_FIELDS,
...nonWritableAttributes,
...nonVisibleAttributes,
]
2021-06-30 20:00:03 +02:00
: [STATIC_FIELDS, COMPONENT_FIELDS, ...nonVisibleWritableAttributes])
);
};
const getNextFields = (fields, key, { allowedFieldsHasKey }) => {
const searchStr = `${key}.`;
const transformedFields = (fields || [])
.filter(field => field.startsWith(searchStr))
.map(field => field.replace(searchStr, ''));
const isAllowed = allowedFieldsHasKey || transformedFields.length > 0;
const nextFields = allowedFieldsHasKey ? null : transformedFields;
return [nextFields, isAllowed];
};
const shouldRemoveAttribute = (model, key, attribute = {}, { withPrivate, isOutput }) => {
const isPassword = attribute.type === 'password';
Hide creator fields from public api by default (#8052) * Add model option to hide/show creators fields in public API response Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add content-types util, rework sanitize-entity's private handling Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update search e2e tests, fix an issue on empty search for the core-api controller (find) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix GraphQL plugin (handle privates attributes on typeDefs + resolver builds) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix sanitizeEntity import Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Move doc update from beta to stable Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix e2e test Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix pr comments Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove creator's field from upload controller routes Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix typedef build for graphql association Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix pr (comments + several issues) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add tests for search behavior in content-manager Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Rename files variables to meaningful names (upload controllers) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix test with search id matching serialNumber Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com> * Add toHasBeenCalledWith check for config.get (utils/content-types.test.js) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> Co-authored-by: Alexandre Bodin <bodin.alex@gmail.com>
2020-10-01 17:47:08 +02:00
const isPrivate = isPrivateAttribute(model, key);
const shouldRemovePassword = isOutput;
const shouldRemovePrivate = !withPrivate && isOutput;
return !!((isPassword && shouldRemovePassword) || (isPrivate && shouldRemovePrivate));
};
Hide creator fields from public api by default (#8052) * Add model option to hide/show creators fields in public API response Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add content-types util, rework sanitize-entity's private handling Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update search e2e tests, fix an issue on empty search for the core-api controller (find) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix GraphQL plugin (handle privates attributes on typeDefs + resolver builds) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix sanitizeEntity import Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Move doc update from beta to stable Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix e2e test Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix pr comments Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove creator's field from upload controller routes Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix typedef build for graphql association Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix pr (comments + several issues) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add tests for search behavior in content-manager Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Rename files variables to meaningful names (upload controllers) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix test with search id matching serialNumber Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com> * Add toHasBeenCalledWith check for config.get (utils/content-types.test.js) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> Co-authored-by: Alexandre Bodin <bodin.alex@gmail.com>
2020-10-01 17:47:08 +02:00
module.exports = sanitizeEntity;