Skip to content

Commit

Permalink
feat: update WatcherEx interface (#342)
Browse files Browse the repository at this point in the history
* feat: update WatcherEx interface

* feat: update WatcherEx interface

* feat: update WatcherEx interface
  • Loading branch information
PokIsemaine committed May 29, 2023
1 parent b8df7d8 commit d9564ef
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 34 deletions.
75 changes: 44 additions & 31 deletions src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@
* InternalEnforcer = CoreEnforcer + Internal API.
*/
class InternalEnforcer extends CoreEnforcer {

/**
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policies
* @param updateType the UpdateType
* @return indicate whether the notification to the Watcher is successful or not
*/
private boolean notifyWatcher(String sec, String ptype, List<List<String>> rules, WatcherEx.UpdateType updateType) {
if(watcher == null || !autoNotifyWatcher) return true;
try {
if (watcher instanceof WatcherEx) switch (updateType) {
case UpdateForAddPolicy:
((WatcherEx) watcher).updateForAddPolicy(sec, ptype, rules.get(0).toArray(new String[0]));
break;
case UpdateForRemovePolicy:
((WatcherEx) watcher).updateForRemovePolicy(sec, ptype, rules.get(0).toArray(new String[0]));
break;
case UpdateForAddPolicies:
((WatcherEx) watcher).updateForAddPolicies(sec, ptype, rules);
break;
case UpdateForRemovePolicies:
((WatcherEx) watcher).updateForRemovePolicies(sec, ptype, rules);
break;
default:
Util.logPrint("UnsupportedUpdateType for notifyWatcher");
break;
} else {
watcher.update();
}
} catch (Exception e) {
Util.logPrint("An exception occurred:" + e.getMessage());
return false;
}
return true;
}

/**
* addPolicy adds a rule to the current policy.
*/
Expand Down Expand Up @@ -59,17 +97,10 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {

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

if (watcher != null && autoNotifyWatcher) {
if (watcher instanceof WatcherEx) {
((WatcherEx) watcher).updateForAddPolicy(rule.toArray(new String[0]));
} else {
watcher.update();
}
}

return true;
return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForAddPolicy);
}


/**
* addPolicies adds rules to the current policy.
*/
Expand Down Expand Up @@ -100,11 +131,7 @@ boolean addPolicies(String sec, String ptype, List<List<String>> rules) {

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

if (watcher != null && autoNotifyWatcher) {
watcher.update();
}

return true;
return notifyWatcher(sec, ptype, rules, WatcherEx.UpdateType.UpdateForAddPolicies);
}

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

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

if (watcher != null && autoNotifyWatcher) {
if (watcher instanceof WatcherEx) {
((WatcherEx) watcher).updateForRemovePolicy(rule.toArray(new String[0]));
} else {
watcher.update();
}
}

return true;
return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForRemovePolicy);
}

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

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

if (watcher != null && autoNotifyWatcher) {
// error intentionally ignored
watcher.update();
}

return true;
return notifyWatcher(sec, ptype, rules, WatcherEx.UpdateType.UpdateForRemovePolicies);
}

/**
Expand Down Expand Up @@ -307,7 +321,7 @@ boolean removeFilteredPolicy(String sec, String ptype, int fieldIndex, String...
if (watcher != null && autoNotifyWatcher) {
// error intentionally ignored
if (watcher instanceof WatcherEx) {
((WatcherEx) watcher).updateForRemoveFilteredPolicy(fieldIndex, fieldValues);
((WatcherEx) watcher).updateForRemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues);
} else {
watcher.update();
}
Expand Down Expand Up @@ -339,5 +353,4 @@ private void buildIncrementalRoleLinks(
buildIncrementalRoleLinks(operation, ptype, rules);
}
}

}
81 changes: 78 additions & 3 deletions src/main/java/org/casbin/jcasbin/persist/WatcherEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,87 @@

import org.casbin.jcasbin.model.Model;

import java.util.List;

public interface WatcherEx extends Watcher {
void updateForAddPolicy(String... params);
/**
* updateForAddPolicy calls the update callback of other instances to synchronize their policy.
* It is called after a policy is added via Enforcer.addPolicy(), Enforcer.addNamedPolicy(),
* Enforcer.addGroupingPolicy() and Enforcer.addNamedGroupingPolicy().
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param params the policy
*/
void updateForAddPolicy(String sec, String ptype, String... params);

void updateForRemovePolicy(String... params);
/**
* updateForRemovePolicy calls the update callback of other instances to synchronize their policy.
* It is called after a policy is removed by Enforcer.removePolicy(), Enforcer.removeNamedPolicy(),
* Enforcer.removeGroupingPolicy() and Enforcer.removeNamedGroupingPolicy().
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param params the policy
*/
void updateForRemovePolicy(String sec, String ptype, String... params);

void updateForRemoveFilteredPolicy(int fieldIndex, String... fieldValues);
/**
* updateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy.
* It is called after Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(),
* Enforcer.RemoveFilteredGroupingPolicy() and Enforcer.RemoveFilteredNamedGroupingPolicy().
*
* @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.
*/
void updateForRemoveFilteredPolicy(String sec, String ptype, int fieldIndex, String... fieldValues);

/**
* updateForSavePolicy calls the update callback of other instances to synchronize their policy.
* It is called after Enforcer.savePolicy()
*
* @param model represents the whole access control model.
*/
void updateForSavePolicy(Model model);

/**
* updateForAddPolicies calls the update callback of other instances to synchronize their policy.
* It is called after Enforcer.addPolicies(), Enforcer.addNamedPolicies(),
* Enforcer.addGroupingPolicies() and Enforcer.addNamedGroupingPolicies().
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policies
*/
void updateForAddPolicies(String sec, String ptype, List<List<String>> rules);

/**
* updateForRemovePolicies calls the update callback of other instances to synchronize their policy.
* It is called after Enforcer.removePolicies(), Enforcer.removeNamedPolicies(),
* Enforcer.removeGroupingPolicies() and Enforcer.removeNamedGroupingPolicies().
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rules the policies
*/
void updateForRemovePolicies(String sec, String ptype, List<List<String>> rules);

/**
*
*/
enum UpdateType {
Update,
UpdateForAddPolicies,
UpdateForAddPolicy,
UpdateForRemoveFilteredPolicy,
UpdateForRemovePolicies,
UpdateForRemovePolicy,
UpdateForSavePolicy,
UpdateForUpdatePolicies,
UpdateForUpdatePolicy

}

}

0 comments on commit d9564ef

Please sign in to comment.