From 2b48145c1c52e8dd5b3cddbab34f639c0353e7df Mon Sep 17 00:00:00 2001 From: Edmond-J-A <79526086+Edmond-J-A@users.noreply.github.com> Date: Wed, 14 Jul 2021 03:22:16 -0500 Subject: [PATCH] feat: Add function hasPolicies (#85) * feat: Add function hasPolicies Signed-off-by: Edmond * feat: Add hasPolicies unit tests Signed-off-by: Edmond --- src/model/Policy.lua | 24 +++++++++++++++++++++--- tests/model/model_spec.lua | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/model/Policy.lua b/src/model/Policy.lua index 7fc9310..6f0fe96 100644 --- a/src/model/Policy.lua +++ b/src/model/Policy.lua @@ -138,7 +138,7 @@ function Policy:getFilteredPolicy(sec, ptype, fieldIndex, ...) if not self.model[sec] then return res end if not self.model[sec][ptype] then return res end - + for _, rule in pairs(self.model[sec][ptype].policy) do local matched = true for i, v in ipairs(fieldValues) do @@ -172,6 +172,24 @@ function Policy:hasPolicy(sec, ptype, rule) return false end +--[[ + * hasPolicies determines whether a model has any of the specified policies. + * + * @param sec the section, "p" or "g". + * @param ptype the policy type, "p", "p2", .. or "g", "g2", .. + * @param rules the policies rules. + * @return whether one is found. +]] +function Policy:hasPolicies(sec, ptype, rules) + for i = 1, #rules do + if self:hasPolicy(sec, ptype, rules[i]) then + return true + end + end + return false +end + + --[[ * addPolicy adds a policy rule to the model. * @@ -205,7 +223,7 @@ function Policy:addPolicies(sec, ptype, rules) if size < #self.model[sec][ptype].policy then return true - else + else return false end end @@ -281,7 +299,7 @@ function Policy:removePolicies(sec, ptype, rules) if size > #self.model[sec][ptype].policy then return true - else + else return false end end diff --git a/tests/model/model_spec.lua b/tests/model/model_spec.lua index 55a9ebf..3a5e982 100644 --- a/tests/model/model_spec.lua +++ b/tests/model/model_spec.lua @@ -41,6 +41,25 @@ describe("model tests", function() assert.is.True(m:hasPolicy("p", "p", rule)) end) + it("test hasPolicies", function () + local m = Model:new() + m:loadModel(basic_path) + local rule = {'admin', 'domain1', 'data1', 'read'} + m:addPolicy("p", "p", rule) + local rule1={'admin', 'domain2', 'data2', 'read'} + m:addPolicy("p", "p", rule1) + local rule2={'admin', 'domain3', 'data3', 'read'} + m:addPolicy("p", "p", rule2) + local rule3={'admin', 'domain4', 'data4', 'read'} + local rulesallmatched={rule,rule1,rule2} + local rulesonematched={rule,rule3} + local rulesnotmatched={rule3} + assert.is.True(m:hasPolicies("p", "p", rulesallmatched)) + assert.is.True(m:hasPolicies("p", "p", rulesonematched)) + assert.is.False(m:hasPolicies("p", "p", rulesnotmatched)) + end) + + it("test addPolicy", function () local m = Model:new() m:loadModel(basic_path)