Skip to content

Commit

Permalink
Merge pull request #30 from Onlineberatung/OB-6773-add-migration-to-m…
Browse files Browse the repository at this point in the history
…igrate-tenant-ids

fix: previous migration to add role names
  • Loading branch information
tkuzynow authored Oct 11, 2023
2 parents 78d27d0 + 703162b commit cf6bdb6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class KeycloakAddRolesToUsersWithRoleTask extends MigrationTasks {
public void execute(Database database) {
if (roleNames != null) {
KeycloakService keycloakService = BeanAwareSpringLiquibase.getBean(KeycloakService.class);
keycloakService.addRolesToUsersWithRoleName(roleNameToSearchForUsers, List.of(roleNames.split(SPLIT_CHAR)));
keycloakService.addRolesToUsersWithRoleName(
roleNameToSearchForUsers, List.of(roleNames.split(SPLIT_CHAR)));
}
}
}
12 changes: 5 additions & 7 deletions src/main/java/com/vi/migrationtool/keycloak/KeycloakService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class KeycloakService {
private static final String SEARCH_PARAM = "search";
private static final String MAX_USERS_TO_MIGRATE = "500";
private static final String ADMIN_REALMS = "/admin/realms/";
private static final String PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG = "The provided role {} doesn't exists in keycloak, please create it first";
private static final String PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG =
"The provided role {} doesn't exists in keycloak, please create it first";
private final KeycloakConfig keycloakConfig;

public void createRole(String roleName) {
Expand Down Expand Up @@ -77,8 +78,7 @@ public void addRoleToUsers(final List<String> usernames, final String roleName)

Optional<RoleRepresentation> role = getRoleBy(roleName, httpHeaders);
if (role.isEmpty()) {
log.error(
PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleName);
log.error(PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleName);
}

var restTemplate = new RestTemplate();
Expand Down Expand Up @@ -113,8 +113,7 @@ private void tryToAddRoleToKeycloakUser(
RestTemplate restTemplate) {
Optional<RoleRepresentation> role = getRoleBy(roleNameDoAdd, httpHeaders);
if (role.isEmpty()) {
log.error(
PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleNameDoAdd);
log.error(PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleNameDoAdd);
}
keycloakUsers.forEach(
user -> callKeycloakToAddRoleToUser(role.get(), httpHeaders, restTemplate, user));
Expand Down Expand Up @@ -314,8 +313,7 @@ private UsersWithRole addCustomAttributeToUsersWithRole(

Optional<RoleRepresentation> role = getRoleBy(roleName, httpHeaders);
if (role.isEmpty()) {
log.error(
PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleName);
log.error(PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleName);
}

var restTemplate = new RestTemplate();
Expand Down

0 comments on commit cf6bdb6

Please sign in to comment.