Allows global and namespaced policies

This commit is contained in:
Aurélien Georget 2016-10-25 11:01:28 +02:00
parent 7b09d9f539
commit 5a9ca376db
4 changed files with 29 additions and 17 deletions

View File

@ -210,7 +210,7 @@ module.exports = strapi => {
// Retrieve the API's name where the controller is located
// to access to the right validators
const api = finder(strapi.api, controller);
const validator = _.get(strapi.api, api + '.config.validators.' + value.config.validate);
const validator = _.get(strapi.api, api + '.validators.' + value.config.validate);
_.merge(validate, _.mapValues(validator, value => {
return builder.build(value);

View File

@ -79,15 +79,24 @@ module.exports = strapi => {
}, cb);
},
// Load API policies from `./api/*/policies/*.js`.
// Load API policies from `./api/*/config/policies/*.js`.
'policies/*': cb => {
dictionary.aggregate({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.policies),
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.policies),
filter: /(.+)\.(js)$/,
depth: 1
}, cb);
},
// Load API validators from `./api/*/config/validators/*.js`.
'validators/*': cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.validators),
filter: /(.+)\.(json|js)$/,
depth: 1
}, cb);
},
// Load API config from `./api/*/config/*.js|json` and `./api/*/config/environments/**/*.js|json`.
'config/**': cb => {
async.parallel({
@ -99,13 +108,6 @@ module.exports = strapi => {
depth: 2
}, cb);
},
validators: cb => {
dictionary.aggregate({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, 'validators'),
filter: /(.+)\.(js|json)$/,
depth: 2
}, cb);
},
specific: cb => {
dictionary.aggregate({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, 'environments', strapi.config.environment),
@ -118,7 +120,7 @@ module.exports = strapi => {
return cb(err);
}
return cb(null, _.merge(config.common, config.specific, {validators: config.validators}));
return cb(null, _.merge(config.common, config.specific));
});
}
},
@ -137,6 +139,7 @@ module.exports = strapi => {
models: api['models/*'],
services: api['services/*'],
policies: api['policies/*'],
validators: api['validators/*'],
config: api['config/**']
};
@ -158,9 +161,6 @@ module.exports = strapi => {
// Merge API models with the main ones.
strapi.models = _.merge({}, strapi.models, _.get(strapi.api, api.name + '.models'));
// Merge API policies with the main ones.
strapi.policies = _.merge({}, strapi.policies, _.get(strapi.api, api.name + '.policies'));
// Merge API routes with the main ones.
strapi.config.routes = _.union([], strapi.config.routes, _.get(strapi.api, api.name + '.config.routes'));
});

View File

@ -78,6 +78,15 @@ module.exports = strapi => {
}, cb);
},
// Load global policies from `./config/policies/*.js`..
'config/policies/*': cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.config, 'policies'),
filter: /(.+)\.(js)$/,
depth: 1
}, cb);
},
// Load APIs from `./api/**/*.js|json`.
'api/**': cb => {
dictionary.optional({
@ -126,6 +135,9 @@ module.exports = strapi => {
// Merge default config and user loaded config together inside `strapi.config`.
strapi.config = _.merge(strapi.config, mergedConfig, packageJSON);
// Exposer global policies
strapi.policies = _.merge({}, config['config/policies/*']);
// Expose user APIs.
strapi.api = config['api/**'];
@ -150,7 +162,6 @@ module.exports = strapi => {
// Initialize empty API objects.
strapi.controllers = {};
strapi.models = {};
strapi.policies = {};
cb();
});

View File

@ -63,7 +63,8 @@ module.exports = class Configuration {
services: 'services',
policies: 'policies',
models: 'models',
plugins: 'plugins'
plugins: 'plugins',
validators: 'validators'
},
// Start off needed empty objects and strings.