PersonaServiceImpl implements PersonaService { @Override public Set<Role> getEffectiveRolesForResource(Persona persona, Resource resource) { if (null == persona) { throw new IllegalArgumentException("Missing persona (null)."); } if (null == resource) { throw new IllegalArgumentException("Missing resource (null)."); } logger.determiningEffectiveRolesForPersonaOnResource(persona.getId(), resource.getId()); List<PersonaResourceRole> results = personaResourceRoleService.getByPersonaAndResource(persona, resource); logger.numOfDirectRolesOnResource(persona.getId(), resource.getId(), results.size()); if (results.size() == 0) { logger.noDirectRolesOnResource(persona.getId(), resource.getId()); List<Organization> organizations = organizationService.getOrganizationsForPersona(persona); Set<Role> roles = new HashSet<>(); for (Organization organization : organizations) { logger.checkingIndirectRolesViaOrganization(persona.getId(), resource.getId(), organization.getId()); List<OrganizationMembership> memberships = membershipService.getPersonaMembershipsForOrganization(persona, organization); Set<Role> organizationRolesForResource = getEffectiveRolesForResource(organization, resource); Set<Role> effectiveRoles = memberships .stream() .map(membership -> { Set<Role> implicitRoles = roleService.getImplicitUserRoles(membership.getRole()); implicitRoles.add(membership.getRole()); return implicitRoles; } ) .flatMap(Collection::stream) .filter(organizationRolesForResource::contains) .collect(Collectors.toSet()); logger.numOfEffectiveRolesViaOrganization( persona.getId(), resource.getId(), organization.getId(), effectiveRoles.size() ); roles.addAll(effectiveRoles); } logger.totalEffectiveRolesOnResource(persona.getId(), resource.getId(), roles.size()); return roles; } Set<Role> roles = new HashSet<>(results.size()); roles.addAll(results.stream().map(PersonaResourceRole::getRole).collect(Collectors.toSet())); roles.addAll( results .stream() .map(r -> roleService.getImplicitUserRoles(r.getRole())) .flatMap(Collection::stream) .collect(Collectors.toSet()) ); logger.totalEffectiveRolesOnResourceWithImplicitRoles(persona.getId(), resource.getId(), roles.size()); return roles; }  Persona getById(UUID id); Persona get(String id); @Override Set<Role> getEffectiveRolesForResource(Persona persona, Resource resource); @Override @Produces Persona getCurrent(); @Override boolean isAllowedToImpersonate(HawkularUser actual, Persona toImpersonate);  }
@Test public void organizationTreePutsUserWithTwoRolesInTwoOrganizations() { HawkularUser jdoe = userService.getOrCreateById(UUID.randomUUID().toString()); Organization org1 = organizationService.createOrganization(UUID.randomUUID().toString(), "", jdoe); Organization org1A = organizationService.createOrganization(UUID.randomUUID().toString(), "", org1); Organization org1B = organizationService.createOrganization(UUID.randomUUID().toString(), "", org1); Resource resource = resourceService.create(UUID.randomUUID().toString(), org1); Set<Role> rolesForResource = personaService.getEffectiveRolesForResource(jdoe, resource); assertEquals("jdoe should have all permissions on resource", 7, rolesForResource.size()); boolean adminFound = false; boolean superUserFound = false; for (Role role : rolesForResource) { if (role.getName().equals("SuperUser")) { superUserFound = true; } if (role.getName().equals("Administrator")) { adminFound = true; } } assertTrue("User should have had super user roles", superUserFound); assertTrue("User should have had admin roles", adminFound); }