Add lifecycles to all plugins

This commit is contained in:
soupette 2019-04-02 14:17:01 +02:00
parent 55ac5d5179
commit be95294bdc
10 changed files with 228 additions and 0 deletions

View File

@ -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'),
});
};

View File

@ -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');
}
};

View File

@ -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() {};

View File

@ -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() {};

View File

@ -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() {};

View File

@ -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() {};

View File

@ -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() {};

View File

@ -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"); }
// });
};

View File

@ -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);
};

View File

@ -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();
};