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

139 lines
4.5 KiB
JavaScript
Executable File

'use strict';
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
module.exports = async () => {
// Check if the plugin users-permissions is installed because the documentation needs it
if (Object.keys(strapi.plugins).indexOf('users-permissions') === -1) {
throw new Error(
'In order to make the documentation plugin works the users-permissions one is required'
);
}
const pluginStore = strapi.store({
environment: '',
type: 'plugin',
name: 'documentation',
});
const restrictedAccess = await pluginStore.get({ key: 'config' });
if (!restrictedAccess) {
pluginStore.set({ key: 'config', value: { restrictedAccess: false } });
}
let shouldUpdateFullDoc = false;
const services = strapi.plugins['documentation'].services.documentation;
// Generate plugins' documentation
const pluginsWithDocumentationNeeded = services.getPluginsWithDocumentationNeeded();
pluginsWithDocumentationNeeded.forEach(plugin => {
const isDocExisting = services.checkIfPluginDocumentationFolderExists(plugin);
if (!isDocExisting) {
services.createDocumentationDirectory(services.getPluginDocumentationPath(plugin));
// create the overrides directory
services.createDocumentationDirectory(services.getPluginOverrideDocumentationPath(plugin));
services.createPluginDocumentationFile(plugin);
shouldUpdateFullDoc = true;
} else {
const needToUpdatePluginDoc = services.checkIfPluginDocNeedsUpdate(plugin);
if (needToUpdatePluginDoc) {
services.createPluginDocumentationFile(plugin);
shouldUpdateFullDoc = true;
}
}
});
// Retrieve all the apis from the apis directory
const apis = services.getApis();
// Generate APIS' documentation
apis.forEach(api => {
const isDocExisting = services.checkIfDocumentationFolderExists(api);
if (!isDocExisting) {
// If the documentation directory doesn't exist create it
services.createDocumentationDirectory(services.getDocumentationPath(api));
// Create the overrides directory
services.createDocumentationDirectory(services.getDocumentationOverridesPath(api));
// Create the documentation files per version
services.createDocumentationFile(api); // Then create the {api}.json documentation file
shouldUpdateFullDoc = true;
} else {
const needToUpdateAPIDoc = services.checkIfAPIDocNeedsUpdate(api);
if (needToUpdateAPIDoc) {
services.createDocumentationFile(api);
shouldUpdateFullDoc = true;
}
}
});
const fullDoc = services.generateFullDoc();
// Verify that the correct documentation folder exists in the documentation plugin
const isMergedDocumentationExists = services.checkIfMergedDocumentationFolderExists();
const documentationPath = services.getMergedDocumentationPath();
if (isMergedDocumentationExists) {
/**
* Retrieve all tags from the documentation and join them
* @param {Object} documentation
* @returns {String}
*/
const getDocTagsToString = documentation => {
return _.get(documentation, 'tags', [])
.map(tag => {
return tag.name.toLowerCase();
})
.sort((a, b) => a - b)
.join('.');
};
const oldDoc = require(path.resolve(documentationPath, 'full_documentation.json'));
const oldDocTags = getDocTagsToString(oldDoc);
const currentDocTags = getDocTagsToString(fullDoc);
// If the tags are different (an api has been deleted) we need to rebuild the documentation
if (oldDocTags !== currentDocTags) {
shouldUpdateFullDoc = true;
}
}
if (!isMergedDocumentationExists || shouldUpdateFullDoc) {
// Create the folder
services.createDocumentationDirectory(documentationPath);
// Write the file
fs.writeFileSync(
path.resolve(documentationPath, 'full_documentation.json'),
JSON.stringify(fullDoc, null, 2),
'utf8'
);
}
// Add permissions
const actions = [
{
section: 'plugins',
displayName: 'Access the Documentation',
uid: 'read',
pluginName: 'documentation',
},
{
section: 'plugins',
displayName: 'Update and delete',
uid: 'settings.update',
subCategory: 'settings',
pluginName: 'documentation',
},
{
section: 'plugins',
displayName: 'Regenerate',
uid: 'settings.regenerate',
subCategory: 'settings',
pluginName: 'documentation',
},
];
await strapi.admin.services.permission.actionProvider.registerMany(actions);
};