public GroupRepresentation updateGroup()

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


    public GroupRepresentation updateGroup(String realmId, String clientId, String clientSec, GroupRepresentation groupRepresentation) {
        try (Keycloak client = getClient(iamServerURL, superAdminRealmID, superAdminUserName, superAdminPassword)) {
            client.realm(realmId).groups().group(groupRepresentation.getId()).update(groupRepresentation);

            List<RoleRepresentation> exRoles = client.realm(realmId).groups().group(groupRepresentation.getId()).roles().realmLevel().listAll();

            if (exRoles != null && !exRoles.isEmpty()) {
                client.realm(realmId).groups().group(groupRepresentation.getId()).roles().realmLevel().remove(exRoles);
            }

            if (groupRepresentation.getRealmRoles() != null && !groupRepresentation.getRealmRoles().isEmpty()) {
                List<RoleRepresentation> roleRepresentation = new ArrayList<>();
                for (String role : groupRepresentation.getRealmRoles()) {
                    RoleResource resource = client.realm(realmId).roles().get(role);
                    if (resource != null) {
                        roleRepresentation.add(resource.toRepresentation());
                    }
                }
                if (!roleRepresentation.isEmpty()) {
                    client.realm(realmId).groups().group(groupRepresentation.getId()).roles().realmLevel().add(roleRepresentation);
                }

            }

            ClientRepresentation clientRepresentation = client.realm(realmId).clients().findByClientId(clientId).get(0);

            List<RoleRepresentation> exClientRoles = client.realm(realmId).groups().group(groupRepresentation.getId())
                    .roles().clientLevel(clientRepresentation.getId()).listAll();

            if (exClientRoles != null && !exClientRoles.isEmpty()) {
                client.realm(realmId).groups().group(groupRepresentation.getId())
                        .roles().clientLevel(clientRepresentation.getId()).remove(exClientRoles);
            }

            if (groupRepresentation.getClientRoles() != null && !groupRepresentation.getClientRoles().isEmpty()) {
                List<RoleRepresentation> clientRepresentations = new ArrayList<>();

                for (String role : groupRepresentation.getClientRoles().get(clientId)) {
                    RoleResource resource = client.realm(realmId).clients().get(clientRepresentation.getId()).roles().get(role);

                    if (resource != null) {
                        clientRepresentations.add(resource.toRepresentation());
                    }
                }
                if (!clientRepresentations.isEmpty()) {
                    client.realm(realmId).groups().group(groupRepresentation.getId()).roles().
                            clientLevel(clientRepresentation.getId()).add(clientRepresentations);
                }

            }

            return client.realm(realmId).groups().group(groupRepresentation.getId()).toRepresentation();

        } catch (Exception ex) {
            String msg = "Error occurred while updating group, reason: " + ex.getMessage();
            LOGGER.error(msg, ex);
            throw new RuntimeException(msg, ex);
        }
    }