diff --git a/src/model/Model.lua b/src/model/Model.lua index 12cfc41..0dfce5a 100644 --- a/src/model/Model.lua +++ b/src/model/Model.lua @@ -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 @@ -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 @@ -163,12 +163,38 @@ function Model:saveSectionToText(sec) end --[[ - * saveModelToText saves the model to the text. + * toText saves the model to the text. * * @return the model text. ]] -function Model:saveModelToText() - +function Model:toText() + 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. diff --git a/tests/model/model_spec.lua b/tests/model/model_spec.lua index d5df0a4..55a9ebf 100644 --- a/tests/model/model_spec.lua +++ b/tests/model/model_spec.lua @@ -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) @@ -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) @@ -182,6 +182,15 @@ describe("model tests", function() } assert.are.same(res, filteredRules) + end) + + it("test toText", function () + local m = Model:new() + m:loadModel(basic_path) + local res = m:toText() + 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 ()