1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/middleware/permission-checker.js

30 lines
830 B
JavaScript
Raw Normal View History

'use strict';
const MissingPermission = require('../missing-permission');
const { ADMIN } = require('../permissions');
module.exports = function(config, permission) {
if (!permission || !config.extendedPermissions) {
return (req, res, next) => next();
}
return (req, res, next) => {
if (
req.user &&
req.user.permissions &&
(req.user.permissions.indexOf(ADMIN) !== -1 ||
req.user.permissions.indexOf(permission) !== -1)
) {
return next();
}
return res
.status(403)
.json(
new MissingPermission({
permission,
message: `You require ${permission} to perform this action`,
})
)
.end();
};
};