@Test void testGetReachableGrantedAuthoritiesSu() { when(su.getName()).thenReturn("SU"); when(su.getIncludes()) .thenReturn(ImmutableList.of(aclTakeOwnership, aclModifyAuditing, aclGeneralChanges)); when(aclTakeOwnership.getName()).thenReturn("ACL_TAKE_OWNERSHIP"); when(aclModifyAuditing.getName()).thenReturn("ACL_MODIFY_AUDITING"); when(aclGeneralChanges.getName()).thenReturn("ACL_GENERAL_CHANGES"); assertEquals( of( new SimpleGrantedAuthority("ROLE_ACL_TAKE_OWNERSHIP"), new SimpleGrantedAuthority("ROLE_ACL_MODIFY_AUDITING"), new SimpleGrantedAuthority("ROLE_ACL_GENERAL_CHANGES"), new SimpleGrantedAuthority("ROLE_SU")), molgenisRoleHierarchy.getReachableGrantedAuthorities( singletonList(new SimpleGrantedAuthority("ROLE_SU")))); }
@Override public Collection<? extends GrantedAuthority> getReachableGrantedAuthorities( Collection<? extends GrantedAuthority> authorities) { return runAsSystem( () -> { if (authorities == null || authorities.isEmpty()) { return AuthorityUtils.NO_AUTHORITIES; } Set<String> roleNames = authorities.stream() .map(GrantedAuthority::getAuthority) .filter(name -> name.startsWith(ROLE_PREFIX)) .map(name -> name.substring(ROLE_PREFIX.length())) .collect(toSet()); Multimap<String, String> allRoleInclusions = getAllRoleInclusions(); Set<String> newlyDiscovered = roleNames; while (!newlyDiscovered.isEmpty()) { Set<String> included = newlyDiscovered.stream() .flatMap(role -> allRoleInclusions.get(role).stream()) .collect(toSet()); newlyDiscovered = copyOf(difference(included, roleNames)); roleNames.addAll(newlyDiscovered); } Set<SimpleGrantedAuthority> reachableRoles = roleNames.stream() .map(SidUtils::createRoleAuthority) .map(SimpleGrantedAuthority::new) .collect(toSet()); LOG.debug( "getReachableGrantedAuthorities() - From the roles {} one can reach {} in zero or more steps.", authorities, reachableRoles); return reachableRoles; }); }
DataserviceRoleHierarchy implements RoleHierarchy { @Override public Collection<? extends GrantedAuthority> getReachableGrantedAuthorities( Collection<? extends GrantedAuthority> authorities) { return runAsSystem( () -> { if (authorities == null || authorities.isEmpty()) { return AuthorityUtils.NO_AUTHORITIES; } Set<String> roleNames = authorities.stream() .map(GrantedAuthority::getAuthority) .filter(name -> name.startsWith(ROLE_PREFIX)) .map(name -> name.substring(ROLE_PREFIX.length())) .collect(toSet()); Multimap<String, String> allRoleInclusions = getAllRoleInclusions(); Set<String> newlyDiscovered = roleNames; while (!newlyDiscovered.isEmpty()) { Set<String> included = newlyDiscovered.stream() .flatMap(role -> allRoleInclusions.get(role).stream()) .collect(toSet()); newlyDiscovered = copyOf(difference(included, roleNames)); roleNames.addAll(newlyDiscovered); } Set<SimpleGrantedAuthority> reachableRoles = roleNames.stream() .map(SidUtils::createRoleAuthority) .map(SimpleGrantedAuthority::new) .collect(toSet()); LOG.debug( "getReachableGrantedAuthorities() - From the roles {} one can reach {} in zero or more steps.", authorities, reachableRoles); return reachableRoles; }); } }
DataserviceRoleHierarchy implements RoleHierarchy { @Override public Collection<? extends GrantedAuthority> getReachableGrantedAuthorities( Collection<? extends GrantedAuthority> authorities) { return runAsSystem( () -> { if (authorities == null || authorities.isEmpty()) { return AuthorityUtils.NO_AUTHORITIES; } Set<String> roleNames = authorities.stream() .map(GrantedAuthority::getAuthority) .filter(name -> name.startsWith(ROLE_PREFIX)) .map(name -> name.substring(ROLE_PREFIX.length())) .collect(toSet()); Multimap<String, String> allRoleInclusions = getAllRoleInclusions(); Set<String> newlyDiscovered = roleNames; while (!newlyDiscovered.isEmpty()) { Set<String> included = newlyDiscovered.stream() .flatMap(role -> allRoleInclusions.get(role).stream()) .collect(toSet()); newlyDiscovered = copyOf(difference(included, roleNames)); roleNames.addAll(newlyDiscovered); } Set<SimpleGrantedAuthority> reachableRoles = roleNames.stream() .map(SidUtils::createRoleAuthority) .map(SimpleGrantedAuthority::new) .collect(toSet()); LOG.debug( "getReachableGrantedAuthorities() - From the roles {} one can reach {} in zero or more steps.", authorities, reachableRoles); return reachableRoles; }); } DataserviceRoleHierarchy(DataService dataService); }
DataserviceRoleHierarchy implements RoleHierarchy { @Override public Collection<? extends GrantedAuthority> getReachableGrantedAuthorities( Collection<? extends GrantedAuthority> authorities) { return runAsSystem( () -> { if (authorities == null || authorities.isEmpty()) { return AuthorityUtils.NO_AUTHORITIES; } Set<String> roleNames = authorities.stream() .map(GrantedAuthority::getAuthority) .filter(name -> name.startsWith(ROLE_PREFIX)) .map(name -> name.substring(ROLE_PREFIX.length())) .collect(toSet()); Multimap<String, String> allRoleInclusions = getAllRoleInclusions(); Set<String> newlyDiscovered = roleNames; while (!newlyDiscovered.isEmpty()) { Set<String> included = newlyDiscovered.stream() .flatMap(role -> allRoleInclusions.get(role).stream()) .collect(toSet()); newlyDiscovered = copyOf(difference(included, roleNames)); roleNames.addAll(newlyDiscovered); } Set<SimpleGrantedAuthority> reachableRoles = roleNames.stream() .map(SidUtils::createRoleAuthority) .map(SimpleGrantedAuthority::new) .collect(toSet()); LOG.debug( "getReachableGrantedAuthorities() - From the roles {} one can reach {} in zero or more steps.", authorities, reachableRoles); return reachableRoles; }); } DataserviceRoleHierarchy(DataService dataService); @Override Collection<? extends GrantedAuthority> getReachableGrantedAuthorities(
      Collection<? extends GrantedAuthority> authorities); ImmutableMap<GrantedAuthority, ImmutableSet<GrantedAuthority>> getAllGrantedAuthorityInclusions(); }
