@@ -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
9090end
@@ -163,12 +163,38 @@ function Model:saveSectionToText(sec)
163163end
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
172198end
173199
174200-- * printModel prints the model to the log.
0 commit comments