@@ -25,26 +25,26 @@ function Model:new()
2525 self .__index = self
2626 self .model = {}
2727 self .sectionNameMap = {
28- [" r" ] = " request_definition" ,
29- [" p" ] = " policy_definition" ,
30- [" g" ] = " role_definition" ,
31- [" e" ] = " policy_effect" ,
32- [" m" ] = " matchers"
28+ [" r" ] = " request_definition" ,
29+ [" p" ] = " policy_definition" ,
30+ [" g" ] = " role_definition" ,
31+ [" e" ] = " policy_effect" ,
32+ [" m" ] = " matchers"
3333 }
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
3737
3838 -- PolicyOperations: [key] = POLICY_ADD/POLICY_REMOVE and value = string(key)
3939 self .PolicyOperations = {
40- POLICY_ADD = " POLICY_ADD" ,
41- POLICY_REMOVE = " POLICY_REMOVE"
40+ POLICY_ADD = " POLICY_ADD" ,
41+ POLICY_REMOVE = " POLICY_REMOVE"
4242 }
4343 return o
4444end
4545
4646function Model :getModCount ()
47- return self .modCount
47+ return self .modCount
4848end
4949
5050function Model :loadAssertion (model , cfg , sec , key )
@@ -53,48 +53,48 @@ function Model:loadAssertion(model, cfg, sec, key)
5353end
5454
5555--[[
56- * addDef adds an assertion to the model.
57- *
58- * @param sec the section, "p" or "g".
59- * @param key the policy type, "p", "p2", .. or "g", "g2", ..
60- * @param value the policy rule, separated by ", ".
61- * @return succeeds or not.
56+ * addDef adds an assertion to the model.
57+ *
58+ * @param sec the section, "p" or "g".
59+ * @param key the policy type, "p", "p2", .. or "g", "g2", ..
60+ * @param value the policy rule, separated by ", ".
61+ * @return succeeds or not.
6262]]
6363function Model :addDef (sec , key , value )
6464
65- if value == " " then return false end
65+ if value == " " then return false end
6666
67- if self .model [sec ] == nil then
68- self .model [sec ] = {}
69- end
67+ if self .model [sec ] == nil then
68+ self .model [sec ] = {}
69+ end
7070
71- if self .model [sec ][key ] == nil then
72- self .model [sec ][key ] = {}
73- end
71+ if self .model [sec ][key ] == nil then
72+ self .model [sec ][key ] = {}
73+ end
7474
75- self .model [sec ][key ] = Assertion :new ()
76- self .model [sec ][key ].key = key
77- self .model [sec ][key ].value = value
78-
79- if sec == " r" or sec == " p" then
80- self .model [sec ][key ].tokens = Util .splitCommaDelimited (self .model [sec ][key ].value )
81- for k , v in pairs (self .model [sec ][key ].tokens ) do
82- self .model [sec ][key ].tokens [k ] = key .. " _" .. self .model [sec ][key ].tokens [k ]
83- end
84- else
85- self .model [sec ][key ].value = Util .removeComments (Util .escapeAssertion (self .model [sec ][key ].value ))
86- end
75+ self .model [sec ][key ] = Assertion :new ()
76+ self .model [sec ][key ].key = key
77+ self .model [sec ][key ].value = value
8778
88- self .modCount = self .modCount + 1
89- return true
79+ if sec == " r" or sec == " p" then
80+ self .model [sec ][key ].tokens = Util .splitCommaDelimited (self .model [sec ][key ].value )
81+ for k , v in pairs (self .model [sec ][key ].tokens ) do
82+ self .model [sec ][key ].tokens [k ] = key .. " _" .. self .model [sec ][key ].tokens [k ]
83+ end
84+ else
85+ self .model [sec ][key ].value = Util .removeComments (Util .escapeAssertion (self .model [sec ][key ].value ))
86+ end
87+
88+ self .modCount = self .modCount + 1
89+ return true
9090end
9191
9292function Model :getKeySuffix (i )
9393 if i == 1 then
94- return " "
95- end
94+ return " "
95+ end
9696
97- return " " .. i
97+ return " " .. i
9898end
9999
100100function Model :loadSection (model , cfg , sec )
@@ -109,9 +109,9 @@ function Model:loadSection(model, cfg, sec)
109109end
110110
111111--[[
112- * loadModel loads the model from model CONF file.
113- *
114- * @param path the path of the model file.
112+ * loadModel loads the model from model CONF file.
113+ *
114+ * @param path the path of the model file.
115115]]
116116function Model :loadModel (path )
117117 local cfg = Config :newConfig (path )
@@ -125,9 +125,9 @@ function Model:loadModel(path)
125125end
126126
127127--[[
128- * loadModelFromText loads the model from the text.
129- *
130- * @param text the model text.
128+ * loadModelFromText loads the model from the text.
129+ *
130+ * @param text the model text.
131131]]
132132function Model :loadModelFromText (text )
133133 local cfg = Config :newConfigFromText (text )
@@ -141,31 +141,31 @@ function Model:loadModelFromText(text)
141141end
142142
143143--[[
144- * saveSectionToText saves the section to the text.
145- *
146- * @return the section text.
144+ * saveSectionToText saves the section to the text.
145+ *
146+ * @return the section text.
147147]]
148148function Model :saveSectionToText (sec )
149- local res = " [" .. self .sectionNameMap [sec ] .. " ]\n "
149+ local res = " [" .. self .sectionNameMap [sec ] .. " ]\n "
150150
151- if not self .model [sec ] then
152- return " "
153- end
151+ if not self .model [sec ] then
152+ return " "
153+ end
154154
155- for key , ast in pairs (self .model [sec ]) do
156- local val = ast .value :gsub (" %_" , " ." )
157- local x = string.format (" %s = %s\n " , key , val )
155+ for key , ast in pairs (self .model [sec ]) do
156+ local val = ast .value :gsub (" %_" , " ." )
157+ local x = string.format (" %s = %s\n " , key , val )
158158
159- res = res .. x
160- end
159+ res = res .. x
160+ end
161161
162- return res
162+ return res
163163end
164164
165165--[[
166- * toText saves the model to the text.
167- *
168- * @return the model text.
166+ * toText saves the model to the text.
167+ *
168+ * @return the model text.
169169]]
170170function Model :toText ()
171171 local tokenPatterns = {}
@@ -177,29 +177,29 @@ function Model:toText()
177177 local s = " "
178178 local writeString = function (sec )
179179 local result = " "
180- for ptype ,_ in pairs (self .model [sec ]) do
180+ for ptype ,_ in pairs (self .model [sec ]) do
181181 local value = self .model [sec ][ptype ].value
182182 for tokenPattern ,newToken in pairs (tokenPatterns ) do
183183 value = string.gsub (value ,tokenPattern ,newToken )
184184 end
185185 result = result .. sec .. " =" .. value .. " \n "
186186 end
187- return result
187+ return result
188188 end
189189 s = s .. " [request_definition]\n " .. writeString (" r" ).. " [policy_definition]\n " .. writeString (" p" )
190190 if self .model [" g" ] then
191191 s = s .. " [role_definition]\n "
192192 for ptype ,_ in pairs (self .model [" g" ]) do
193193 s = s .. ptype .. " =" .. self .model [" g" ][ptype ].value .. " \n "
194194 end
195- end
195+ end
196196 s = s .. " [policy_effect]\n " .. writeString (" e" ).. " [matchers]\n " .. writeString (" m" )
197197 return s
198198end
199199
200- -- * printModel prints the model to the log.
200+ -- * printModel prints the model to the log.
201201function Model :printModel ()
202- self .logger :info (" Model: \n " )
202+ self .logger :info (" Model: \n " )
203203 for k ,v in pairs (self .model ) do
204204 for k2 , v2 in pairs (v ) do
205205 self .logger :info (" [%s.%s]:" , k , k2 )
@@ -210,23 +210,23 @@ end
210210
211211-- sortPoliciesByPriority sorts policies by their priorities if 'priority' token exists
212212function Model :sortPoliciesByPriority ()
213- if not self .model [" p" ] then return end
214-
215- for ptype , ast in pairs (self .model [" p" ]) do
216- local priorityIndex = 0
217- for inx , token in pairs (ast .tokens ) do
218- if token == ptype .. " _priority" then
219- priorityIndex = inx
220- break
221- end
222- end
223- if priorityIndex == 0 then
224- return
225- end
226-
227- table.sort (ast .policy , function (a , b )
228- return a [priorityIndex ] < b [priorityIndex ]
229- end )
213+ if not self .model [" p" ] then return end
214+
215+ for ptype , ast in pairs (self .model [" p" ]) do
216+ local priorityIndex = 0
217+ for inx , token in pairs (ast .tokens ) do
218+ if token == ptype .. " _priority" then
219+ priorityIndex = inx
220+ break
221+ end
222+ end
223+ if priorityIndex == 0 then
224+ return
225+ end
226+
227+ table.sort (ast .policy , function (a , b )
228+ return a [priorityIndex ] < b [priorityIndex ]
229+ end )
230230 end
231231end
232232
0 commit comments