private List searchUsers()

in services/src/main/java/org/apache/custos/service/federated/client/keycloak/KeycloakClient.java [1702:1742]


    private List<UserRepresentation> searchUsers(Keycloak client, String tenantId, String username,
                                                 String firstName, String lastName, String email, String search, int offset, int limit) {

        // Searching for users by username returns also partial matches, so need to filter down to an exact match if it exists
        List<UserRepresentation> userResourceList = null;
        if (search != null && !search.trim().equals("")) {
            userResourceList = client.realm(tenantId).users().search(search, offset, limit);
        } else {

            userResourceList = client.realm(tenantId).users().search(
                    username.toLowerCase(), firstName, lastName, email, offset, limit);
        }

        if (userResourceList != null && !userResourceList.isEmpty()) {
            List<UserRepresentation> newList = new ArrayList<>();
            for (UserRepresentation userRepresentation : userResourceList) {
                RoleMappingResource resource = client.realm(tenantId).users().get(userRepresentation.getId()).roles();
                MappingsRepresentation representation = resource.getAll();
                if (representation != null && representation.getRealmMappings() != null) {
                    List<String> roleRepresentations = new ArrayList<>();
                    representation.getRealmMappings().forEach(t -> roleRepresentations.add(t.getName()));
                    userRepresentation.setRealmRoles(roleRepresentations);
                }
                if (representation != null && representation.getClientMappings() != null) {
                    Map<String, List<String>> roleRepresentations = new HashMap<>();
                    representation.getClientMappings().keySet().forEach(key -> {
                        if (representation.getClientMappings().get(key).getMappings() != null) {
                            List<String> roleList = new ArrayList<>();
                            representation.getClientMappings().get(key).getMappings().forEach(t -> roleList.add(t.getName()));
                            roleRepresentations.put(key, roleList);
                        }
                    });
                    userRepresentation.setClientRoles(roleRepresentations);
                }
                newList.add(userRepresentation);
            }
            return userResourceList;
        }

        return userResourceList;
    }