Jean-Sébastien Herbaux 1e5b1c99f6
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

144 lines
3.6 KiB
JavaScript

'use strict';
const { convertToStrapiError } = require('../../errors');
module.exports = async () => {
// set plugin store
const configurator = strapi.store({
type: 'plugin',
name: 'upload',
key: 'settings',
});
strapi.plugins.upload.provider = createProvider(strapi.plugins.upload.config || {});
// if provider config does not exist set one by default
const config = await configurator.get();
if (!config) {
await configurator.set({
value: {
sizeOptimization: true,
responsiveDimensions: true,
},
});
}
await pruneObsoleteRelations();
await registerPermissionActions();
};
const wrapFunctionForErrors = fn => async (...args) => {
try {
return await fn(...args);
} catch (err) {
throw convertToStrapiError(err);
}
};
const createProvider = ({ provider, providerOptions, actionOptions = {} }) => {
try {
const providerInstance = require(`strapi-provider-upload-${provider}`).init(providerOptions);
return Object.assign(Object.create(baseProvider), {
...providerInstance,
upload: wrapFunctionForErrors((file, options = actionOptions.upload) => {
return providerInstance.upload(file, options);
}),
delete: wrapFunctionForErrors((file, options = actionOptions.delete) => {
return providerInstance.delete(file, options);
}),
});
} catch (err) {
strapi.log.error(err);
throw new Error(
`The provider package isn't installed. Please run \`npm install strapi-provider-upload-${provider}\``
);
}
};
const baseProvider = {
extend(obj) {
Object.assign(this, obj);
},
upload() {
throw new Error('Provider upload method is not implemented');
},
delete() {
throw new Error('Provider delete method is not implemented');
},
};
const pruneObsoleteRelations = async () => {
const { upload: plugin } = strapi.plugins;
const modelIsNotDefined = !plugin || !plugin.models || !plugin.models.file;
if (modelIsNotDefined) {
return Promise.resolve();
}
await strapi.query('file', 'upload').custom(pruneObsoleteRelationsQuery)();
};
const pruneObsoleteRelationsQuery = ({ model }) => {
if (model.orm !== 'mongoose') {
return Promise.resolve();
}
const models = Array.from(strapi.db.models.values());
const modelsId = models.map(model => model.globalId);
return model.updateMany(
{ related: { $elemMatch: { kind: { $nin: modelsId } } } },
{ $pull: { related: { kind: { $nin: modelsId } } } }
);
};
const registerPermissionActions = async () => {
const actions = [
{
section: 'plugins',
displayName: 'Access the Media Library',
uid: 'read',
pluginName: 'upload',
},
{
section: 'plugins',
displayName: 'Create (upload)',
uid: 'assets.create',
subCategory: 'assets',
pluginName: 'upload',
},
{
section: 'plugins',
displayName: 'Update (crop, details, replace) + delete',
uid: 'assets.update',
subCategory: 'assets',
pluginName: 'upload',
},
{
section: 'plugins',
displayName: 'Download',
uid: 'assets.download',
subCategory: 'assets',
pluginName: 'upload',
},
{
section: 'plugins',
displayName: 'Copy link',
uid: 'assets.copy-link',
subCategory: 'assets',
pluginName: 'upload',
},
{
section: 'settings',
displayName: 'Access the Media Library settings page',
uid: 'settings.read',
category: 'media library',
pluginName: 'upload',
},
];
await strapi.admin.services.permission.actionProvider.registerMany(actions);
};