Skip to content

Commit

Permalink
feat: fix the addPolicy bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tx2002 committed Nov 26, 2024
1 parent 8dd4940 commit 183c8d7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/main/java/org/casbin/jcasbin/model/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

import static org.casbin.jcasbin.util.Util.splitCommaDelimited;

/**
* Policy represents the whole access control policy user defined.
Expand Down Expand Up @@ -187,6 +190,16 @@ public boolean addPolicy(String sec, String ptype, List<String> rule) {
List<List<String>> policy = assertion.policy;
int priorityIndex = assertion.priorityIndex;

String line = ptype + "," + String.join(",", rule);
if ("".equals(line)) {
return false;
}
if (line.charAt(0) == '#') {
return false;
}
String[] tokens = splitCommaDelimited(line);
List<String> linePolicy = Arrays.asList(Arrays.copyOfRange(tokens, 1, tokens.length));

// ensure the policies is ordered by priority value
if ("p".equals(sec) && priorityIndex >= 0) {
int value = Integer.parseInt(rule.get(priorityIndex));
Expand All @@ -200,13 +213,13 @@ public boolean addPolicy(String sec, String ptype, List<String> rule) {
right = mid;
}
}
policy.add(left, rule);
policy.add(left, linePolicy);
for (int i = left; i < assertion.policy.size(); ++i) {
assertion.policyIndex.put(assertion.policy.get(i).toString(), i);
}
} else {
policy.add(rule);
assertion.policyIndex.put(rule.toString(), policy.size() - 1);
policy.add(linePolicy);
assertion.policyIndex.put(linePolicy.toString(), policy.size() - 1);
}

return true;
Expand Down

0 comments on commit 183c8d7

Please sign in to comment.