/** * Converts a JSON representation of an access control entry to an {@link AccessControlEntry}. * * @param jsonAce * the json object * @return the access control entry */ private static AccessControlEntry getAce(JSONObject jsonAce) { String role = (String) jsonAce.get(ROLE); String action = (String) jsonAce.get(ACTION); Boolean allow = (Boolean) jsonAce.get(ALLOW); return new AccessControlEntry(role, action, allow); }
@Override public int compare(AccessControlEntry o1, AccessControlEntry o2) { // compare role int compareTo = StringUtils.trimToEmpty(o1.getRole()).compareTo(StringUtils.trimToEmpty(o2.getRole())); if (compareTo != 0) return compareTo; // compare action compareTo = StringUtils.trimToEmpty(o1.getAction()).compareTo(StringUtils.trimToEmpty(o2.getAction())); if (compareTo != 0) return compareTo; // compare allow return Boolean.valueOf(o1.isAllow()).compareTo(o2.isAllow()); } };
boolean foundAce = false; for (AccessControlEntry ace : acl.getEntries()) { if (ace.getAction().equalsIgnoreCase(action) && ace.getRole().equalsIgnoreCase(role)) { if (ace.isAllow() == allow) { newAcl.getEntries().add(new AccessControlEntry(role, action, allow)); newAcl.getEntries().add(new AccessControlEntry(role, action, allow));
/** * Reduces an access control list by an access control entry * * @param acl * the access control list to reduce * @param role * the role of the access control entry to remove * @param action * the action of the access control entry to remove * @return the reduced access control list or the same if already contained */ public static AccessControlList reduceAcl(AccessControlList acl, String role, String action) { AccessControlList newAcl = new AccessControlList(); for (AccessControlEntry ace : acl.getEntries()) { if (!ace.getAction().equalsIgnoreCase(action) || !ace.getRole().equalsIgnoreCase(role)) { newAcl.getEntries().add(ace); } } return newAcl; }
JSONObject singleJsonEntry = new JSONObject(); jsonAcl.put(ACE, singleJsonEntry); singleJsonEntry.put(ACTION, singleEntry.getAction()); singleJsonEntry.put(ROLE, singleEntry.getRole()); singleJsonEntry.put(ALLOW, singleEntry.isAllow()); break; default: for (AccessControlEntry entry : entries) { JSONObject jsonEntry = new JSONObject(); jsonEntry.put(ACTION, entry.getAction()); jsonEntry.put(ROLE, entry.getRole()); jsonEntry.put(ALLOW, entry.isAllow()); jsonEntryArray.add(jsonEntry);
/** Create a single access control entry. */ public static Either<AccessControlEntry, List<AccessControlEntry>> entry(String role, String action, boolean allow) { return left(new AccessControlEntry(role, action, allow)); }
@Override public AccessControlEntry apply(Tuple<String, Boolean> action) { return new AccessControlEntry(role, action.getA(), action.getB()); } }).value();