Search in sources :

Example 1 with PrincipalComparator

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();
}
Also used : AclEntry(org.apache.wiki.api.core.AclEntry) TreeMap(java.util.TreeMap) PrincipalComparator(org.apache.wiki.util.comparators.PrincipalComparator) PagePermission(org.apache.wiki.auth.permissions.PagePermission) Permission(java.security.Permission) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap) Principal(java.security.Principal)

Aggregations

Permission (java.security.Permission)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AclEntry (org.apache.wiki.api.core.AclEntry)1 PagePermission (org.apache.wiki.auth.permissions.PagePermission)1 PrincipalComparator (org.apache.wiki.util.comparators.PrincipalComparator)1