Skip to content

Commit

Permalink
feat: loading policy with priority explicitly
Browse files Browse the repository at this point in the history
Signed-off-by: Rushikesh Tote <[email protected]>
  • Loading branch information
rushitote committed May 21, 2021
1 parent 0b8e7da commit 77a115f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
29 changes: 29 additions & 0 deletions spec/main/enforcer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,33 @@ describe("Enforcer tests", function ()
assert.is.True(e:enforce("bob", "data2", "read"))
assert.is.True(e:enforce("bob", "data2", "write"))
end)

it("explicit priority test", function ()
local model = path .. "/examples/priority_model_explicit.conf"
local policy = path .. "/examples/priority_policy_explicit.csv"

local e = Enforcer:new(model, policy)
assert.is.True(e:enforce("alice", "data1", "write"))
assert.is.True(e:enforce("alice", "data1", "read"))
assert.is.False(e:enforce("bob", "data2", "read"))
assert.is.True(e:enforce("bob", "data2", "write"))
assert.is.False(e:enforce("data1_deny_group", "data1", "read"))
assert.is.False(e:enforce("data1_deny_group", "data1", "write"))
assert.is.True(e:enforce("data2_allow_group", "data2", "read"))
assert.is.True(e:enforce("data2_allow_group", "data2", "write"))

local rule = {"1", "bob", "data2", "write", "deny"}
e.model:addPolicy("p", "p", rule)
e.model:sortPoliciesByPriority()
e.model:printPolicy()

assert.is.True(e:enforce("alice", "data1", "write"))
assert.is.True(e:enforce("alice", "data1", "read"))
assert.is.False(e:enforce("bob", "data2", "read"))
assert.is.False(e:enforce("bob", "data2", "write"))
assert.is.False(e:enforce("data1_deny_group", "data1", "read"))
assert.is.False(e:enforce("data1_deny_group", "data1", "write"))
assert.is.True(e:enforce("data2_allow_group", "data2", "read"))
assert.is.True(e:enforce("data2_allow_group", "data2", "write"))
end)
end)
5 changes: 4 additions & 1 deletion src/main/CoreEnforcer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ end
]]
function CoreEnforcer:loadPolicy()
self.model:clearPolicy()
self.adapter:loadPolicy(self.model);
self.adapter:loadPolicy(self.model)

self.model:sortPoliciesByPriority()
self.model:printPolicy()

if self.autoBuildRoleLinks then
self:buildRoleLinks()
end
Expand Down
22 changes: 22 additions & 0 deletions src/model/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,26 @@ function Model:printModel()
end
end

-- sortPoliciesByPriority sorts policies by their priorities if 'priority' token exists
function Model:sortPoliciesByPriority()
if not self.model["p"] then return end

for ptype, ast in pairs(self.model["p"]) do
local priorityIndex = 0
for inx, token in pairs(ast.tokens) do
if token == ptype .. "_priority" then
priorityIndex = inx
break
end
end
if priorityIndex == 0 then
return
end

table.sort(ast.policy, function (a, b)
return a[priorityIndex] < b[priorityIndex]
end)
end
end

return Model

0 comments on commit 77a115f

Please sign in to comment.