mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 06:07:41 +00:00
clean permissions in db at startup
Signed-off-by: Pierre Noël <petersg83@gmail.com>
This commit is contained in:
parent
e078c0b022
commit
0330aba229
@ -1,6 +1,30 @@
|
|||||||
const adminActions = require('../admin-actions');
|
const adminActions = require('../admin-actions');
|
||||||
|
|
||||||
module.exports = async () => {
|
const registerPermissionActions = () => {
|
||||||
const actionProvider = strapi.admin.services.permission.provider;
|
const actionProvider = strapi.admin.services.permission.provider;
|
||||||
actionProvider.register(adminActions.actions);
|
actionProvider.register(adminActions.actions);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const cleanPermissionInDatabase = async () => {
|
||||||
|
const actionProvider = strapi.admin.services.permission.provider;
|
||||||
|
const dbPermissions = await strapi.admin.services.permission.find();
|
||||||
|
const allActionsMap = actionProvider.getAllByMap();
|
||||||
|
const permissionsToRemoveIds = [];
|
||||||
|
|
||||||
|
dbPermissions.forEach(perm => {
|
||||||
|
if (
|
||||||
|
!allActionsMap.has(perm.action) ||
|
||||||
|
(allActionsMap.get(perm.action).section === 'contentTypes' &&
|
||||||
|
!allActionsMap.get(perm.action).subjects.includes(perm.subject))
|
||||||
|
) {
|
||||||
|
permissionsToRemoveIds.push(perm.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await strapi.admin.services.permission.deleteByIds(permissionsToRemoveIds);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = async () => {
|
||||||
|
registerPermissionActions();
|
||||||
|
await cleanPermissionInDatabase();
|
||||||
|
};
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
const { yup } = require('strapi-utils');
|
const { yup } = require('strapi-utils');
|
||||||
const { validateRegisterProviderAction } = require('../validation/action-provider');
|
const { validateRegisterProviderAction } = require('../validation/action-provider');
|
||||||
const { getActionId, createAction } = require('../domain/action');
|
const { getActionId, createAction } = require('../domain/action');
|
||||||
@ -8,12 +9,19 @@ const actionProviderFactory = () => {
|
|||||||
return {
|
return {
|
||||||
get(uid, pluginName) {
|
get(uid, pluginName) {
|
||||||
const actionId = getActionId({ pluginName, uid });
|
const actionId = getActionId({ pluginName, uid });
|
||||||
return actions.find(p => p.actionId === actionId);
|
const action = actions.find(p => p.actionId === actionId);
|
||||||
|
return _.cloneDeep(action);
|
||||||
},
|
},
|
||||||
getAll() {
|
getAll() {
|
||||||
return Array.from(actions.values());
|
return _.cloneDeep(Array.from(actions.values()));
|
||||||
|
},
|
||||||
|
getAllByMap() {
|
||||||
|
return _.cloneDeep(actions);
|
||||||
},
|
},
|
||||||
register(newActions) {
|
register(newActions) {
|
||||||
|
if (strapi.isLoaded) {
|
||||||
|
throw new Error(`You can't register new actions outside of the bootstrap function.`);
|
||||||
|
}
|
||||||
validateRegisterProviderAction(newActions);
|
validateRegisterProviderAction(newActions);
|
||||||
newActions.forEach(newAction => {
|
newActions.forEach(newAction => {
|
||||||
const actionId = getActionId(newAction);
|
const actionId = getActionId(newAction);
|
||||||
|
|||||||
@ -5,13 +5,22 @@ const actionProvider = require('./action-provider');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete permissions of roles in database
|
* Delete permissions of roles in database
|
||||||
* @param params ids of roles
|
* @param rolesIds ids of roles
|
||||||
* @returns {Promise<array>}
|
* @returns {Promise<array>}
|
||||||
*/
|
*/
|
||||||
const deleteByRolesIds = rolesIds => {
|
const deleteByRolesIds = rolesIds => {
|
||||||
return strapi.query('permission', 'admin').delete({ role_in: rolesIds });
|
return strapi.query('permission', 'admin').delete({ role_in: rolesIds });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete permissions
|
||||||
|
* @param ids ids of permissions
|
||||||
|
* @returns {Promise<array>}
|
||||||
|
*/
|
||||||
|
const deleteByIds = ids => {
|
||||||
|
return strapi.query('permission', 'admin').delete({ id_in: ids });
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find assigned permissions in the database
|
* Find assigned permissions in the database
|
||||||
* @param params query params to find the permissions
|
* @param params query params to find the permissions
|
||||||
@ -59,6 +68,7 @@ const assign = async (roleID, permissions = []) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
find,
|
find,
|
||||||
deleteByRolesIds,
|
deleteByRolesIds,
|
||||||
|
deleteByIds,
|
||||||
assign,
|
assign,
|
||||||
provider: actionProvider,
|
provider: actionProvider,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -372,13 +372,7 @@ class Strapi {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const adminBootstrap = _.get(this.admin.config, 'functions.bootstrap');
|
// plugins bootstrap
|
||||||
await execBootstrap(adminBootstrap).catch(err => {
|
|
||||||
strapi.log.error(`Bootstrap function in admin failed`);
|
|
||||||
strapi.log.error(err);
|
|
||||||
strapi.stop();
|
|
||||||
});
|
|
||||||
|
|
||||||
const pluginBoostraps = Object.keys(this.plugins).map(plugin => {
|
const pluginBoostraps = Object.keys(this.plugins).map(plugin => {
|
||||||
return execBootstrap(_.get(this.plugins[plugin], 'config.functions.bootstrap')).catch(err => {
|
return execBootstrap(_.get(this.plugins[plugin], 'config.functions.bootstrap')).catch(err => {
|
||||||
strapi.log.error(`Bootstrap function in plugin "${plugin}" failed`);
|
strapi.log.error(`Bootstrap function in plugin "${plugin}" failed`);
|
||||||
@ -386,10 +380,18 @@ class Strapi {
|
|||||||
strapi.stop();
|
strapi.stop();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all(pluginBoostraps);
|
await Promise.all(pluginBoostraps);
|
||||||
|
|
||||||
return execBootstrap(_.get(this.config, ['functions', 'bootstrap']));
|
// user bootstrap
|
||||||
|
await execBootstrap(_.get(this.config, ['functions', 'bootstrap']));
|
||||||
|
|
||||||
|
// admin bootstrap : should always run after the others
|
||||||
|
const adminBootstrap = _.get(this.admin.config, 'functions.bootstrap');
|
||||||
|
return execBootstrap(adminBootstrap).catch(err => {
|
||||||
|
strapi.log.error(`Bootstrap function in admin failed`);
|
||||||
|
strapi.log.error(err);
|
||||||
|
strapi.stop();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async freeze() {
|
async freeze() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user