2020-10-28 18:47:14 +01:00
|
|
|
'use strict';
|
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const { pipeAsync } = require('@strapi/utils');
|
|
|
|
|
2020-10-28 18:47:14 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const createPermissionChecker =
|
|
|
|
(strapi) =>
|
|
|
|
({ userAbility, model }) => {
|
|
|
|
const permissionsManager = strapi.admin.services.permission.createPermissionsManager({
|
|
|
|
ability: userAbility,
|
|
|
|
model,
|
|
|
|
});
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const toSubject = (entity) => (entity ? permissionsManager.toSubject(entity, model) : model);
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const can = (action, entity, field) => {
|
|
|
|
return userAbility.can(action, toSubject(entity), field);
|
|
|
|
};
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const cannot = (action, entity, field) => {
|
|
|
|
return userAbility.cannot(action, toSubject(entity), field);
|
|
|
|
};
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const sanitizeOutput = (data, { action = ACTIONS.read } = {}) => {
|
|
|
|
return permissionsManager.sanitizeOutput(data, { subject: toSubject(data), action });
|
|
|
|
};
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const sanitizeQuery = (query, { action = ACTIONS.read } = {}) => {
|
|
|
|
return permissionsManager.sanitizeQuery(query, { subject: model, action });
|
|
|
|
};
|
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const sanitizeInput = (action, data, entity) => {
|
|
|
|
return permissionsManager.sanitizeInput(data, {
|
|
|
|
subject: entity ? toSubject(entity) : model,
|
|
|
|
action,
|
|
|
|
});
|
|
|
|
};
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const sanitizeCreateInput = (data) => sanitizeInput(ACTIONS.create, data);
|
|
|
|
const sanitizeUpdateInput = (entity) => (data) => sanitizeInput(ACTIONS.update, data, entity);
|
2020-11-03 16:48:14 +01:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const buildPermissionQuery = (query, action) => {
|
|
|
|
return permissionsManager.addPermissionsQueryTo(query, action);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} query
|
|
|
|
* @param {keyof typeof ACTIONS} action
|
|
|
|
*/
|
|
|
|
const sanitizedQuery = (query, action) => {
|
|
|
|
return pipeAsync(
|
|
|
|
(q) => sanitizeQuery(q, action),
|
|
|
|
(q) => buildPermissionQuery(q, action)
|
|
|
|
)(query);
|
|
|
|
};
|
2021-01-26 10:18:43 +01:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
// Sanitized queries shortcuts
|
|
|
|
Object.keys(ACTIONS).forEach((action) => {
|
2023-03-15 18:37:42 +01:00
|
|
|
sanitizedQuery[action] = (query) => sanitizedQuery(query, ACTIONS[action]);
|
2023-02-09 11:35:50 +01:00
|
|
|
});
|
2020-11-03 16:48:14 +01:00
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
// Permission utils shortcuts
|
2022-08-08 23:33:39 +02:00
|
|
|
Object.keys(ACTIONS).forEach((action) => {
|
|
|
|
can[action] = (...args) => can(ACTIONS[action], ...args);
|
|
|
|
cannot[action] = (...args) => cannot(ACTIONS[action], ...args);
|
|
|
|
});
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
return {
|
2023-02-09 11:35:50 +01:00
|
|
|
// Permission utils
|
2022-08-08 23:33:39 +02:00
|
|
|
can,
|
|
|
|
cannot,
|
2023-02-09 11:35:50 +01:00
|
|
|
// Sanitizers
|
2022-08-08 23:33:39 +02:00
|
|
|
sanitizeOutput,
|
2023-02-09 11:35:50 +01:00
|
|
|
sanitizeQuery,
|
2022-08-08 23:33:39 +02:00
|
|
|
sanitizeCreateInput,
|
|
|
|
sanitizeUpdateInput,
|
2023-02-09 11:35:50 +01:00
|
|
|
// Queries Builder
|
|
|
|
sanitizedQuery,
|
2022-08-08 23:33:39 +02:00
|
|
|
};
|
2020-10-28 18:47:14 +01:00
|
|
|
};
|
|
|
|
|
2021-07-13 18:46:36 +02:00
|
|
|
module.exports = ({ strapi }) => ({
|
|
|
|
create: createPermissionChecker(strapi),
|
|
|
|
});
|