Skip to content

Commit bcda75a

Browse files
authored
feat: Add function Model:toText (#83)
* feat: Add function toText Signed-off-by: Edmond <edomondja@gmail.com> * feat: Add toText unit tests Signed-off-by: Edmond <edomondja@gmail.com>
1 parent 40bb1f7 commit bcda75a

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/model/Model.lua

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function Model:new()
3030
["g"] = "role_definition",
3131
["e"] = "policy_effect",
3232
["m"] = "matchers"
33-
}
33+
}
3434

3535
self.requiredSections = {"r", "p", "e", "m"} -- Minimal required sections for a model to be valid
3636
self.modCount = 0 -- used by CoreEnforcer to detect changes to Model
@@ -84,7 +84,7 @@ function Model:addDef(sec, key, value)
8484
else
8585
self.model[sec][key].value = Util.removeComments(Util.escapeAssertion(self.model[sec][key].value))
8686
end
87-
87+
8888
self.modCount = self.modCount + 1
8989
return true
9090
end
@@ -163,12 +163,38 @@ function Model:saveSectionToText(sec)
163163
end
164164

165165
--[[
166-
* saveModelToText saves the model to the text.
166+
* toText saves the model to the text.
167167
*
168168
* @return the model text.
169169
]]
170-
function Model:saveModelToText()
171-
170+
function Model:toText()
171+
local tokenPatterns={}
172+
for _,ptype in pairs({"r","p"}) do
173+
for _,token in pairs(self.model[ptype][ptype].tokens) do
174+
tokenPatterns[token]=string.gsub (string.gsub (token,"^p_","p."),"^r_","r.")
175+
end
176+
end
177+
local s=""
178+
local writeString=function(sec)
179+
local result=""
180+
for ptype,_ in pairs(self.model[sec]) do
181+
local value=self.model[sec][ptype].value
182+
for tokenPattern,newToken in pairs(tokenPatterns) do
183+
value=string.gsub(value,tokenPattern,newToken)
184+
end
185+
result=result..sec.."="..value.."\n"
186+
end
187+
return result
188+
end
189+
s=s.."[request_definition]\n"..writeString("r").."[policy_definition]\n"..writeString("p")
190+
if self.model["g"] then
191+
s=s.."[role_definition]\n"
192+
for ptype,_ in pairs(self.model["g"]) do
193+
s=s..ptype.."="..self.model["g"][ptype].value.."\n"
194+
end
195+
end
196+
s=s.."[policy_effect]\n"..writeString("e").."[matchers]\n"..writeString("m")
197+
return s
172198
end
173199

174200
-- * printModel prints the model to the log.

tests/model/model_spec.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ local basic_path = path .. "/examples/basic_model.conf"
1919
local rbac_path = path .. "/examples/rbac_model.conf"
2020
local rbac_with_domains_path = path .. "/examples/rbac_with_domains_model.conf"
2121

22-
describe("model tests", function()
23-
22+
describe("model tests", function()
23+
2424
it("test getPolicy", function ()
2525
local m = Model:new()
2626
m:loadModel(basic_path)
@@ -30,7 +30,7 @@ describe("model tests", function()
3030

3131
assert.are.same(m:getPolicy("p", "p"), {rule})
3232
end)
33-
33+
3434
it("test hasPolicy", function ()
3535
local m = Model:new()
3636
m:loadModel(basic_path)
@@ -182,6 +182,15 @@ describe("model tests", function()
182182
}
183183
assert.are.same(res, filteredRules)
184184

185+
end)
186+
187+
it("test toText", function ()
188+
local m = Model:new()
189+
m:loadModel(basic_path)
190+
local res = m:toText()
191+
local saveText="[request_definition]\nr=sub, obj, act\n[policy_definition]\np=sub, obj, act\n[policy_effect]\ne=some(where (p_eft == allow))\n[matchers]\nm=r.sub == p.sub && r.obj == p.obj && r.act == p.act\n"
192+
assert.are.same(res, saveText)
193+
185194
end)
186195

187196
it("test printPolicy and printModel", function ()

0 commit comments

Comments
 (0)