2020-06-29 16:32:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const { subject: asSubject } = require('@casl/ability');
|
|
|
|
const { permittedFieldsOf, rulesToQuery } = require('@casl/ability/extra');
|
2020-07-01 18:08:21 +02:00
|
|
|
const { VALID_REST_OPERATORS, sanitizeEntity } = require('strapi-utils');
|
2020-06-29 16:32:14 +02:00
|
|
|
|
2020-07-01 15:44:28 +02:00
|
|
|
const ops = {
|
|
|
|
common: VALID_REST_OPERATORS,
|
|
|
|
boolean: ['$or'],
|
|
|
|
cleanable: ['$elemMatch'],
|
|
|
|
};
|
|
|
|
|
2020-06-29 16:32:14 +02:00
|
|
|
module.exports = (ability, action, model) => ({
|
|
|
|
ability,
|
|
|
|
action,
|
|
|
|
model,
|
|
|
|
|
|
|
|
get query() {
|
|
|
|
return buildStrapiQuery(buildCaslQuery(ability, action, model));
|
|
|
|
},
|
|
|
|
|
|
|
|
toSubject(target, subjectType = model) {
|
|
|
|
return asSubject(subjectType, target);
|
|
|
|
},
|
|
|
|
|
2020-07-01 13:03:30 +02:00
|
|
|
pickPermittedFieldsOf(data, options = {}) {
|
2020-07-01 18:08:21 +02:00
|
|
|
return this.sanitize(data, { ...options, isOutput: false });
|
2020-07-01 13:03:30 +02:00
|
|
|
},
|
|
|
|
|
2020-06-29 16:32:14 +02:00
|
|
|
sanitize(data, options = {}) {
|
2020-07-01 13:03:30 +02:00
|
|
|
const {
|
|
|
|
subject = this.toSubject(data),
|
|
|
|
action: actionOverride = action,
|
|
|
|
withPrivate = true,
|
2020-07-01 18:08:21 +02:00
|
|
|
isOutput = true,
|
2020-07-01 13:03:30 +02:00
|
|
|
} = options;
|
|
|
|
|
|
|
|
if (_.isArray(data)) {
|
|
|
|
return data.map(this.sanitize.bind(this));
|
|
|
|
}
|
2020-06-29 16:32:14 +02:00
|
|
|
|
|
|
|
const permittedFields = permittedFieldsOf(ability, actionOverride, subject);
|
|
|
|
|
2020-07-01 18:08:21 +02:00
|
|
|
return sanitizeEntity(data, {
|
|
|
|
model: strapi.getModel(model),
|
|
|
|
includeFields: _.isEmpty(permittedFields) ? null : permittedFields,
|
2020-07-01 13:03:30 +02:00
|
|
|
withPrivate,
|
2020-07-01 18:08:21 +02:00
|
|
|
isOutput,
|
2020-07-01 13:03:30 +02:00
|
|
|
});
|
2020-06-29 16:32:14 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const buildCaslQuery = (ability, action, model) => {
|
|
|
|
const query = rulesToQuery(ability, action, model, o => o.conditions);
|
2020-07-01 15:44:28 +02:00
|
|
|
return query && _.has(query, '$or') ? _.pick(query, '$or') : {};
|
2020-06-29 16:32:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const buildStrapiQuery = caslQuery => {
|
2020-07-01 15:44:28 +02:00
|
|
|
const transform = _.flow([flattenDeep, cleanupUnwantedProperties]);
|
|
|
|
return transform(caslQuery);
|
2020-06-29 16:32:14 +02:00
|
|
|
};
|
|
|
|
|
2020-07-01 15:44:28 +02:00
|
|
|
const flattenDeep = condition => {
|
|
|
|
if (_.isArray(condition)) {
|
|
|
|
return _.map(condition, flattenDeep);
|
|
|
|
}
|
|
|
|
|
|
|
|
const shouldIgnore = e => !!ops.common.includes(e);
|
|
|
|
const shouldPerformTransformation = (v, k) => _.isObject(v) && !_.isArray(v) && !shouldIgnore(k);
|
|
|
|
|
2020-06-29 16:32:14 +02:00
|
|
|
const result = {};
|
2020-07-01 15:44:28 +02:00
|
|
|
const set = (key, value) => (result[key] = value);
|
|
|
|
|
|
|
|
const getTransformParams = (prevKey, v, k) =>
|
|
|
|
shouldIgnore(k) ? [`${prevKey}_${k.replace('$', '')}`, v] : [`${prevKey}.${k}`, v];
|
2020-06-29 16:32:14 +02:00
|
|
|
|
|
|
|
_.each(condition, (value, key) => {
|
2020-07-01 15:44:28 +02:00
|
|
|
if (ops.boolean.includes(key)) {
|
|
|
|
set(key.replace('$', '_'), _.map(value, flattenDeep));
|
|
|
|
} else if (shouldPerformTransformation(value, key)) {
|
|
|
|
_.each(flattenDeep(value), (v, k) => {
|
|
|
|
set(...getTransformParams(key, v, k));
|
2020-06-29 16:32:14 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-07-01 15:44:28 +02:00
|
|
|
set(key, value);
|
2020-06-29 16:32:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2020-07-01 15:44:28 +02:00
|
|
|
const cleanupUnwantedProperties = condition => {
|
|
|
|
const shouldClean = e => ops.cleanable.find(o => e.includes(`.${o}`));
|
|
|
|
|
|
|
|
return _.reduce(
|
|
|
|
condition,
|
|
|
|
(acc, value, key) => ({
|
2020-06-29 16:32:14 +02:00
|
|
|
...acc,
|
2020-07-01 15:44:28 +02:00
|
|
|
[shouldClean(key) ? key.split(`.${shouldClean(key)}`).join('') : key]: value,
|
2020-06-29 16:32:14 +02:00
|
|
|
}),
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
};
|