Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add function hasPolicies #85

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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