143 lines
4.5 KiB
JavaScript
Raw Normal View History

I18n/ permissions rework (#9535) * Add a domain layer for the permission, rework the engine handling of the permissions Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add permissions-fields-to-properties migration for the admin Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removes useless console.log Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove debug logLevel from provider-login.test.e2e.js Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Adds the new layout for the GET permissions, allow to subscribe to actionRegistered events, adds i18n handlers Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix typo Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update permissions validators Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update unit tests Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update integrations test + fix some validation issues Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Change plugins & settings section format for the permissions layout * only return locales property to localized subjects for the permission's layout * Do not send the locales property to the permission's layout when there is no locales created * Add the 'locales' property to publish & delete routes * Fix unwanted mutation of the sections builder states on multiple builds * Fix units tests with (new engine) * Fix admin-role e2e test - Add locales property to the update payload * fix e2e testsé * Update e2e snapshots * Fix unit test for i18n bootstrap * Add mocks for i18n/bootstrap test * Fix has-locale condition & updatePermission validator * Avoid mutation in migration, always authorize super admin for has-locales condition * Rework rbac domain objects, add a hook module and a provider factory * Remove old providers * Update the admin services & tests for the new rbac domain & providers * Fix tests, bootstrap functions & services following rbac domain rework * Update migration runner * PR comments Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove useless console.log * Fix sanitizeCondition bug * Section builder rework * Add test for the section-builder section & add jsdoc for the permission domain * pr comments (without the migrations) * fix fields-to-properties migration * Add jsdoc for the sections-builder * Moves createBoundAbstractDomain from permission domain to the engine service * Remove debug logLevel for admin role test (e2e) * Fix core-store * Fix hooks & move business logic from i18n bootstrap to dedicated services * add route get-non-localized-fields * use write and read permission * refacto * add input validator * add route doc * handle ST Co-authored-by: Pierre Noël <petersg83@gmail.com> Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
2021-03-25 14:59:44 +01:00
'use strict';
const { curry, pipe, merge, set, pick, omit, includes, isArray, prop } = require('lodash/fp');
/**
* Domain representation of an Action (RBAC)
* @typedef {Object} Action
* @property {string} actionId - The unique identifier of the action
* @property {string} section - The section linked to the action
* @property {string} displayName - The human readable name of an action
* @property {string} category - The main category of an action
* @property {string} [subCategory] - The secondary category of an action (only for settings and plugins section)
* @property {string} [pluginName] - The plugin which provide the action
* @property {string[]} [subjects] - A list of subjects on which the action can be applied
* @property {Object} options - The options of an action
* @property {string[]} options.applyToProperties - The list of properties that can be associated with an action
*/
/**
* Set of attributes used to create a new {@link Action} object
* @typedef {Action, { uid: string }} CreateActionPayload
*/
/**
* Return the default attributes of a new {@link Action}
* @return Partial<Action>
*/
const getDefaultActionAttributes = () => ({
options: {
applyToProperties: null,
},
});
/**
* Get the list of all the valid attributes of an {@link Action}
* @return {string[]}
*/
const actionFields = [
'section',
'displayName',
'category',
'subCategory',
'pluginName',
'subjects',
'options',
'actionId',
];
/**
* Remove unwanted attributes from an {@link Action}
* @type {function(action: Action | CreateActionPayload): Action}
*/
const sanitizeActionAttributes = pick(actionFields);
/**
* Create and return an identifier for an {@link CreateActionPayload}.
* The format is based on the action's source ({@link CreateActionPayload.pluginName} or 'application') and {@link CreateActionPayload.uid}.
* @param {CreateActionPayload} attributes
* @return {string}
*/
const computeActionId = attributes => {
const { pluginName, uid } = attributes;
if (!pluginName) {
return `application::${uid}`;
}
if (pluginName === 'admin') {
return `admin::${uid}`;
}
return `plugins::${pluginName}.${uid}`;
};
/**
* Assign an actionId attribute to an {@link CreateActionPayload} object
* @param {CreateActionPayload} attrs - Payload used to create an action
* @return {CreateActionPayload}
*/
const assignActionId = attrs => set('actionId', computeActionId(attrs), attrs);
/**
* Transform an action by adding or removing the {@link Action.subCategory} attribute
* @param {Action} action - The action to process
* @return {Action}
*/
const assignOrOmitSubCategory = action => {
const shouldHaveSubCategory = ['settings', 'plugins'].includes(action.section);
return shouldHaveSubCategory
? set('subCategory', action.subCategory || 'general', action)
: omit('subCategory', action);
};
/**
* Check if a property can be applied to an {@link Action}
* @type (function(property: string, action: Action): boolean) | (function(property: string): (function(action: Action): boolean))
* @return {boolean} Return true if the property can be applied for the given action
*/
const appliesToProperty = curry((property, action) => {
return pipe(prop('options.applyToProperties'), includes(property))(action);
});
/**
* Check if an action applies to a subject
* @param {string} subject
* @param {Action} action
* @return {boolean}
*/
const appliesToSubject = curry((subject, action) => {
return isArray(action.subjects) && includes(subject, action.subjects);
});
/**
* Transform the given attributes into a domain representation of an Action
* @type (function(payload: CreateActionPayload): Action)
* @param {CreateActionPayload} payload - The action payload containing the attributes needed to create an {@link Action}
* @return {Action} A newly created {@link Action}
*/
const create = pipe(
// Create and assign an action identifier to the action
// (need to be done before the sanitizeActionAttributes since we need the uid here)
assignActionId,
// Add or remove the sub category field based on the pluginName attribute
assignOrOmitSubCategory,
// Remove unwanted attributes from the payload
sanitizeActionAttributes,
// Complete the action creation by adding default values for some attributes
merge(getDefaultActionAttributes())
);
module.exports = {
actionFields,
appliesToProperty,
appliesToSubject,
assignActionId,
assignOrOmitSubCategory,
create,
computeActionId,
getDefaultActionAttributes,
sanitizeActionAttributes,
};