Skip to content

Commit

Permalink
feat: Add code relates with policyMap
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 23, 2021
1 parent 4cf7737 commit 2a67105
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/model/Assertion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Assertion:new()
o.tokens = {}
o.policy = {}
o.RM = {}

o.policyMap={}
setmetatable(o,self)
self.__index = self
return o
Expand Down
5 changes: 4 additions & 1 deletion src/model/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function Model:addDef(sec, key, value)
self.model[sec][key] = Assertion:new()
self.model[sec][key].key = key
self.model[sec][key].value = value

self.model[sec][key].policyMap={}
if sec == "r" or sec == "p" then
self.model[sec][key].tokens = Util.splitCommaDelimited(self.model[sec][key].value)
for k, v in pairs(self.model[sec][key].tokens) do
Expand Down Expand Up @@ -227,6 +227,9 @@ function Model:sortPoliciesByPriority()
table.sort(ast.policy, function (a, b)
return a[priorityIndex] < b[priorityIndex]
end)
for i,policy in pairs(ast.policy) do
ast.policyMap[table.concat(policy,",")]=i
end
end
end

Expand Down
82 changes: 58 additions & 24 deletions src/model/Policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ function Policy:printPolicy()
end
end

--[[
* printPolicyMap prints the policyMap to log.
]]
function Policy:printPolicyMap()
self.logger:info("policyMap: \n")
if self.model["p"] then
for k, ast in pairs(self.model["p"]) do
self.logger:info("%s: key,value", k )
for i,v in pairs(ast.policyMap) do
self.logger:info("{%s,%s}", i, v)
end
end
end

if self.model["g"] then
for k, ast in pairs(self.model["g"]) do
self.logger:info("%s: key,value", k )
for i,v in pairs(ast.policyMap) do
self.logger:info("{%s,%s}", i, v)
end
end
end
end

--[[
* savePolicyToText saves the policy to the text.
*
Expand Down Expand Up @@ -96,12 +120,14 @@ function Policy:clearPolicy()
if self.model["p"] then
for _, v in pairs(self.model["p"]) do
v.policy = {}
v.policyMap={}
end
end

if self.model["g"] then
for _, v in pairs(self.model["g"]) do
v.policy = {}
v.policyMap={}
end
end
end
Expand Down Expand Up @@ -163,12 +189,11 @@ end
* @return whether the rule exists.
]]
function Policy:hasPolicy(sec, ptype, rule)
for _, r in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(rule, r) then
return true
end
if self.model[sec][ptype].policyMap[table.concat(rule,",")]==nil then
return false
else
return true
end
return false
end

--[[
Expand Down Expand Up @@ -200,6 +225,7 @@ end
function Policy:addPolicy(sec, ptype, rule)
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
return true
end
return false
Expand All @@ -217,6 +243,7 @@ function Policy:addPolicies(sec, ptype, rules)
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

Expand All @@ -238,14 +265,16 @@ end
]]
function Policy:updatePolicy(sec, ptype, oldRule, newRule)
if not self:hasPolicy(sec, ptype, oldRule) then return false end

for k, v in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(oldRule, v) then
table.remove(self.model[sec][ptype].policy, k)
table.insert(self.model[sec][ptype].policy, newRule)
return true
end
local key = table.concat(oldRule,",")
local index = self.model[sec][ptype].policyMap[key]
table.remove(self.model[sec][ptype].policy, index)
table.insert(self.model[sec][ptype].policy, newRule)
self.model[sec][ptype].policyMap={}
for i,policy in pairs(self.model[sec][ptype].policy) do
local tempKey=table.concat(policy,",")
self.model[sec][ptype].policyMap[tempKey]=i
end
return true
end

-- Updates multiple policy rules from the model.
Expand All @@ -268,12 +297,16 @@ end
* @return succeeds or not.
]]
function Policy:removePolicy(sec, ptype, rule)
for i = 1, #self.model[sec][ptype].policy do
local r = self.model[sec][ptype].policy[i]
if Util.arrayEquals(r, rule) then
table.remove(self.model[sec][ptype].policy, i)
return true
if self:hasPolicy(sec,ptype,rule) then
local key = table.concat(rule,",")
local index = self.model[sec][ptype].policyMap[key]
table.remove(self.model[sec][ptype].policy, index)
self.model[sec][ptype].policyMap={}
for i,policy in pairs(self.model[sec][ptype].policy) do
local tempKey=table.concat(policy,",")
self.model[sec][ptype].policyMap[tempKey]=i
end
return true
end
return false
end
Expand Down Expand Up @@ -301,12 +334,9 @@ end
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
if self:hasPolicy(sec, ptype, rule) then
table.insert(effected,rule)
self:removePolicy(sec,ptype,rule)
end
end
return effected
Expand Down Expand Up @@ -346,8 +376,12 @@ function Policy:removeFilteredPolicy(sec, ptype, fieldIndex, fieldValues)
table.insert(tmp, rule)
end
end

self.model[sec][ptype].policyMap={}
self.model[sec][ptype].policy = tmp
for i,policy in pairs(self.model[sec][ptype].policy) do
local tempKey=table.concat(policy,",")
self.model[sec][ptype].policyMap[tempKey]=i
end
return res, effects
end

Expand Down
8 changes: 8 additions & 0 deletions src/util/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,19 @@ function Util.loadPolicyLine(line, model)
end

model.model[sec][key].policy = model.model[sec][key].policy or {}
model.model[sec][key].policyMap={}
for i,policy in pairs(model.model[sec][key].policy) do
model.model[sec][key].policyMap[table.concat(policy,",")]=i
end
local rules = {}
for i = 2, #tokens do
table.insert(rules, tokens[i])
end
table.insert(model.model[sec][key].policy, rules)
model.model[sec][key].policyMap={}
for i,policy in pairs(model.model[sec][key].policy) do
model.model[sec][key].policyMap[table.concat(policy,",")]=i
end
end


Expand Down
4 changes: 2 additions & 2 deletions tests/model/model_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("model tests", function()

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))
assert.is.False(m:removePolicy("p", "p", rules[1]))
end)

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

0 comments on commit 2a67105

Please sign in to comment.