mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 18:33:55 +00:00
Send 401 if action is not enabled
This commit is contained in:
parent
9a801e4ac1
commit
5e2748f81f
@ -1,5 +1,47 @@
|
||||
const pathToRegexp = require('path-to-regexp');
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = async (ctx, next) => {
|
||||
const config = strapi.plugins['users-permissions'].config;
|
||||
let match = false;
|
||||
|
||||
await next();
|
||||
const matchRoute = async (route, plugin) => {
|
||||
if (match) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let value = _.clone(route.path);
|
||||
|
||||
if (route.config.prefix !== undefined) {
|
||||
value = route.config.prefix + value;
|
||||
} else if (plugin) {
|
||||
value = `${plugin}/${value}`;
|
||||
}
|
||||
|
||||
const re = pathToRegexp(value);
|
||||
match = re.test(ctx.url) && ctx.method === route.method;
|
||||
|
||||
if (match) {
|
||||
const permissions = _.get(config, ['0', 'permissions', plugin || 'application']);
|
||||
const action = _.get(permissions, `controllers.${route.handler.toLowerCase()}`);
|
||||
|
||||
if (action.enabled) {
|
||||
await next();
|
||||
} else {
|
||||
ctx.unauthorized('Nop!');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_.forEach(strapi.config.routes, value => {
|
||||
matchRoute(value);
|
||||
});
|
||||
|
||||
if (strapi.plugins) {
|
||||
_.forEach(strapi.plugins, (plugin, name) => {
|
||||
_.forEach(plugin.config.routes, value => {
|
||||
matchRoute(value, name);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
const pathToRegexp = require('path-to-regexp');
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = strapi => {
|
||||
|
||||
@ -63,4 +63,4 @@
|
||||
"rimraf": "^2.6.2",
|
||||
"webpack": "^3.8.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user