Skip to content

Commit 3f7e220

Browse files
authored
feat: Implement loadFilteredPolicy() (#84)
Signed-off-by: Rushikesh Tote <rushi.tote@gmail.com>
1 parent bcda75a commit 3f7e220

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

src/main/CoreEnforcer.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,18 @@ end
225225
* @param filter the filter used to specify which type of policy should be loaded.
226226
]]
227227
function CoreEnforcer:loadFilteredPolicy(filter)
228+
self.model:clearPolicy()
229+
if not Util.isInstance(self.adapter, FilteredAdapter) then
230+
error("Filtered policies are not supported by this adapter.")
231+
end
228232

233+
self.adapter:loadFilteredPolicy(self.model, filter)
234+
235+
self:initBuildRoleLinks()
236+
self.model:printPolicy()
237+
if self.autoBuildRoleLinks then
238+
self:buildRoleLinks()
239+
end
229240
end
230241

231242
--[[

src/persist/file_adapter/FilteredAdapter.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ Filter.__index = Filter
3333
* supports loading of filtered policies.
3434
]]
3535
FilteredAdapter = {
36-
adapter,
37-
isFiltered = true,
38-
filePath,
39-
filter = {}
36+
isFiltered = true
4037
}
4138
setmetatable(FilteredAdapter, Adapter)
4239

@@ -46,6 +43,7 @@ function FilteredAdapter:new(filePath)
4643
self.__index = self
4744
o.filePath = filePath
4845
o.adapter = FileAdapter:new(filePath)
46+
o.filter = {}
4947
o.filter = setmetatable(o.filter, Filter)
5048
return o
5149
end
@@ -132,7 +130,7 @@ function FilteredAdapter:filterWords(line, filter)
132130
local i = 1
133131
for _, v in pairs(filter) do
134132
i = i + 1
135-
if #v>0 and Util.trim(v) == Util.trim(line[i]) then
133+
if #v>0 and Util.trim(v) ~= Util.trim(line[i]) then
136134
skipLine = true
137135
break
138136
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--Copyright 2021 The casbin Authors. All Rights Reserved.
2+
--
3+
--Licensed under the Apache License, Version 2.0 (the "License");
4+
--you may not use this file except in compliance with the License.
5+
--You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
--Unless required by applicable law or agreed to in writing, software
10+
--distributed under the License is distributed on an "AS IS" BASIS,
11+
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
--See the License for the specific language governing permissions and
13+
--limitations under the License.
14+
15+
local filtered_adapter_module = require("src.persist.file_adapter.FilteredAdapter")
16+
local enforcer_module = require("src.main.Enforcer")
17+
local path = os.getenv("PWD") or io.popen("cd"):read()
18+
19+
describe("FilteredAdapter tests", function ()
20+
it("init FilteredAdapter test", function ()
21+
local adapter = FilteredAdapter:new(path .. "/examples/rbac_with_domains_policy.csv")
22+
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", adapter)
23+
assert.is.False(e:HasPolicy("admin", "domain1", "data1", "read"))
24+
end)
25+
26+
it("load filtered policy test", function ()
27+
local adapter = FilteredAdapter:new(path .. "/examples/rbac_with_domains_policy.csv")
28+
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", path .. "/examples/rbac_with_domains_policy.csv")
29+
e:setAdapter(adapter)
30+
31+
assert.is.True(e:HasPolicy("admin", "domain1", "data1", "read"))
32+
assert.is.True(e:HasPolicy("admin", "domain2", "data2", "read"))
33+
34+
local filter = {}
35+
setmetatable(filter, Filter)
36+
filter.G = {"", "", "domain1"}
37+
filter.P = {"", "domain1"}
38+
39+
e:loadFilteredPolicy(filter)
40+
41+
assert.is.True(e:HasPolicy("admin", "domain1", "data1", "read"))
42+
assert.is.False(e:HasPolicy("admin", "domain2", "data2", "read"))
43+
end)
44+
45+
it("invalid filter test", function ()
46+
local adapter = FilteredAdapter:new(path .. "/examples/rbac_with_domains_policy.csv")
47+
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", path .. "/examples/rbac_with_domains_policy.csv")
48+
e:setAdapter(adapter)
49+
50+
local filter = {"", "domain1"}
51+
assert.has_error(function ()
52+
e:loadFilteredPolicy(filter)
53+
end)
54+
end)
55+
56+
it("empty filter test", function ()
57+
local adapter = FilteredAdapter:new(path .. "/examples/rbac_with_domains_policy.csv")
58+
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", path .. "/examples/rbac_with_domains_policy.csv")
59+
e:setAdapter(adapter)
60+
61+
e:loadFilteredPolicy(nil)
62+
63+
assert.is.False(e.adapter.isFiltered)
64+
end)
65+
66+
it("unsupported filtered policy test", function ()
67+
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", path .. "/examples/rbac_with_domains_policy.csv")
68+
69+
local filter = {}
70+
setmetatable(filter, Filter)
71+
filter.G = {"", "", "domain1"}
72+
filter.P = {"", "domain1"}
73+
assert.has_error(function ()
74+
e:loadFilteredPolicy(filter)
75+
end)
76+
end)
77+
78+
it("invalid file path test", function ()
79+
local adapter = FilteredAdapter:new(path .. "/examples/does_not_exist_policy.csv")
80+
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", path .. "/examples/rbac_with_domains_policy.csv")
81+
e:setAdapter(adapter)
82+
83+
assert.has_error(function ()
84+
e:loadFilteredPolicy(nil)
85+
end)
86+
end)
87+
end)

0 commit comments

Comments
 (0)