Skip to content

Commit d9564ef

Browse files
authored
feat: update WatcherEx interface (#342)
* feat: update WatcherEx interface * feat: update WatcherEx interface * feat: update WatcherEx interface
1 parent b8df7d8 commit d9564ef

File tree

2 files changed

+122
-34
lines changed

2 files changed

+122
-34
lines changed

src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,44 @@
3131
* InternalEnforcer = CoreEnforcer + Internal API.
3232
*/
3333
class InternalEnforcer extends CoreEnforcer {
34+
35+
/**
36+
*
37+
* @param sec the section, "p" or "g".
38+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
39+
* @param rules the policies
40+
* @param updateType the UpdateType
41+
* @return indicate whether the notification to the Watcher is successful or not
42+
*/
43+
private boolean notifyWatcher(String sec, String ptype, List<List<String>> rules, WatcherEx.UpdateType updateType) {
44+
if(watcher == null || !autoNotifyWatcher) return true;
45+
try {
46+
if (watcher instanceof WatcherEx) switch (updateType) {
47+
case UpdateForAddPolicy:
48+
((WatcherEx) watcher).updateForAddPolicy(sec, ptype, rules.get(0).toArray(new String[0]));
49+
break;
50+
case UpdateForRemovePolicy:
51+
((WatcherEx) watcher).updateForRemovePolicy(sec, ptype, rules.get(0).toArray(new String[0]));
52+
break;
53+
case UpdateForAddPolicies:
54+
((WatcherEx) watcher).updateForAddPolicies(sec, ptype, rules);
55+
break;
56+
case UpdateForRemovePolicies:
57+
((WatcherEx) watcher).updateForRemovePolicies(sec, ptype, rules);
58+
break;
59+
default:
60+
Util.logPrint("UnsupportedUpdateType for notifyWatcher");
61+
break;
62+
} else {
63+
watcher.update();
64+
}
65+
} catch (Exception e) {
66+
Util.logPrint("An exception occurred:" + e.getMessage());
67+
return false;
68+
}
69+
return true;
70+
}
71+
3472
/**
3573
* addPolicy adds a rule to the current policy.
3674
*/
@@ -59,17 +97,10 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {
5997

6098
buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_ADD);
6199

62-
if (watcher != null && autoNotifyWatcher) {
63-
if (watcher instanceof WatcherEx) {
64-
((WatcherEx) watcher).updateForAddPolicy(rule.toArray(new String[0]));
65-
} else {
66-
watcher.update();
67-
}
68-
}
69-
70-
return true;
100+
return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForAddPolicy);
71101
}
72102

103+
73104
/**
74105
* addPolicies adds rules to the current policy.
75106
*/
@@ -100,11 +131,7 @@ boolean addPolicies(String sec, String ptype, List<List<String>> rules) {
100131

101132
buildIncrementalRoleLinks(sec, ptype, rules, Model.PolicyOperations.POLICY_ADD);
102133

103-
if (watcher != null && autoNotifyWatcher) {
104-
watcher.update();
105-
}
106-
107-
return true;
134+
return notifyWatcher(sec, ptype, rules, WatcherEx.UpdateType.UpdateForAddPolicies);
108135
}
109136

