Skip to content

Commit 4cf7737

Browse files
authored
feat: Add function removePoliciesWithEffected (#95)
Signed-off-by: Edmond <edomondja@gmail.com>
1 parent 9951b5b commit 4cf7737

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/model/Policy.lua

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,35 +286,30 @@ end
286286
* @return succeeds or not.
287287
]]
288288
function Policy:removePolicies(sec, ptype, rules)
289-
local size = #self.model[sec][ptype].policy
290-
for _, rule in pairs(rules) do
291-
for k, v in pairs(self.model[sec][ptype].policy) do
292-
if Util.arrayEquals(rule, v) then
293-
table.remove(self.model[sec][ptype].policy, k)
294-
break
295-
end
296-
end
297-
end
298-
299-
if size > #self.model[sec][ptype].policy then
300-
return true
301-
else
302-
return false
303-
end
289+
return #self:removePoliciesWithEffected(sec, ptype, rules)~=0
304290
end
305291

306292
--[[
307-
* removeFilteredPolicyReturnsEffects removes policy rules based on field filters from the model.
293+
* removePoliciesWithEffected removes policy rules from the model, and returns effected rules.
308294
*
309295
* @param sec the section, "p" or "g".
310296
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
311-
* @param fieldIndex the policy rule's start index to be matched.
312-
* @param ... fieldValues the field values to be matched, value ""
313-
* means not to match this field.
314-
* @return succeeds(effects.size() &gt; 0) or not.
297+
* @param rules the policy rules.
298+
*
299+
* @return effected.
315300
]]
316-
function Policy:removeFilteredPolicyReturnsEffects(sec, ptype, fieldIndex, ...)
317-
return {}
301+
function Policy:removePoliciesWithEffected(sec, ptype, rules)
302+
local effected={}
303+
for _,rule in pairs(rules) do
304+
for k, v in pairs(self.model[sec][ptype].policy) do
305+
if Util.arrayEquals(rule, v) then
306+
table.insert(effected,rule)
307+
table.remove(self.model[sec][ptype].policy, k)
308+
break
309+
end
310+
end
311+
end
312+
return effected
318313
end
319314

320315
--[[

tests/model/model_spec.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ describe("model tests", function()
8787
assert.is.False(m:removePolicy("p", "p", rule))
8888
end)
8989

90+
it("test removePoliciesWithEffected", function ()
91+
local m = Model:new()
92+
m:loadModel(basic_path)
93+
94+
local rules = {{'admin', 'domain1', 'data1', 'read'},{'admin', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}}
95+
assert.is.False(m:hasPolicies("p", "p", rules))
96+
97+
m:addPolicies("p", "p", rules)
98+
assert.is.True(m:hasPolicies("p", "p", rules))
99+
100+
assert.are.same(m:removePoliciesWithEffected("p", "p", rules),rules)
101+
assert.is.False(m:hasPolicies("p", "p", rules))
102+
assert.is.False(m:removePolicy("p", "p", rules))
103+
end)
104+
90105
it("test addRolePolicy", function ()
91106
local m = Model:new()
92107
m:loadModel(rbac_path)

0 commit comments

Comments
 (0)