61 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict';
const { getOr } = require('lodash/fp');
const { policy: policyUtils } = require('@strapi/utils');
2021-10-27 18:54:58 +02:00
const { ForbiddenError } = require('@strapi/utils').errors;
const createPoliciesMiddleware = (resolverConfig, { strapi }) => {
return async (resolve, ...rest) => {
const resolverPolicies = getOr([], 'policies', resolverConfig);
// Transform every policy into a unique format
const policies = resolverPolicies.map(policy => policyUtils.get(policy));
// Create a graphql policy context
const context = createGraphQLPolicyContext(...rest);
// Run policies & throw an error if one of them fails
for (const policy of policies) {
const result = await policy(context, { strapi });
if (![true, undefined].includes(result)) {
2021-10-27 18:54:58 +02:00
throw new ForbiddenError();
}
}
return resolve(...rest);
};
};
const createGraphQLPolicyContext = (parent, args, context, info) => {
return policyUtils.createPolicyContext('graphql', {
get parent() {
return parent;
},
get args() {
return args;
},
get context() {
return context;
},
get info() {
return info;
},
get state() {
return this.context.state;
},
get http() {
return this.context.koaContext;
},
});
};
module.exports = {
createPoliciesMiddleware,
};