Skip to content

Commit

Permalink
feat: Add function hasPolicies (#85)
Browse files Browse the repository at this point in the history
* feat: Add function hasPolicies

Signed-off-by: Edmond <[email protected]>

* feat: Add  hasPolicies unit tests

Signed-off-by: Edmond <[email protected]>
  • Loading branch information
Edmond-J-A authored Jul 14, 2021
1 parent 3f7e220 commit 2b48145
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/model/Policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/model/model_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2b48145

Please sign in to comment.