@Test void testGetRolesForSid() { User molgenisUser = mock(User.class); when(molgenisUser.getId()).thenReturn("user"); when(userService.getUser("user")).thenReturn(molgenisUser); PrincipalSid user = mock(PrincipalSid.class); when(user.getPrincipal()).thenReturn("user"); Role role1 = mock(Role.class); when(role1.getName()).thenReturn("role1"); Role role2 = mock(Role.class); when(role2.getName()).thenReturn("role2"); RoleMembership roleMembership1 = mock(RoleMembership.class); RoleMembership roleMembership2 = mock(RoleMembership.class); when(roleMembership1.getRole()).thenReturn(role1); when(roleMembership2.getRole()).thenReturn(role2); Repository userRepository = mock(Repository.class); Query userQuery = mock(Query.class); when(userQuery.eq(RoleMembershipMetadata.USER, "user")).thenReturn(userQuery); when(userQuery.findAll()).thenReturn(Stream.of(roleMembership1, roleMembership2)); when(userRepository.query()).thenReturn(userQuery); doReturn(userRepository) .when(dataService) .getRepository(RoleMembershipMetadata.ROLE_MEMBERSHIP, RoleMembership.class); doReturn(true) .when(userPermissionEvaluator) .hasPermission( new EntityTypeIdentity(RoleMembershipMetadata.ROLE_MEMBERSHIP), EntityTypePermission.READ_DATA); List<Sid> expected = Arrays.asList(new GrantedAuthoritySid("ROLE_role1"), new GrantedAuthoritySid("ROLE_role2")); UserRoleTools userRoleTools = new UserRoleTools(userService, dataService, userPermissionEvaluator); assertEquals(expected, userRoleTools.getRolesForSid(user)); }
public List<Sid> getRolesForSid(Sid sid) { List<Sid> roles = new ArrayList<>(); if (sid instanceof PrincipalSid) { roles = getRolesForUser(sid); } else if (sid instanceof GrantedAuthoritySid) { String role = ((GrantedAuthoritySid) sid).getGrantedAuthority(); roles = getParentRoles(getRoleName(role)); } return roles; }
UserRoleTools { public List<Sid> getRolesForSid(Sid sid) { List<Sid> roles = new ArrayList<>(); if (sid instanceof PrincipalSid) { roles = getRolesForUser(sid); } else if (sid instanceof GrantedAuthoritySid) { String role = ((GrantedAuthoritySid) sid).getGrantedAuthority(); roles = getParentRoles(getRoleName(role)); } return roles; } }
UserRoleTools { public List<Sid> getRolesForSid(Sid sid) { List<Sid> roles = new ArrayList<>(); if (sid instanceof PrincipalSid) { roles = getRolesForUser(sid); } else if (sid instanceof GrantedAuthoritySid) { String role = ((GrantedAuthoritySid) sid).getGrantedAuthority(); roles = getParentRoles(getRoleName(role)); } return roles; } UserRoleTools(
      UserService userService,
      DataService dataService,
      UserPermissionEvaluator userPermissionEvaluator); }
UserRoleTools { public List<Sid> getRolesForSid(Sid sid) { List<Sid> roles = new ArrayList<>(); if (sid instanceof PrincipalSid) { roles = getRolesForUser(sid); } else if (sid instanceof GrantedAuthoritySid) { String role = ((GrantedAuthoritySid) sid).getGrantedAuthority(); roles = getParentRoles(getRoleName(role)); } return roles; } UserRoleTools(
      UserService userService,
      DataService dataService,
      UserPermissionEvaluator userPermissionEvaluator); void checkRoleExists(String role); void checkUserExists(String user); List<Sid> getSids(List<String> users, List<String> roles); static Optional<String> getUsername(Sid sid); static Optional<String> getRolename(Sid sid); static String getName(Sid sid); List<Sid> getRolesForSid(Sid sid); }
UserRoleTools { public List<Sid> getRolesForSid(Sid sid) { List<Sid> roles = new ArrayList<>(); if (sid instanceof PrincipalSid) { roles = getRolesForUser(sid); } else if (sid instanceof GrantedAuthoritySid) { String role = ((GrantedAuthoritySid) sid).getGrantedAuthority(); roles = getParentRoles(getRoleName(role)); } return roles; } UserRoleTools(
      UserService userService,
      DataService dataService,
      UserPermissionEvaluator userPermissionEvaluator); void checkRoleExists(String role); void checkUserExists(String user); List<Sid> getSids(List<String> users, List<String> roles); static Optional<String> getUsername(Sid sid); static Optional<String> getRolename(Sid sid); static String getName(Sid sid); List<Sid> getRolesForSid(Sid sid); static final String ANONYMOUS; }