110137
/**
@@ -145,15 +172,7 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
145172

146173
buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_REMOVE);
147174

148-
if (watcher != null && autoNotifyWatcher) {
149-
if (watcher instanceof WatcherEx) {
150-
((WatcherEx) watcher).updateForRemovePolicy(rule.toArray(new String[0]));
151-
} else {
152-
watcher.update();
153-
}
154-
}
155-
156-
return true;
175+
return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForRemovePolicy);
157176
}
158177

159178
/**
@@ -262,12 +281,7 @@ boolean removePolicies(String sec, String ptype, List<List<String>> rules) {
262281

263282
buildIncrementalRoleLinks(sec, ptype, rules, Model.PolicyOperations.POLICY_REMOVE);
264283

265-
if (watcher != null && autoNotifyWatcher) {
266-
// error intentionally ignored
267-
watcher.update();
268-
}
269-
270-
return true;
284+
return notifyWatcher(sec, ptype, rules, WatcherEx.UpdateType.UpdateForRemovePolicies);
271285
}
272286

273287
/**
@@ -307,7 +321,7 @@ boolean removeFilteredPolicy(String sec, String ptype, int fieldIndex, String...
307321
if (watcher != null && autoNotifyWatcher) {
308322
// error intentionally ignored
309323
if (watcher instanceof WatcherEx) {
310-
((WatcherEx) watcher).updateForRemoveFilteredPolicy(fieldIndex, fieldValues);
324+
((WatcherEx) watcher).updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues);
311325
} else {
312326
watcher.update();
313327
}
@@ -339,5 +353,4 @@ private void buildIncrementalRoleLinks(
339353
buildIncrementalRoleLinks(operation, ptype, rules);
340354
}
341355
}
342-
343356
}

src/main/java/org/casbin/jcasbin/persist/WatcherEx.java

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,87 @@
1616

1717
import org.casbin.jcasbin.model.Model;
1818

19+
import java.util.List;
20+
1921
public interface WatcherEx extends Watcher {
20-
void updateForAddPolicy(String... params);
22+
/**
23+
* updateForAddPolicy calls the update callback of other instances to synchronize their policy.
24+
* It is called after a policy is added via Enforcer.addPolicy(), Enforcer.addNamedPolicy(),
25+
* Enforcer.addGroupingPolicy() and Enforcer.addNamedGroupingPolicy().
26+
*
27+
* @param sec the section, "p" or "g".
28+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
29+
* @param params the policy
30+
*/
31+
void updateForAddPolicy(String sec, String ptype, String... params);
2132

22-
void updateForRemovePolicy(String... params);
33+
/**
34+
* updateForRemovePolicy calls the update callback of other instances to synchronize their policy.
35+
* It is called after a policy is removed by Enforcer.removePolicy(), Enforcer.removeNamedPolicy(),
36+
* Enforcer.removeGroupingPolicy() and Enforcer.removeNamedGroupingPolicy().
37+
*
38+
* @param sec the section, "p" or "g".
39+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
40+
* @param params the policy
41+
*/
42+
void updateForRemovePolicy(String sec, String ptype, String... params);
2343

24-
void updateForRemoveFilteredPolicy(int fieldIndex, String... fieldValues);
44+
/**
45+
* updateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy.
46+
* It is called after Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(),
47+
* Enforcer.RemoveFilteredGroupingPolicy() and Enforcer.RemoveFilteredNamedGroupingPolicy().
48+
*
49+
* @param sec the section, "p" or "g".
50+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
51+
* @param fieldIndex the policy rule's start index to be matched.
52+
* @param fieldValues the field values to be matched, value "" means not to match this field.
53+
*/
54+
void updateForRemoveFilteredPolicy(String sec, String ptype, int fieldIndex, String... fieldValues);
2555

56+
/**
57+
* updateForSavePolicy calls the update callback of other instances to synchronize their policy.
58+
* It is called after Enforcer.savePolicy()
59+
*
60+
* @param model represents the whole access control model.
61+
*/
2662
void updateForSavePolicy(Model model);
63+
64+
/**
65+
* updateForAddPolicies calls the update callback of other instances to synchronize their policy.
66+
* It is called after Enforcer.addPolicies(), Enforcer.addNamedPolicies(),
67+
* Enforcer.addGroupingPolicies() and Enforcer.addNamedGroupingPolicies().
68+
*
69+
* @param sec the section, "p" or "g".
70+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
71+
* @param rules the policies
72+
*/
73+
void updateForAddPolicies(String sec, String ptype, List<List<String>> rules);
74+
75+
/**
76+
* updateForRemovePolicies calls the update callback of other instances to synchronize their policy.
77+
* It is called after Enforcer.removePolicies(), Enforcer.removeNamedPolicies(),
78+
* Enforcer.removeGroupingPolicies() and Enforcer.removeNamedGroupingPolicies().
79+
*
80+
* @param sec the section, "p" or "g".
81+
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
82+
* @param rules the policies
83+
*/
84+
void updateForRemovePolicies(String sec, String ptype, List<List<String>> rules);
85+
86+
/**
87+
*
88+
*/
89+
enum UpdateType {
90+
Update,
91+
UpdateForAddPolicies,
92+
UpdateForAddPolicy,
93+
UpdateForRemoveFilteredPolicy,
94+
UpdateForRemovePolicies,
95+
UpdateForRemovePolicy,
96+
UpdateForSavePolicy,
97+
UpdateForUpdatePolicies,
98+
UpdateForUpdatePolicy
99+
100+
}
101+
27102
}

0 commit comments

Comments
 (0)