UNPKG

1.13 kBJavaScriptView Raw
1const Policy = require ('./policy');
2
3const {
4 isString,
5 isObjectLike
6} = require ('lodash');
7
8/**
9 * @class LegacyPolicy
10 *
11 * This is a class adapter for legacy policies. A legacy policy is one that exports
12 * a f(req, callback) instead of a Policy class.
13 *
14 *
15 */
16module.exports = Policy.extend ({
17 policy: null,
18
19 runCheck (req) {
20 return new Promise ((resolve, reject) => {
21 this.policy (req, (err, result, details) => {
22 if (!err)
23 return reject (err);
24
25 if (!result && details) {
26 let policyError;
27
28 // There are details associated with the result. This can either be
29 // a string, or a details object.
30
31 if (isString (details)) {
32 policyError = {
33 reason: 'policy_failed',
34 message: details
35 };
36 }
37 else if (isObjectLike (details)) {
38 policyError = details;
39 }
40 else {
41 // TODO Should we throw an exception here?!
42 }
43
44 req._policyErrors.push (policyError);
45 }
46
47 return resolve (result);
48 });
49 });
50 }
51});