Skip to content

Commit

Permalink
feat: Add function removePoliciesWithEffected (#95)
Browse files Browse the repository at this point in the history
Signed-off-by: Edmond <edomondja@gmail.com>
  • Loading branch information
Edmond-J-A committed Jul 21, 2021
1 parent 9951b5b commit 4cf7737
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/model/Policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -286,35 +286,30 @@ end
* @return succeeds or not.
]]
function Policy:removePolicies(sec, ptype, rules)
local size = #self.model[sec][ptype].policy
for _, rule in pairs(rules) do
for k, v in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(rule, v) then
table.remove(self.model[sec][ptype].policy, k)
break
end
end
end

if size > #self.model[sec][ptype].policy then
return true
else
return false
end
return #self:removePoliciesWithEffected(sec, ptype, rules)~=0
end

--[[
* removeFilteredPolicyReturnsEffects removes policy rules based on field filters from the model.
* removePoliciesWithEffected removes policy rules from the model, and returns effected rules.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param ... fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds(effects.size() &gt; 0) or not.
* @param rules the policy rules.
*
* @return effected.
]]
function Policy:removeFilteredPolicyReturnsEffects(sec, ptype, fieldIndex, ...)
return {}
function Policy:removePoliciesWithEffected(sec, ptype, rules)
local effected={}
for _,rule in pairs(rules) do
for k, v in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(rule, v) then
table.insert(effected,rule)
table.remove(self.model[sec][ptype].policy, k)
break
end
end
end
return effected
end

--[[
Expand Down
15 changes: 15 additions & 0 deletions tests/model/model_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ describe("model tests", function()
assert.is.False(m:removePolicy("p", "p", rule))
end)

it("test removePoliciesWithEffected", function ()
local m = Model:new()
m:loadModel(basic_path)

local rules = {{'admin', 'domain1', 'data1', 'read'},{'admin', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}}
assert.is.False(m:hasPolicies("p", "p", rules))

m:addPolicies("p", "p", rules)
assert.is.True(m:hasPolicies("p", "p", rules))

assert.are.same(m:removePoliciesWithEffected("p", "p", rules),rules)
assert.is.False(m:hasPolicies("p", "p", rules))
assert.is.False(m:removePolicy("p", "p", rules))
end)

it("test addRolePolicy", function ()
local m = Model:new()
m:loadModel(rbac_path)
Expand Down

0 comments on commit 4cf7737

Please sign in to comment.