11import logging
22
3- from casbin import util
4-
5-
63class Policy :
74 def __init__ (self ):
85 self .logger = logging .getLogger ()
@@ -36,7 +33,7 @@ def clear_policy(self):
3633 if sec not in self .model .keys ():
3734 continue
3835
39- for key , ast in self .model [sec ].items ():
36+ for key in self .model [sec ].keys ():
4037 self .model [sec ][key ].policy = []
4138
4239 def get_policy (self , sec , ptype ):
@@ -46,19 +43,10 @@ def get_policy(self, sec, ptype):
4643
4744 def get_filtered_policy (self , sec , ptype , field_index , * field_values ):
4845 """gets rules based on field filters from a policy."""
49- res = []
50-
51- for rule in self .model [sec ][ptype ].policy :
52- matched = True
53- for i , field_value in enumerate (field_values ):
54- if field_value != '' and rule [field_index + i ] != field_value :
55- matched = False
56- break
57-
58- if matched :
59- res .append (rule )
60-
61- return res
46+ return [
47+ rule for rule in self .model [sec ][ptype ].policy
48+ if all (value == "" or rule [field_index + i ] == value for i , value in enumerate (field_values ))
49+ ]
6250
6351 def has_policy (self , sec , ptype , rule ):
6452 """determines whether a model has the specified policy rule."""
@@ -80,14 +68,14 @@ def add_policy(self, sec, ptype, rule):
8068
8169 def add_policies (self ,sec ,ptype ,rules ):
8270 """adds policy rules to the model."""
83-
71+
8472 for rule in rules :
8573 if self .has_policy (sec ,ptype ,rule ):
8674 return False
8775
8876 for rule in rules :
8977 self .model [sec ][ptype ].policy .append (rule )
90-
78+
9179 return True
9280
9381 def update_policy (self , sec , ptype , old_rule , new_rule ):
@@ -109,7 +97,6 @@ def update_policies(self, sec, ptype, old_rules, new_rules):
10997
11098 def remove_policy (self , sec , ptype , rule ):
11199 """removes a policy rule from the model."""
112-
113100 if not self .has_policy (sec , ptype , rule ):
114101 return False
115102
@@ -126,7 +113,7 @@ def remove_policies(self, sec, ptype, rules):
126113 self .model [sec ][ptype ].policy .remove (rule )
127114 if rule in self .model [sec ][ptype ].policy :
128115 return False
129-
116+
130117 return True
131118
132119 def remove_filtered_policy (self , sec , ptype , field_index , * field_values ):
@@ -140,13 +127,7 @@ def remove_filtered_policy(self, sec, ptype, field_index, *field_values):
140127 return res
141128
142129 for rule in self .model [sec ][ptype ].policy :
143- matched = True
144- for i , field_value in enumerate (field_values ):
145- if field_value != '' and rule [field_index + i ] != field_value :
146- matched = False
147- break
148-
149- if matched :
130+ if all (value == "" or rule [field_index + i ] == value for i , value in enumerate (field_values )):
150131 res = True
151132 else :
152133 tmp .append (rule )
@@ -157,14 +138,15 @@ def remove_filtered_policy(self, sec, ptype, field_index, *field_values):
157138
158139 def get_values_for_field_in_policy (self , sec , ptype , field_index ):
159140 """gets all values for a field for all rules in a policy, duplicated values are removed."""
160-
161141 values = []
162142 if sec not in self .model .keys ():
163143 return values
164144 if ptype not in self .model [sec ]:
165145 return values
166146
167147 for rule in self .model [sec ][ptype ].policy :
168- values .append (rule [field_index ])
148+ value = rule [field_index ]
149+ if value not in values :
150+ values .append (value )
169151
170- return util . array_remove_duplicates ( values )
152+ return values
0 commit comments