All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openl.security.acl.RoleHierarchyImpl Maven / Gradle / Ivy

The newest version!
package org.openl.security.acl;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;

import org.openl.rules.security.Group;

public class RoleHierarchyImpl implements RoleHierarchy {

    @Override
    public Collection getReachableGrantedAuthorities(
            Collection authorities) {
        if (authorities == null || authorities.isEmpty()) {
            return AuthorityUtils.NO_AUTHORITIES;
        }
        Collection reachableGrantedAuthorities = new HashSet<>();
        Queue queue = new LinkedList<>(authorities);
        Set p = authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
        while (!queue.isEmpty()) {
            GrantedAuthority ga = queue.poll();
            if (ga instanceof Group) {
                Group group = (Group) ga;
                if (group.getPrivileges() != null) {
                    for (GrantedAuthority g : group.getPrivileges()) {
                        if (!p.contains(g.getAuthority())) {
                            queue.add(g);
                            p.add(g.getAuthority());
                        }
                    }
                }
            }
            reachableGrantedAuthorities.add(ga);
        }
        return reachableGrantedAuthorities;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy