Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Refactoring method casbinJsGetPermissionForUser
Browse files Browse the repository at this point in the history
JackYifan committed Jun 15, 2024
1 parent 844cc65 commit e15299e
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/main/java/org/casbin/jcasbin/main/Frontend.java
Original file line number Diff line number Diff line change
@@ -27,26 +27,20 @@ public static String casbinJsGetPermissionForUser(Enforcer e, String user) {
Model model = e.getModel();
Map<String, Object> m = new HashMap<>();
m.put("m", model.saveModelToText().trim());
List<List<String>> policies = new ArrayList<>();
for (String ptype : model.model.get("p").keySet()) {
List<List<String>> policy = model.getPolicy("p", ptype);
for (List<String> p : policy) {
List<String> tmp = new ArrayList<>(p);
tmp.add(0, ptype);
policies.add(tmp);
}
}
m.put("p", policies);
policies = new ArrayList<>();
for (String ptype : model.model.get("g").keySet()) {
List<List<String>> policy = model.getPolicy("g", ptype);
for (List<String> p : policy) {
List<String> tmp = new ArrayList<>(p);
tmp.add(0, ptype);
policies.add(tmp);
}
}
m.put("g", policies);
m.put("p", getPolicyBySection(model,"p"));
m.put("g", getPolicyBySection(model,"g"));
return new Gson().toJson(m);
}
private static List<List<String>> getPolicyBySection(Model model, String section) {
List<List<String>> policies = new ArrayList<>();
for (String ptype : model.model.get(section).keySet()) {
List<List<String>> policy = model.getPolicy(section, ptype);
for (List<String> p : policy) {
List<String> tmp = new ArrayList<>(p);
tmp.add(0, ptype);
policies.add(tmp);
}
}
return policies;
}
}

0 comments on commit e15299e

Please sign in to comment.