use of org.apache.wiki.util.comparators.PrincipalComparator in project jspwiki by apache.
the class DefaultAclManager method printAcl.
/**
* Generates an ACL string for inclusion in a wiki page, based on a supplied Acl object. All of the permissions in this Acl are
* assumed to apply to the same page scope. The names of the pages are ignored; only the actions and principals matter.
*
* @param acl the ACL
* @return the ACL string
*/
protected static String printAcl(final Acl acl) {
// Extract the ACL entries into a Map with keys == permissions, values == principals
final Map<String, List<Principal>> permissionPrincipals = new TreeMap<>();
final Enumeration<AclEntry> entries = acl.aclEntries();
while (entries.hasMoreElements()) {
final AclEntry entry = entries.nextElement();
final Principal principal = entry.getPrincipal();
final Enumeration<Permission> permissions = entry.permissions();
while (permissions.hasMoreElements()) {
final Permission permission = permissions.nextElement();
List<Principal> principals = permissionPrincipals.get(permission.getActions());
if (principals == null) {
principals = new ArrayList<>();
final String action = permission.getActions();
if (action.indexOf(',') != -1) {
throw new IllegalStateException("AclEntry permission cannot have multiple targets.");
}
permissionPrincipals.put(action, principals);
}
principals.add(principal);
}
}
// Now, iterate through each permission in the map and generate an ACL string
final StringBuilder s = new StringBuilder();
for (final Map.Entry<String, List<Principal>> entry : permissionPrincipals.entrySet()) {
final String action = entry.getKey();
final List<Principal> principals = entry.getValue();
principals.sort(new PrincipalComparator());
s.append("[{ALLOW ").append(action).append(" ");
for (int i = 0; i < principals.size(); i++) {
final Principal principal = principals.get(i);
s.append(principal.getName());
if (i < (principals.size() - 1)) {
s.append(",");
}
}
s.append("}]\n");
}
return s.toString();
}
Aggregations