mirror of
https://github.com/strapi/strapi.git
synced 2025-07-23 09:00:19 +00:00

Hi, I'm been using Strapi for a project I'm doing and you guys have done a great work! I love the idea. This is a small Pull Request, however, I find it very useful if you would like to have your global policies organized by folders. Just in case you have more than 10 of them. If you try at the moment to create a policy inside a folder, it will throw ``` Cannot read property 'handler' of undefined ``` This will fix it and it will allow you to have folders on your `config/policies` folder.
115 lines
3.1 KiB
JavaScript
115 lines
3.1 KiB
JavaScript
|
|
// Public dependencies.
|
|
const _ = require('lodash');
|
|
/* eslint-disable prefer-template */
|
|
module.exports = {
|
|
get: function (policy, plugin, policies = [], endpoint, currentApiName) {
|
|
// Define global policy prefix.
|
|
const globalPolicyPrefix = 'global.';
|
|
const pluginPolicyPrefix = 'plugins.';
|
|
const policySplited = policy.split('.');
|
|
|
|
// Looking for global policy or namespaced.
|
|
if (
|
|
_.startsWith(policy, globalPolicyPrefix, 0) &&
|
|
!_.isUndefined(
|
|
_.get(
|
|
strapi.config.policies,
|
|
policy.replace(globalPolicyPrefix, '').toLowerCase()
|
|
)
|
|
)
|
|
) {
|
|
// Global policy.
|
|
return policies.push(
|
|
this.parsePolicy(
|
|
_.get(
|
|
strapi.config.policies,
|
|
policy.replace(globalPolicyPrefix, '').toLowerCase()
|
|
)
|
|
)
|
|
);
|
|
} else if (
|
|
_.startsWith(policy, pluginPolicyPrefix, 0) &&
|
|
strapi.plugins[policySplited[1]] &&
|
|
!_.isUndefined(
|
|
_.get(
|
|
strapi.plugins,
|
|
policySplited[1] +
|
|
'.config.policies.' +
|
|
policySplited[2].toLowerCase()
|
|
)
|
|
)
|
|
) {
|
|
// Plugin's policies can be used from app APIs with a specific syntax (`plugins.pluginName.policyName`).
|
|
return policies.push(
|
|
this.parsePolicy(_.get(
|
|
strapi.plugins,
|
|
policySplited[1] +
|
|
'.config.policies.' +
|
|
policySplited[2].toLowerCase()
|
|
))
|
|
);
|
|
} else if (
|
|
!_.startsWith(policy, globalPolicyPrefix, 0) &&
|
|
plugin &&
|
|
!_.isUndefined(
|
|
_.get(
|
|
strapi.plugins,
|
|
plugin + '.config.policies.' + policy.toLowerCase()
|
|
)
|
|
)
|
|
) {
|
|
// Plugin policy used in the plugin itself.
|
|
return policies.push(
|
|
this.parsePolicy(_.get(
|
|
strapi.plugins,
|
|
plugin + '.config.policies.' + policy.toLowerCase()
|
|
))
|
|
);
|
|
} else if (
|
|
!_.startsWith(policy, globalPolicyPrefix, 0) &&
|
|
!_.isUndefined(
|
|
_.get(
|
|
strapi.api,
|
|
currentApiName + '.config.policies.' + policy.toLowerCase()
|
|
)
|
|
)
|
|
) {
|
|
// API policy used in the API itself.
|
|
return policies.push(
|
|
this.parsePolicy(_.get(
|
|
strapi.api,
|
|
currentApiName + '.config.policies.' + policy.toLowerCase()
|
|
))
|
|
);
|
|
}
|
|
|
|
strapi.log.error(`Ignored attempt to bind to ${endpoint} with unknown policy "${policy}"`);
|
|
},
|
|
|
|
parsePolicy: (policy) => {
|
|
if (_.isFunction(policy)) {
|
|
return policy;
|
|
}
|
|
|
|
return policy.handler;
|
|
},
|
|
|
|
// Middleware used for every routes.
|
|
// Expose the endpoint in `this`.
|
|
globalPolicy: (endpoint, value, route = {}, plugin) => {
|
|
return async (ctx, next) => {
|
|
ctx.request.route = {
|
|
endpoint: _.trim(endpoint),
|
|
controller: value.handler.split('.')[0].toLowerCase(),
|
|
action: value.handler.split('.')[1].toLowerCase(),
|
|
splittedEndpoint: _.trim(route.endpoint),
|
|
verb: route.verb && _.trim(route.verb.toLowerCase()),
|
|
plugin
|
|
};
|
|
|
|
await next();
|
|
};
|
|
}
|
|
};
|