Search in sources :

Example 1 with UserResource

use of org.keycloak.admin.client.resource.UserResource in project openremote by openremote.

the class KeycloakCleanSetup method onStart.

@Override
public void onStart() throws Exception {
    super.onStart();
    // Delete all realms that are not the master realm
    LOG.info("Deleting all non-master realms");
    RealmsResource realmsResource = keycloakProvider.getRealms(accessToken);
    List<RealmRepresentation> realms = realmsResource.findAll();
    for (RealmRepresentation realmRepresentation : realms) {
        if (!realmRepresentation.getRealm().equals(MASTER_REALM)) {
            keycloakProvider.getRealms(accessToken).realm(realmRepresentation.getRealm()).remove();
        }
    }
    // Find out if there is a client already present for this application, if so, delete it
    masterClientsResource.findAll().stream().filter(clientRepresentation -> clientRepresentation.getClientId().equals(KEYCLOAK_CLIENT_ID)).map(ClientRepresentation::getId).forEach(clientObjectId -> {
        LOG.info("Deleting client: " + clientObjectId);
        masterClientsResource.get(clientObjectId).remove();
    });
    // Find out if there are any users except the admin, delete them
    masterUsersResource.search(null, null, null).stream().filter(userRepresentation -> !userRepresentation.getUsername().equals(MASTER_REALM_ADMIN_USER)).map(userRepresentation -> {
        LOG.info("Deleting user: " + userRepresentation);
        return masterUsersResource.get(userRepresentation.getId());
    }).forEach(UserResource::remove);
}
Also used : ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Container(org.openremote.container.Container) List(java.util.List) RealmsResource(org.keycloak.admin.client.resource.RealmsResource) AbstractKeycloakSetup(org.openremote.manager.setup.AbstractKeycloakSetup) UserResource(org.keycloak.admin.client.resource.UserResource) Logger(java.util.logging.Logger) Constants(org.openremote.model.Constants) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) RealmsResource(org.keycloak.admin.client.resource.RealmsResource) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource)

Aggregations

List (java.util.List)1 Logger (java.util.logging.Logger)1 RealmsResource (org.keycloak.admin.client.resource.RealmsResource)1 UserResource (org.keycloak.admin.client.resource.UserResource)1 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)1 Container (org.openremote.container.Container)1 AbstractKeycloakSetup (org.openremote.manager.setup.AbstractKeycloakSetup)1 Constants (org.openremote.model.Constants)1