-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Policy.lua
372 lines (332 loc) · 10.6 KB
/
Policy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.
require "src/model/Assertion"
require "src/util/Util"
require "src/util/Log"
-- model's struct is map<string, map<string, Assertion>>
Policy = {}
function Policy:new()
local o = {}
o.logger = Log:getLogger()
setmetatable(o, self)
self.__index = self
return o
end
--[[
* buildRoleLinks initializes the roles in RBAC.
*
* @param rm the role manager.
]]
function Policy:buildRoleLinks(rmMap)
if self.model["g"] then
for ptype, ast in pairs(self.model["g"]) do
local rm = rmMap[ptype]
ast:buildRoleLinks(rm)
end
end
end
--[[
* printPolicy prints the policy to log.
]]
function Policy:printPolicy()
self.logger:info("Policy: \n")
if self.model["p"] then
for k, ast in pairs(self.model["p"]) do
self.logger:info("%s: %s:", k, ast.value)
self.logger:info(ast.policy)
end
end
if self.model["g"] then
for k, ast in pairs(self.model["g"]) do
self.logger:info("%s: %s:", k, ast.value)
self.logger:info(ast.policy)
end
end
end
--[[
* savePolicyToText saves the policy to the text.
*
* @return the policy text.
]]
function Policy:savePolicyToText()
local res = ""
if self.model["p"] then
for key, ast in pairs(self.model["p"]) do
for _, rule in pairs(ast.policy) do
local x = string.format("%s, %s\n", key, table.concat(rule, ", "))
res = res .. x
end
end
end
if self.model["g"] then
for key, ast in pairs(self.model["g"]) do
for _, rule in pairs(ast.policy) do
local x = string.format("%s, %s\n", key, table.concat(rule, ", "))
res = res .. x
end
end
end
return res
end
--[[
* clearPolicy clears all current policy.
]]
function Policy:clearPolicy()
if self.model["p"] then
for _, v in pairs(self.model["p"]) do
v.policy = {}
end
end
if self.model["g"] then
for _, v in pairs(self.model["g"]) do
v.policy = {}
end
end
end
--[[
* getPolicy gets all rules in a policy.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @return the policy rules of section sec and policy type ptype.
]]
function Policy:getPolicy(sec, ptype)
return self.model[sec][ptype].policy
end
--[[
* getFilteredPolicy gets rules based on field filters from a policy.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param ... fieldValues the field values to be matched, value ""
* means not to match this field.
* @return the filtered policy rules of section sec and policy type ptype.
]]
function Policy:getFilteredPolicy(sec, ptype, fieldIndex, ...)
local res = {}
local fieldValues = {...}
if not self.model[sec] then return res end
if not self.model[sec][ptype] then return res end
for _, rule in pairs(self.model[sec][ptype].policy) do
local matched = true
for i, v in ipairs(fieldValues) do
if v ~= "" and rule[fieldIndex + i] ~= v then
matched = false
break
end
end
if matched then
table.insert(res, rule)
end
end
return res
end
--[[
* hasPolicy determines whether a model has the specified policy rule.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rule the policy rule.
* @return whether the rule exists.
]]
function Policy:hasPolicy(sec, ptype, rule)
for _, r in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(rule, r) then
return true
end
end
return false
end
--[[
* addPolicy adds a policy rule to the model.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rule the policy rule.
* @return succeeds or not.
]]
function Policy:addPolicy(sec, ptype, rule)
if not self:hasPolicy(sec, ptype, rule) then
table.insert(self.model[sec][ptype].policy, rule)
return true
end
return false
end
--[[
* addPolicies adds policy rules to the model.
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policy rules.
* @return succeeds or not.
]]
function Policy:addPolicies(sec, ptype, rules)
local size = #self.model[sec][ptype].policy
for _, rule in pairs(rules) do
if not self:hasPolicy(sec, ptype, rule) then
table.insert(self.model[sec][ptype].policy, rule)
end
end
if size < #self.model[sec][ptype].policy then
return true
else
return false
end
end
--[[
* UpdatePolicy updates a policy rule from the model.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param oldRule the old rule.
* @param newRule the new rule.
* @return succeeds or not.
]]
function Policy:updatePolicy(sec, ptype, oldRule, newRule)
if not self:hasPolicy(sec, ptype, oldRule) then return false end
for k, v in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(oldRule, v) then
table.remove(self.model[sec][ptype].policy, k)
table.insert(self.model[sec][ptype].policy, newRule)
return true
end
end
end
-- Updates multiple policy rules from the model.
function Policy:updatePolicies(sec, ptype, oldRules, newRules)
for _, rule in pairs(oldRules) do
if not self:hasPolicy(sec, ptype, rule) then
return false
end
end
return self:removePolicies(sec, ptype, oldRules) and self:addPolicies(sec, ptype, newRules)
end
--[[
* removePolicy removes a policy rule from the model.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rule the policy rule.
* @return succeeds or not.
]]
function Policy:removePolicy(sec, ptype, rule)
for i = 1, #self.model[sec][ptype].policy do
local r = self.model[sec][ptype].policy[i]
if Util.arrayEquals(r, rule) then
table.remove(self.model[sec][ptype].policy, i)
return true
end
end
return false
end
--[[
* removePolicies removes rules from the current policy.
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policy rules.
* @return succeeds or not.
]]
function Policy:removePolicies(sec, ptype, rules)
local size = #self.model[sec][ptype].policy
for _, rule in pairs(rules) do
for k, v in pairs(self.model[sec][ptype].policy) do
if Util.arrayEquals(rule, v) then
table.remove(self.model[sec][ptype].policy, k)
break
end
end
end
if size > #self.model[sec][ptype].policy then
return true
else
return false
end
end
--[[
* removeFilteredPolicyReturnsEffects removes policy rules based on field filters from the model.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param ... fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds(effects.size() > 0) or not.
]]
function Policy:removeFilteredPolicyReturnsEffects(sec, ptype, fieldIndex, ...)
return {}
end
--[[
* removeFilteredPolicy removes policy rules based on field filters from the model.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param fieldIndex the policy rule's start index to be matched.
* @param ... fieldValues the field values to be matched, value ""
* means not to match this field.
* @return succeeds or not.
]]
function Policy:removeFilteredPolicy(sec, ptype, fieldIndex, fieldValues)
local tmp = {}
local res = false
local effects = {}
if not self.model[sec] then return res end
if not self.model[sec][ptype] then return res end
for _, rule in pairs(self.model[sec][ptype].policy) do
local matched = true
for i, value in pairs(fieldValues) do
if value ~= "" and rule[fieldIndex+i] ~= value then
matched = false
break
end
end
if matched then
table.insert(effects, rule)
res = true
else
table.insert(tmp, rule)
end
end
self.model[sec][ptype].policy = tmp
return res, effects
end
--[[
* getValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param fieldIndex the policy rule's index.
* @return the field values specified by fieldIndex.
]]
function Policy:getValuesForFieldInPolicy(sec, ptype, fieldIndex)
local values = {}
for _, rule in pairs(self.model[sec][ptype].policy) do
values[#values + 1] = rule[fieldIndex]
end
values = Util.arrayRemoveDuplications(values)
return values
end
function Policy:buildIncrementalRoleLinks(rm, op, sec, ptype, rules)
if sec == "g" then
self.model[sec][ptype]:buildIncrementalRoleLinks(rm, op, rules)
end
end
function Policy:hasPolicies(sec, ptype, rules)
for _, rule in pairs(rules) do
if self:hasPolicy(sec, ptype, rule) then
return true
end
end
return false
end
return Policy