Skip to content

Commit 363976c

Browse files
authored
Merge pull request #111 from Edmond-J-A/master
feat: Add function addPoliciesWithAffected
2 parents 7158a79 + b746bc3 commit 363976c

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/model/Policy.lua

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,25 @@ end
239239
* @return succeeds or not.
240240
]]
241241
function Policy:addPolicies(sec, ptype, rules)
242-
local size = #self.model[sec][ptype].policy
242+
return self:addPoliciesWithAffected(sec, ptype, rules)~=0
243+
end
244+
245+
--[[
246+
* addPoliciesWithAffected adds policy rules to the model.
247+
* @param sec the section, "p" or "g".
248+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
249+
* @param rules the policy rules.
250+
* @return effected.
251+
]]
252+
function Policy:addPoliciesWithAffected(sec, ptype, rules)
253+
local effected={}
243254
for _, rule in pairs(rules) do
244-
if not self:hasPolicy(sec, ptype, rule) then
245-
table.insert(self.model[sec][ptype].policy, rule)
246-
self.model[sec][ptype].policyMap[table.concat(rule,",")]=#self.model[sec][ptype].policy
247-
end
248-
end
255+
if self:addPolicy(sec, ptype, rule) then
256+
table.insert(effected, rule)
249257

250-
if size < #self.model[sec][ptype].policy then
251-
return true
252-
else
253-
return false
258+
end
254259
end
260+
return effected
255261
end
256262

257263
--[[
@@ -332,9 +338,8 @@ end
332338
function Policy:removePoliciesWithEffected(sec, ptype, rules)
333339
local effected={}
334340
for _,rule in pairs(rules) do
335-
if self:hasPolicy(sec, ptype, rule) then
341+
if self:removePolicy(sec,ptype,rule) then
336342
table.insert(effected,rule)
337-
self:removePolicy(sec,ptype,rule)
338343
end
339344
end
340345
return effected

tests/model/model_spec.lua

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ describe("model tests", function()
5959
assert.is.False(m:hasPolicies("p", "p", rulesnotmatched))
6060
end)
6161

62-
6362
it("test addPolicy", function ()
6463
local m = Model:new()
6564
m:loadModel(basic_path)
@@ -72,6 +71,25 @@ describe("model tests", function()
7271
assert.is.True(m:hasPolicy("p", "p", rule))
7372
end)
7473

74+
it("test addPoliciesWithAffected", function ()
75+
local m = Model:new()
76+
m:loadModel(basic_path)
77+
78+
local rules = {{'admin', 'domain1', 'data1', 'read'},{'admin', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}}
79+
assert.is.False(m:hasPolicies("p", "p", rules))
80+
81+
assert.are.same(rules,m:addPoliciesWithAffected("p", "p", rules))
82+
assert.is.True(m:hasPolicies("p", "p", rules))
83+
84+
local rules1 = {{'Alice', 'domain1', 'data1', 'read'},{'Bob', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}}
85+
assert.is.True(m:hasPolicies("p", "p", rules1))
86+
87+
assert.are.same({{'Alice', 'domain1', 'data1', 'read'},{'Bob', 'domain2', 'data2', 'read'}},m:addPoliciesWithAffected("p", "p", rules1))
88+
assert.is.True(m:hasPolicy("p", "p", {'Alice', 'domain1', 'data1', 'read'}))
89+
assert.is.True(m:hasPolicy("p", "p", {'Bob', 'domain2', 'data2', 'read'}))
90+
91+
end)
92+
7593
it("test removePolicy", function ()
7694
local m = Model:new()
7795
m:loadModel(basic_path)
@@ -100,6 +118,16 @@ describe("model tests", function()
100118
assert.are.same(rules,m:removePoliciesWithEffected("p", "p", rules))
101119
assert.is.False(m:hasPolicies("p", "p", rules))
102120
assert.is.False(m:removePolicy("p", "p", rules[1]))
121+
122+
m:addPolicies("p", "p", rules)
123+
assert.is.True(m:hasPolicies("p", "p", rules))
124+
125+
local removeList={{'Alice', 'domain1', 'data1', 'read'},{'admin', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}}
126+
assert.is.False(m:hasPolicy("p", "p", {'Alice', 'domain1', 'data1', 'read'}))
127+
128+
assert.are.same({{'admin', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}},m:removePoliciesWithEffected("p", "p", removeList))
129+
assert.is.False(m:hasPolicy("p", "p", removeList[2]))
130+
assert.is.False(m:removePolicy("p", "p", removeList[3]))
103131
end)
104132

105133
it("test addRolePolicy", function ()

0 commit comments

Comments
 (0)