2020-10-28 18:47:14 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const ACTIONS = {
|
2021-08-06 18:09:49 +02:00
|
|
|
read: 'plugin::content-manager.explorer.read',
|
|
|
|
create: 'plugin::content-manager.explorer.create',
|
|
|
|
update: 'plugin::content-manager.explorer.update',
|
|
|
|
delete: 'plugin::content-manager.explorer.delete',
|
|
|
|
publish: 'plugin::content-manager.explorer.publish',
|
|
|
|
unpublish: 'plugin::content-manager.explorer.publish',
|
2020-10-28 18:47:14 +01:00
|
|
|
};
|
|
|
|
|
2021-07-13 18:46:36 +02:00
|
|
|
const createPermissionChecker = strapi => ({ userAbility, model }) => {
|
2020-10-28 18:47:14 +01:00
|
|
|
const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
|
|
|
|
ability: userAbility,
|
|
|
|
model,
|
|
|
|
});
|
|
|
|
|
|
|
|
const toSubject = entity => (entity ? permissionsManager.toSubject(entity, model) : model);
|
|
|
|
|
2020-12-01 16:38:47 +01:00
|
|
|
const can = (action, entity, field) => {
|
|
|
|
return userAbility.can(action, toSubject(entity), field);
|
2020-10-28 18:47:14 +01:00
|
|
|
};
|
|
|
|
|
2020-12-01 16:38:47 +01:00
|
|
|
const cannot = (action, entity, field) => {
|
|
|
|
return userAbility.cannot(action, toSubject(entity), field);
|
2020-10-28 18:47:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const sanitizeOutput = (data, { action = ACTIONS.read } = {}) => {
|
|
|
|
return permissionsManager.sanitize(data, {
|
|
|
|
subject: toSubject(data),
|
|
|
|
action,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const sanitizeInput = (action, data, entity) => {
|
|
|
|
return permissionsManager.sanitize(data, {
|
2020-11-27 11:52:00 +01:00
|
|
|
subject: entity ? toSubject(entity) : model,
|
2020-10-28 18:47:14 +01:00
|
|
|
action,
|
|
|
|
isOutput: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-03 16:48:14 +01:00
|
|
|
const sanitizeCreateInput = data => sanitizeInput(ACTIONS.create, data);
|
|
|
|
const sanitizeUpdateInput = entity => data => sanitizeInput(ACTIONS.update, data, entity);
|
|
|
|
|
2021-01-26 10:18:43 +01:00
|
|
|
const buildPermissionQuery = (query, action) => permissionsManager.queryFrom(query, action);
|
|
|
|
|
|
|
|
const buildReadQuery = query => buildPermissionQuery(query, ACTIONS.read);
|
|
|
|
const buildDeleteQuery = query => buildPermissionQuery(query, ACTIONS.delete);
|
2020-11-03 16:48:14 +01:00
|
|
|
|
2020-10-28 18:47:14 +01:00
|
|
|
Object.keys(ACTIONS).forEach(action => {
|
|
|
|
can[action] = (...args) => can(ACTIONS[action], ...args);
|
|
|
|
cannot[action] = (...args) => cannot(ACTIONS[action], ...args);
|
|
|
|
});
|
|
|
|
|
2020-11-03 16:48:14 +01:00
|
|
|
return {
|
2020-10-28 18:47:14 +01:00
|
|
|
can,
|
|
|
|
cannot,
|
|
|
|
sanitizeOutput,
|
2020-11-03 16:48:14 +01:00
|
|
|
sanitizeCreateInput,
|
|
|
|
sanitizeUpdateInput,
|
2021-01-26 10:18:43 +01:00
|
|
|
buildReadQuery,
|
|
|
|
buildDeleteQuery,
|
2020-10-28 18:47:14 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-07-13 18:46:36 +02:00
|
|
|
module.exports = ({ strapi }) => ({
|
|
|
|
create: createPermissionChecker(strapi),
|
|
|
|
});
|