25
25
import java .util .ArrayList ;
26
26
import java .util .List ;
27
27
28
+ import static java .util .Collections .singletonList ;
29
+
28
30
/**
29
31
* InternalEnforcer = CoreEnforcer + Internal API.
30
32
*/
@@ -33,6 +35,11 @@ class InternalEnforcer extends CoreEnforcer {
33
35
* addPolicy adds a rule to the current policy.
34
36
*/
35
37
boolean addPolicy (String sec , String ptype , List <String > rule ) {
38
+ if (mustUseDispatcher ()) {
39
+ dispatcher .addPolicies (sec , ptype , singletonList (rule ));
40
+ return true ;
41
+ }
42
+
36
43
if (model .hasPolicy (sec , ptype , rule )) {
37
44
return false ;
38
45
}
@@ -50,11 +57,7 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {
50
57
51
58
model .addPolicy (sec , ptype , rule );
52
59
53
- if (sec .equals ("g" )) {
54
- List <List <String >> rules = new ArrayList <>();
55
- rules .add (rule );
56
- buildIncrementalRoleLinks (Model .PolicyOperations .POLICY_ADD , ptype , rules );
57
- }
60
+ buildIncrementalRoleLinks (sec , ptype , singletonList (rule ), Model .PolicyOperations .POLICY_ADD );
58
61
59
62
if (watcher != null && autoNotifyWatcher ) {
60
63
if (watcher instanceof WatcherEx ) {
@@ -71,6 +74,11 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {
71
74
* addPolicies adds rules to the current policy.
72
75
*/
73
76
boolean addPolicies (String sec , String ptype , List <List <String >> rules ) {
77
+ if (mustUseDispatcher ()) {
78
+ dispatcher .addPolicies (sec , ptype , rules );
79
+ return true ;
80
+ }
81
+
74
82
if (model .hasPolicies (sec , ptype , rules )) {
75
83
return false ;
76
84
}
@@ -90,9 +98,7 @@ boolean addPolicies(String sec, String ptype, List<List<String>> rules) {
90
98
91
99
model .addPolicies (sec , ptype , rules );
92
100
93
- if (sec .equals ("g" )) {
94
- buildIncrementalRoleLinks (Model .PolicyOperations .POLICY_ADD , ptype , rules );
95
- }
101
+ buildIncrementalRoleLinks (sec , ptype , rules , Model .PolicyOperations .POLICY_ADD );
96
102
97
103
if (watcher != null && autoNotifyWatcher ) {
98
104
watcher .update ();
@@ -115,6 +121,11 @@ public void buildIncrementalRoleLinks(Model.PolicyOperations op, String ptype, L
115
121
* removePolicy removes a rule from the current policy.
116
122
*/
117
123
boolean removePolicy (String sec , String ptype , List <String > rule ) {
124
+ if (mustUseDispatcher ()) {
125
+ dispatcher .removePolicies (sec , ptype , singletonList (rule ));
126
+ return true ;
127
+ }
128
+
118
129
if (adapter != null && autoSave ) {
119
130
try {
120
131
adapter .removePolicy (sec , ptype , rule );
@@ -132,11 +143,7 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
132
143
return false ;
133
144
}
134
145
135
- if (sec .equals ("g" )) {
136
- List <List <String >> rules = new ArrayList <>();
137
- rules .add (rule );
138
- buildIncrementalRoleLinks (Model .PolicyOperations .POLICY_REMOVE , ptype , rules );
139
- }
146
+ buildIncrementalRoleLinks (sec , ptype , singletonList (rule ), Model .PolicyOperations .POLICY_REMOVE );
140
147
141
148
if (watcher != null && autoNotifyWatcher ) {
142
149
if (watcher instanceof WatcherEx ) {
@@ -159,7 +166,7 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
159
166
* @return succeeds or not.
160
167
*/
161
168
boolean updatePolicy (String sec , String ptype , List <String > oldRule , List <String > newRule ) {
162
- if (dispatcher != null && autoNotifyDispatcher ) {
169
+ if (mustUseDispatcher () ) {
163
170
dispatcher .updatePolicy (sec , ptype , oldRule , newRule );
164
171
return true ;
165
172
}
@@ -225,6 +232,11 @@ boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String
225
232
* removePolicies removes rules from the current policy.
226
233
*/
227
234
boolean removePolicies (String sec , String ptype , List <List <String >> rules ) {
235
+ if (mustUseDispatcher ()) {
236
+ dispatcher .removePolicies (sec , ptype , rules );
237
+ return true ;
238
+ }
239
+
228
240
if (!model .hasPolicies (sec , ptype , rules )) {
229
241
return false ;
230
242
}
@@ -248,9 +260,7 @@ boolean removePolicies(String sec, String ptype, List<List<String>> rules) {
248
260
return false ;
249
261
}
250
262
251
- if (sec .equals ("g" )) {
252
- buildIncrementalRoleLinks (Model .PolicyOperations .POLICY_REMOVE , ptype , rules );
253
- }
263
+ buildIncrementalRoleLinks (sec , ptype , rules , Model .PolicyOperations .POLICY_REMOVE );
254
264
255
265
if (watcher != null && autoNotifyWatcher ) {
256
266
// error intentionally ignored
@@ -264,8 +274,13 @@ boolean removePolicies(String sec, String ptype, List<List<String>> rules) {
264
274
* removeFilteredPolicy removes rules based on field filters from the current policy.
265
275
*/
266
276
boolean removeFilteredPolicy (String sec , String ptype , int fieldIndex , String ... fieldValues ) {
277
+ if (mustUseDispatcher ()) {
278
+ dispatcher .removeFilteredPolicy (sec , ptype , fieldIndex , fieldValues );
279
+ return true ;
280
+ }
281
+
267
282
if (fieldValues == null || fieldValues .length == 0 ) {
268
- Util .logPrint ("Invaild fieldValues parameter" );
283
+ Util .logPrint ("Invalid fieldValues parameter" );
269
284
return false ;
270
285
}
271
286
@@ -287,9 +302,7 @@ boolean removeFilteredPolicy(String sec, String ptype, int fieldIndex, String...
287
302
return false ;
288
303
}
289
304
290
- if (sec .equals ("g" )) {
291
- buildIncrementalRoleLinks (Model .PolicyOperations .POLICY_REMOVE , ptype , effects );
292
- }
305
+ buildIncrementalRoleLinks (sec , ptype , effects , Model .PolicyOperations .POLICY_REMOVE );
293
306
294
307
if (watcher != null && autoNotifyWatcher ) {
295
308
// error intentionally ignored
@@ -315,4 +328,16 @@ int getDomainIndex(String ptype) {
315
328
}
316
329
return index ;
317
330
}
331
+
332
+ private void buildIncrementalRoleLinks (
333
+ final String sec ,
334
+ final String ptype ,
335
+ final List <List <String >> rules ,
336
+ final Model .PolicyOperations operation
337
+ ) {
338
+ if ("g" .equals (sec )) {
339
+ buildIncrementalRoleLinks (operation , ptype , rules );
340
+ }
341
+ }
342
+
318
343
}
0 commit comments