From be95294bdcd6d34d5ce1f66e26c27f138543eb53 Mon Sep 17 00:00:00 2001 From: soupette Date: Tue, 2 Apr 2019 14:17:01 +0200 Subject: [PATCH] Add lifecycles to all plugins --- .../admin/src/lifecycles.js | 17 +++++ .../admin/src/lifecycles/didGetSecuredData.js | 29 ++++++++ .../admin/src/lifecycles.js | 11 +++ .../admin/src/lifecycles.js | 11 +++ .../admin/src/lifecycles.js | 11 +++ .../admin/src/lifecycles.js | 11 +++ .../admin/src/lifecycles.js | 11 +++ .../admin/src/lifecycles.js | 35 +++++++++ .../admin/src/lifecycles/didGetSecuredData.js | 21 ++++++ .../admin/src/lifecycles/willSecure.js | 71 +++++++++++++++++++ 10 files changed, 228 insertions(+) create mode 100644 packages/strapi-plugin-content-manager/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-content-manager/admin/src/lifecycles/didGetSecuredData.js create mode 100644 packages/strapi-plugin-content-type-builder/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-documentation/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-email/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-settings-manager/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-upload/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-users-permissions/admin/src/lifecycles.js create mode 100644 packages/strapi-plugin-users-permissions/admin/src/lifecycles/didGetSecuredData.js create mode 100644 packages/strapi-plugin-users-permissions/admin/src/lifecycles/willSecure.js diff --git a/packages/strapi-plugin-content-manager/admin/src/lifecycles.js b/packages/strapi-plugin-content-manager/admin/src/lifecycles.js new file mode 100644 index 0000000000..1b9bc7c917 --- /dev/null +++ b/packages/strapi-plugin-content-manager/admin/src/lifecycles.js @@ -0,0 +1,17 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() { + // Set hooks for the AdminPage container. + // Note: we don't need to specify the first argument because we already know what "willSecure" refers to. + this.setHooks({ + didGetSecuredData: require('./lifecycles/didGetSecuredData.js'), + }); +}; diff --git a/packages/strapi-plugin-content-manager/admin/src/lifecycles/didGetSecuredData.js b/packages/strapi-plugin-content-manager/admin/src/lifecycles/didGetSecuredData.js new file mode 100644 index 0000000000..674b907615 --- /dev/null +++ b/packages/strapi-plugin-content-manager/admin/src/lifecycles/didGetSecuredData.js @@ -0,0 +1,29 @@ +const { map, omit } = require('lodash'); +const request = require('utils/request').default; +const pluginId = require('../pluginId'); + +// TODO: update needs to be updated when the models are retrieved from the admin. + +module.exports = async function didGetSecuredData() { + const { updatePlugin } = this.props; + const requestURL = `/${pluginId}/models`; + + try { + const { + models: { models }, + } = await request(requestURL, { method: 'GET' }); + const menu = [ + { + name: 'Content Types', + links: map(omit(models, 'plugins'), (model, key) => ({ + label: model.labelPlural || model.label || key, + destination: key, + })), + }, + ]; + + updatePlugin(pluginId, 'leftMenuSections', menu); + } catch (err) { + strapi.notification.error('content-manager.error.model.fetch'); + } +}; diff --git a/packages/strapi-plugin-content-type-builder/admin/src/lifecycles.js b/packages/strapi-plugin-content-type-builder/admin/src/lifecycles.js new file mode 100644 index 0000000000..b92d87a279 --- /dev/null +++ b/packages/strapi-plugin-content-type-builder/admin/src/lifecycles.js @@ -0,0 +1,11 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() {}; diff --git a/packages/strapi-plugin-documentation/admin/src/lifecycles.js b/packages/strapi-plugin-documentation/admin/src/lifecycles.js new file mode 100644 index 0000000000..b92d87a279 --- /dev/null +++ b/packages/strapi-plugin-documentation/admin/src/lifecycles.js @@ -0,0 +1,11 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() {}; diff --git a/packages/strapi-plugin-email/admin/src/lifecycles.js b/packages/strapi-plugin-email/admin/src/lifecycles.js new file mode 100644 index 0000000000..b92d87a279 --- /dev/null +++ b/packages/strapi-plugin-email/admin/src/lifecycles.js @@ -0,0 +1,11 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() {}; diff --git a/packages/strapi-plugin-settings-manager/admin/src/lifecycles.js b/packages/strapi-plugin-settings-manager/admin/src/lifecycles.js new file mode 100644 index 0000000000..b92d87a279 --- /dev/null +++ b/packages/strapi-plugin-settings-manager/admin/src/lifecycles.js @@ -0,0 +1,11 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() {}; diff --git a/packages/strapi-plugin-upload/admin/src/lifecycles.js b/packages/strapi-plugin-upload/admin/src/lifecycles.js new file mode 100644 index 0000000000..b92d87a279 --- /dev/null +++ b/packages/strapi-plugin-upload/admin/src/lifecycles.js @@ -0,0 +1,11 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() {}; diff --git a/packages/strapi-plugin-users-permissions/admin/src/lifecycles.js b/packages/strapi-plugin-users-permissions/admin/src/lifecycles.js new file mode 100644 index 0000000000..2f336a7e71 --- /dev/null +++ b/packages/strapi-plugin-users-permissions/admin/src/lifecycles.js @@ -0,0 +1,35 @@ +/* + * + * SET THE HOOKS TO ENABLE THE MAGIC OF STRAPI. + * ------------------------------------------- + * + * Secure, customise and enhance your project by setting + * the hooks via this file. + * + */ + +module.exports = function lifecycles() { + // TODO: Make it work and remove it when the split-admin PR has been merged + // const componentsToAdd = [ + // { + // area: 'NavRight', + // key: 'UsersPermissionsLogout', + // mainComponent: require('./components/Logout').default, + // }, + // ]; + + // this.setComponents(componentsToAdd); + + // Set hooks for the AdminPage container. + // Note: we don't need to specify the first argument because we already know what "willSecure" refers to. + this.setHooks({ + didGetSecuredData: require('./lifecycles/didGetSecuredData.js'), + willSecure: require('./lifecycles/willSecure.js'), + }); + + // Set hooks for the App container of the Content Manager. + // Note: we have to specify the first argument to select a specific container which is located in a plugin, or not. + // this.setHooks('content-manager.App', { + // willSomething: (props, store) => { console.log("Do Something"); } + // }); +}; diff --git a/packages/strapi-plugin-users-permissions/admin/src/lifecycles/didGetSecuredData.js b/packages/strapi-plugin-users-permissions/admin/src/lifecycles/didGetSecuredData.js new file mode 100644 index 0000000000..18f1809c43 --- /dev/null +++ b/packages/strapi-plugin-users-permissions/admin/src/lifecycles/didGetSecuredData.js @@ -0,0 +1,21 @@ +const pluginId = require('../pluginId'); + +module.exports = function didGetSecuredData() { + const { + props: { updatePlugin }, + } = this; + const leftMenuSections = [ + { + links: [ + { + label: 'Users', + destination: 'user', + plugin: 'content-manager', + }, + ], + name: 'Content Types', + }, + ]; + + updatePlugin(pluginId, 'leftMenuSections', leftMenuSections); +}; diff --git a/packages/strapi-plugin-users-permissions/admin/src/lifecycles/willSecure.js b/packages/strapi-plugin-users-permissions/admin/src/lifecycles/willSecure.js new file mode 100644 index 0000000000..6f3ab9a0e6 --- /dev/null +++ b/packages/strapi-plugin-users-permissions/admin/src/lifecycles/willSecure.js @@ -0,0 +1,71 @@ +const { includes } = require('lodash'); +const auth = require('utils/auth').default; + +module.exports = function willSecure() { + const { + props: { + hideLeftMenu, + hideLogout, + history, + location: { pathname }, + resetLocaleDefaultClassName, + setAppSecured, + setLocaleCustomClassName, + showLeftMenu, + showLogout, + store, + unsetAppSecured, + }, + } = this; + + const cb = () => + this.setState({ + shouldSecureAfterAllPluginsAreMounted: false, + }); + + const initializerReducer = store.getState().getIn(['users-permissions_initializer']); + + const nonProtectedURLs = ['/plugins/users-permissions/auth']; + const redirectEndPoint = initializerReducer.get('hasAdminUser') + ? '/plugins/users-permissions/auth/login' + : '/plugins/users-permissions/auth/register'; + + if (auth.getToken()) { + resetLocaleDefaultClassName(); // NOTE: Temporary should be removed when we switch to administrators + setAppSecured(); + showLeftMenu(); + showLogout(); + + return cb(); + } + + if (!includes(nonProtectedURLs, pathname)) { + hideLeftMenu(); + hideLogout(); + setLocaleCustomClassName('localeDropdownMenuNotLogged'); // NOTE: Temporary should be removed when we switch to administrators + unsetAppSecured(); + history.push(redirectEndPoint); + + return cb(); + } + + if ( + pathname === '/plugins/users-permissions/auth/login' || + pathname === '/plugins/users-permissions/auth/register' + ) { + hideLeftMenu(); + hideLogout(); + setLocaleCustomClassName('localeDropdownMenuNotLogged'); // NOTE: Temporary should be removed when we switch to administrators + unsetAppSecured(); + history.push(redirectEndPoint); + + return cb(); + } + + resetLocaleDefaultClassName(); // NOTE: Temporary should be removed when we switch to administrators + showLeftMenu(); + showLogout(); + setAppSecured(); + + return cb(); +};