Skip to content

Commit

Permalink
Merge pull request #60 from Onlineberatung/develop
Browse files Browse the repository at this point in the history
fix: migration authentication logic
  • Loading branch information
tkuzynow authored May 14, 2024
2 parents e146121 + 17bf384 commit a6e74c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/vi/migrationtool/keycloak/KeycloakService.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ private UsersWithRole addCustomAttributeToUsersWithRole(
var httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);

authenticateInKeycloak(httpHeaders);

Optional<RoleRepresentation> role = getRoleBy(roleName, httpHeaders);
if (role.isEmpty()) {
log.error(PROVIDED_ROLE_DOESNT_EXISTS_IN_KEYCLOAK_MSG, roleName);
Expand All @@ -308,8 +310,7 @@ private UsersWithRole addCustomAttributeToUsersWithRole(
var pageNumber = 1;
var users = getUsersWithRoleName(roleName, getFirstElementIndex(pageNumber));
while (!users.isEmpty()) {
KeycloakLoginResponseDTO loginResponse = keycloakLoginService.loginAdminUser();
httpHeaders.setBearerAuth(loginResponse.getAccessToken());
authenticateInKeycloak(httpHeaders);
var migratedUsersPage =
addCustomAttributeToUsers(customAttribute, value, httpHeaders, restTemplate, users);
pageNumber++;
Expand All @@ -320,6 +321,11 @@ private UsersWithRole addCustomAttributeToUsersWithRole(
return new UsersWithRole(roleName, updatedUsers);
}

private void authenticateInKeycloak(HttpHeaders httpHeaders) {
KeycloakLoginResponseDTO loginResponse = keycloakLoginService.loginAdminUser();
httpHeaders.setBearerAuth(loginResponse.getAccessToken());
}

private List<String> addCustomAttributeToUsers(
String customAttribute,
Long value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public void execute(Database database) {
adviceSeekerTenant.getUserId(),
adviceSeekerTenant.getTenantId());

keycloakUserService.updateUserCustomAttribute(
"tenantId", adviceSeekerTenant.getTenantId(), adviceSeekerTenant.getUserId());

userServiceJdbcTemplate.update(
"UPDATE user SET tenant_id = ? WHERE user_id = ?",
adviceSeekerTenant.getTenantId(),
Expand All @@ -73,8 +76,6 @@ public void execute(Database database) {
adviceSeekerTenant.getTenantId(),
adviceSeekerTenant.getUserId());

keycloakUserService.updateUserCustomAttribute(
"tenantId", adviceSeekerTenant.getTenantId(), adviceSeekerTenant.getUserId());

log.info(
"Successfully set tenantId for {} with id {} to {}",
Expand All @@ -95,6 +96,9 @@ private void updateConsultantTenant(
consultantTenant.getUserId(),
consultantTenant.getTenantId());

keycloakUserService.updateUserCustomAttribute(
"tenantId", consultantTenant.getTenantId(), consultantTenant.getUserId());

userServiceJdbcTemplate.update(
"UPDATE consultant_agency SET tenant_id = ? WHERE consultant_id = ?",
consultantTenant.getTenantId(),
Expand All @@ -110,9 +114,6 @@ private void updateConsultantTenant(
consultantTenant.getTenantId(),
consultantTenant.getUserId());

keycloakUserService.updateUserCustomAttribute(
"tenantId", consultantTenant.getTenantId(), consultantTenant.getUserId());

log.info(
"Successfully set tenantId for {} with id {} to {}",
"consultant, consultant_agency, session and keycloak user",
Expand Down

0 comments on commit a6e74c3

Please sign in to comment.