github.com/outbrain/consul@v1.4.5/ui-v2/app/utils/acls-status.js (about) 1 // This is used by all acl routes to check whether 2 // acls are enabled on the server, and whether the user 3 // has a valid token 4 // Right now this is very acl specific, but is likely to be 5 // made a bit more less specific 6 7 export default function(isValidServerError, P = Promise) { 8 return function(obj) { 9 const propName = Object.keys(obj)[0]; 10 const p = obj[propName]; 11 let authorize; 12 let enable; 13 return { 14 isAuthorized: new P(function(resolve) { 15 authorize = function(bool) { 16 resolve(bool); 17 }; 18 }), 19 isEnabled: new P(function(resolve) { 20 enable = function(bool) { 21 resolve(bool); 22 }; 23 }), 24 [propName]: p 25 .catch(function(e) { 26 switch (e.errors[0].status) { 27 case '500': 28 if (isValidServerError(e)) { 29 enable(true); 30 authorize(false); 31 } else { 32 return P.reject(e); 33 } 34 break; 35 case '403': 36 enable(true); 37 authorize(false); 38 break; 39 case '401': 40 enable(false); 41 authorize(false); 42 break; 43 default: 44 enable(false); 45 authorize(false); 46 throw e; 47 } 48 return []; 49 }) 50 .then(function(res) { 51 enable(true); 52 authorize(true); 53 return res; 54 }), 55 }; 56 }; 57 }