Skip to content

Commit

Permalink
Merge pull request #111 from Edmond-J-A/master
Browse files Browse the repository at this point in the history
feat: Add function addPoliciesWithAffected
  • Loading branch information
hsluoyz authored Aug 5, 2021
2 parents 7158a79 + b746bc3 commit 363976c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/model/Policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,25 @@ end
* @return succeeds or not.
]]
function Policy:addPolicies(sec, ptype, rules)
local size = #self.model[sec][ptype].policy
return self:addPoliciesWithAffected(sec, ptype, rules)~=0
end

--[[
* addPoliciesWithAffected adds policy rules to the model.
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policy rules.
* @return effected.
]]
function Policy:addPoliciesWithAffected(sec, ptype, rules)
local effected={}
for _, rule in pairs(rules) do
if not self:hasPolicy(sec, ptype, rule) then
table.insert(self.model[sec][ptype].policy, rule)
self.model[sec][ptype].policyMap[table.concat(rule,",")]=#self.model[sec][ptype].policy
end
end
if self:addPolicy(sec, ptype, rule) then
table.insert(effected, rule)

if size < #self.model[sec][ptype].policy then
return true
else
return false
end
end
return effected
end

--[[
Expand Down Expand Up @@ -332,9 +338,8 @@ end
function Policy:removePoliciesWithEffected(sec, ptype, rules)
local effected={}
for _,rule in pairs(rules) do
if self:hasPolicy(sec, ptype, rule) then
if self:removePolicy(sec,ptype,rule) then
table.insert(effected,rule)
self:removePolicy(sec,ptype,rule)
end
end
return effected
Expand Down
30 changes: 29 additions & 1 deletion tests/model/model_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ describe("model tests", function()
assert.is.False(m:hasPolicies("p", "p", rulesnotmatched))
end)


it("test addPolicy", function ()
local m = Model:new()
m:loadModel(basic_path)
Expand All @@ -72,6 +71,25 @@ describe("model tests", function()
assert.is.True(m:hasPolicy("p", "p", rule))
end)

it("test addPoliciesWithAffected", 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))

assert.are.same(rules,m:addPoliciesWithAffected("p", "p", rules))
assert.is.True(m:hasPolicies("p", "p", rules))

local rules1 = {{'Alice', 'domain1', 'data1', 'read'},{'Bob', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}}
assert.is.True(m:hasPolicies("p", "p", rules1))

assert.are.same({{'Alice', 'domain1', 'data1', 'read'},{'Bob', 'domain2', 'data2', 'read'}},m:addPoliciesWithAffected("p", "p", rules1))
assert.is.True(m:hasPolicy("p", "p", {'Alice', 'domain1', 'data1', 'read'}))
assert.is.True(m:hasPolicy("p", "p", {'Bob', 'domain2', 'data2', 'read'}))

end)

it("test removePolicy", function ()
local m = Model:new()
m:loadModel(basic_path)
Expand Down Expand Up @@ -100,6 +118,16 @@ describe("model tests", function()
assert.are.same(rules,m:removePoliciesWithEffected("p", "p", rules))
assert.is.False(m:hasPolicies("p", "p", rules))
assert.is.False(m:removePolicy("p", "p", rules[1]))

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

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

assert.are.same({{'admin', 'domain2', 'data2', 'read'},{'admin', 'domain1', 'data1', 'write'}},m:removePoliciesWithEffected("p", "p", removeList))
assert.is.False(m:hasPolicy("p", "p", removeList[2]))
assert.is.False(m:removePolicy("p", "p", removeList[3]))
end)

it("test addRolePolicy", function ()
Expand Down

0 comments on commit 363976c

Please sign in to comment.