Skip to content

Commit

Permalink
Merge d3e889d into 40bb1f7
Browse files Browse the repository at this point in the history
  • Loading branch information
Edmond-J-A committed Jul 11, 2021
2 parents 40bb1f7 + d3e889d commit a0be4ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
32 changes: 29 additions & 3 deletions src/model/Model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Model:new()
["g"] = "role_definition",
["e"] = "policy_effect",
["m"] = "matchers"
}
}

self.requiredSections = {"r", "p", "e", "m"} -- Minimal required sections for a model to be valid
self.modCount = 0 -- used by CoreEnforcer to detect changes to Model
Expand Down Expand Up @@ -84,7 +84,7 @@ function Model:addDef(sec, key, value)
else
self.model[sec][key].value = Util.removeComments(Util.escapeAssertion(self.model[sec][key].value))
end

self.modCount = self.modCount + 1
return true
end
Expand Down Expand Up @@ -168,7 +168,33 @@ end
* @return the model text.
]]
function Model:saveModelToText()

local tokenPatterns={}
for _,ptype in pairs({"r","p"}) do
for _,token in pairs(self.model[ptype][ptype].tokens) do
tokenPatterns[token]=string.gsub (string.gsub (token,"^p_","p."),"^r_","r.")
end
end
local s=""
local writeString=function(sec)
local result=""
for ptype,_ in pairs(self.model[sec]) do
local value=self.model[sec][ptype].value
for tokenPattern,newToken in pairs(tokenPatterns) do
value=string.gsub(value,tokenPattern,newToken)
end
result=result..sec.."="..value.."\n"
end
return result
end
s=s.."[request_definition]\n"..writeString("r").."[policy_definition]\n"..writeString("p")
if self.model["g"] then
s=s.."[role_definition]\n"
for ptype,_ in pairs(self.model["g"]) do
s=s..ptype.."="..self.model["g"][ptype].value.."\n"
end
end
s=s.."[policy_effect]\n"..writeString("e").."[matchers]\n"..writeString("m")
return s
end

-- * printModel prints the model to the log.
Expand Down
15 changes: 12 additions & 3 deletions tests/model/model_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ local basic_path = path .. "/examples/basic_model.conf"
local rbac_path = path .. "/examples/rbac_model.conf"
local rbac_with_domains_path = path .. "/examples/rbac_with_domains_model.conf"

describe("model tests", function()
describe("model tests", function()

it("test getPolicy", function ()
local m = Model:new()
m:loadModel(basic_path)
Expand All @@ -30,7 +30,7 @@ describe("model tests", function()

assert.are.same(m:getPolicy("p", "p"), {rule})
end)

it("test hasPolicy", function ()
local m = Model:new()
m:loadModel(basic_path)
Expand Down Expand Up @@ -182,6 +182,15 @@ describe("model tests", function()
}
assert.are.same(res, filteredRules)

end)

it("test saveModelToText", function ()
local m = Model:new()
m:loadModel(basic_path)
local res = m:saveModelToText()
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"
assert.are.same(res, saveText)

end)

it("test printPolicy and printModel", function ()
Expand Down

0 comments on commit a0be4ce

Please sign in to comment.