-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
load js policy if policy config is set
- Loading branch information
Showing
2 changed files
with
27 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
if (!window.policyConfig) | ||
throw "Missing data. Please provide window.policyConfig" | ||
if (!window.policyConfig.rules) | ||
throw "Missing rules. Please provide window.policyConfig.rules" | ||
if (!window.policyConfig.locals) | ||
throw "Missing locals. Please provide window.policyConfig.locals" | ||
|
||
// evaluate rules | ||
const rules = {} | ||
for (let name in window.policyConfig.rules) { | ||
const rule = window.policyConfig.rules[name] | ||
try { | ||
rules[name] = eval(rule) | ||
} catch (e) { | ||
console.info("Policy Engine Error: ", e) | ||
if (window.policyConfig?.rules && window.policyConfig?.locals) { | ||
// evaluate rules | ||
const rules = {} | ||
for (let name in window.policyConfig.rules) { | ||
const rule = window.policyConfig.rules[name] | ||
try { | ||
rules[name] = eval(rule) | ||
} catch (e) { | ||
console.info("Policy Engine Error: ", e) | ||
} | ||
} | ||
} | ||
|
||
window.policy = { | ||
isAllowed: function (name, params = {}) { | ||
const rule = rules[name] | ||
if (!rule) throw "Policy Engine Error: rule #{name} not found." | ||
return rule(rules, window.policyConfig.locals, params) | ||
}, | ||
window.policy = { | ||
isAllowed: function (name, params = {}) { | ||
const rule = rules[name] | ||
if (!rule) throw "Policy Engine Error: rule #{name} not found." | ||
return rule(rules, window.policyConfig.locals, params) | ||
}, | ||
} | ||
} else { | ||
console.info( | ||
"Policy engine is not loaded because window.policyConfig does not contain required data" | ||
) | ||
} |