DataserviceRoleHierarchy implements RoleHierarchy { @Override public Collection<? extends GrantedAuthority> getReachableGrantedAuthorities( Collection<? extends GrantedAuthority> authorities) { return runAsSystem( () -> { if (authorities == null || authorities.isEmpty()) { return AuthorityUtils.NO_AUTHORITIES; } Set<String> roleNames = authorities.stream() .map(GrantedAuthority::getAuthority) .filter(name -> name.startsWith(ROLE_PREFIX)) .map(name -> name.substring(ROLE_PREFIX.length())) .collect(toSet()); Multimap<String, String> allRoleInclusions = getAllRoleInclusions(); Set<String> newlyDiscovered = roleNames; while (!newlyDiscovered.isEmpty()) { Set<String> included = newlyDiscovered.stream() .flatMap(role -> allRoleInclusions.get(role).stream()) .collect(toSet()); newlyDiscovered = copyOf(difference(included, roleNames)); roleNames.addAll(newlyDiscovered); } Set<SimpleGrantedAuthority> reachableRoles = roleNames.stream() .map(SidUtils::createRoleAuthority) .map(SimpleGrantedAuthority::new) .collect(toSet()); LOG.debug( "getReachableGrantedAuthorities() - From the roles {} one can reach {} in zero or more steps.", authorities, reachableRoles); return reachableRoles; }); } DataserviceRoleHierarchy(DataService dataService); @Override Collection<? extends GrantedAuthority> getReachableGrantedAuthorities(
      Collection<? extends GrantedAuthority> authorities); ImmutableMap<GrantedAuthority, ImmutableSet<GrantedAuthority>> getAllGrantedAuthorityInclusions();  }