Skip to content

Commit

Permalink
Merge pull request #71 from CaritasDeutschland/hotfix-extended-loggin…
Browse files Browse the repository at this point in the history
…g-during-registration

Hotfix extended logging during registration
  • Loading branch information
mobo4b authored Dec 11, 2020
2 parents f90b6f5 + b79da5b commit 9a12454
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import de.caritas.cob.userservice.api.manager.consultingType.ConsultingTypeManager;
import de.caritas.cob.userservice.api.manager.consultingType.ConsultingTypeSettings;
import de.caritas.cob.userservice.api.model.CreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.model.rocketchat.login.LoginResponseDTO;
import de.caritas.cob.userservice.api.repository.session.ConsultingType;
import de.caritas.cob.userservice.api.repository.session.Session;
Expand All @@ -26,6 +26,7 @@
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientHelper;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -34,8 +35,8 @@
/**
* Facade to encapsulate the steps to initialize an user account (create chat/agency relation or a
* new session).
*
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class CreateUserFacade {
Expand All @@ -56,13 +57,17 @@ public class CreateUserFacade {
/**
* Creates a user in Keycloak and MariaDB. Then creates a session or chat account depending on the
* provided {@link ConsultingType}.
*
*
* @param user {@link UserDTO}
* @return {@link KeycloakCreateUserResponseDTO}
*
*/
public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDTO user) {

String lineSeparator = System.getProperty("line.separator");
StringBuilder logStringBuilder = new StringBuilder();
logStringBuilder.append("=== UserDTO ===" + lineSeparator);
logStringBuilder.append("object: " + user.toString());

KeycloakCreateUserResponseDTO response;
String userId;

Expand Down Expand Up @@ -94,6 +99,11 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT
return response;
}

logStringBuilder.append("userId " + userId + lineSeparator);
logStringBuilder.append("===============" + lineSeparator);

log.warn(logStringBuilder.toString());

// Update Keycloak account data and create user and session in MariaDB
updateAccountData(userId, user, consultingType);

Expand All @@ -103,9 +113,9 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT
/**
* Update the Keycloak account data (roles, password, e-mail address), create the user in MariaDB
* and initialize a session or chat relation (depending on {@link ConsultingType}).
*
* @param userId Keycloak user ID
* @param user {@link UserDTO} from registration form
*
* @param userId Keycloak user ID
* @param user {@link UserDTO} from registration form
* @param consultingType {@link ConsultingType}
*/
private void updateAccountData(String userId, UserDTO user, ConsultingType consultingType) {
Expand Down Expand Up @@ -146,9 +156,9 @@ private void updateAccountData(String userId, UserDTO user, ConsultingType consu
/**
* Initializes the provided {@link User} account depending on the consulting type. Consulting type
* KREUZBUND will get a chat/agency relation, all others will be provided with a session.
*
* @param user {@link UserDTO}
* @param dbUser {@link User}
*
* @param user {@link UserDTO}
* @param dbUser {@link User}
* @param consultingTypeSettings {@link ConsultingTypeSettings}
*/
private void initializeUserAccount(UserDTO user, User dbUser,
Expand All @@ -166,9 +176,9 @@ private void initializeUserAccount(UserDTO user, User dbUser,

/**
* Creates a new session for the provided {@link User}.
*
* @param user {@link UserDTO}
* @param dbUser {@link User}
*
* @param user {@link UserDTO}
* @param dbUser {@link User}
* @param consultingTypeSettings {@link ConsultingTypeSettings}
*/
private void createUserSession(UserDTO user, User dbUser,
Expand Down Expand Up @@ -244,17 +254,18 @@ private void createUserChatAgencyRelation(UserDTO user, User dbUser) {

} catch (InternalServerErrorException serviceException) {
rollBackUserAccount(dbUser.getUserId(), dbUser, null, userAgency);
throw new InternalServerErrorException("Could not create user-agency relation for Kreuzbund registration");
throw new InternalServerErrorException(
"Could not create user-agency relation for Kreuzbund registration");
}
}

/**
* Deletes the provided user in Keycloak and MariaDB and its related session or user <->
* chat/agency relations.
*
* @param userId Keycloak user ID
*
* @param userId Keycloak user ID
* @param session {@link Session}
* @param dbUser {@link User}
* @param dbUser {@link User}
*/
private void rollBackUserAccount(String userId, User dbUser, Session session,
UserAgency userAgency) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package de.caritas.cob.userservice.api.service.helper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.userservice.api.authorization.Authority;
import de.caritas.cob.userservice.api.authorization.UserRole;
import de.caritas.cob.userservice.api.exception.keycloak.KeycloakException;
import de.caritas.cob.userservice.api.helper.UserHelper;
import de.caritas.cob.userservice.api.model.CreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.service.LogService;
import de.caritas.cob.userservice.api.service.helper.aspect.KeycloakAdminClientLogout;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Response;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
Expand All @@ -18,16 +30,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import de.caritas.cob.userservice.api.authorization.Authority;
import de.caritas.cob.userservice.api.authorization.UserRole;
import de.caritas.cob.userservice.api.exception.keycloak.KeycloakException;
import de.caritas.cob.userservice.api.helper.UserHelper;
import de.caritas.cob.userservice.api.model.CreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO;
import de.caritas.cob.userservice.api.service.LogService;
import de.caritas.cob.userservice.api.service.helper.aspect.KeycloakAdminClientLogout;
import lombok.extern.slf4j.Slf4j;

/**
* Helper class for the KeycloakService. Communicates to the Keycloak Admin API over the Keycloak
Expand All @@ -37,6 +39,7 @@
@Slf4j
@Service
public class KeycloakAdminClientHelper {

@Value("${keycloak.auth-server-url}")
private String KEYCLOAK_SERVER_URL;

Expand Down Expand Up @@ -105,9 +108,9 @@ public KeycloakCreateUserResponseDTO createKeycloakUser(final UserDTO user) thro
/**
* Creates a user with firstname and lastname in Keycloak and returns its Keycloak user ID.
*
* @param user {@link UserDTO}
* @param user {@link UserDTO}
* @param firstName first name of user
* @param lastName last name of user
* @param lastName last name of user
* @return {@link KeycloakCreateUserResponseDTO}
*/
@KeycloakAdminClientLogout
Expand Down Expand Up @@ -225,20 +228,50 @@ public void updateUserRole(final String userId) throws Exception {
/**
* Assigns the role with the given name to the given user ID.
*
* @param userId Keycloak user ID
* @param userId Keycloak user ID
* @param roleName Keycloak role name
*/
@KeycloakAdminClientLogout
public void updateRole(final String userId, final String roleName) {
// Get realm and user resources
RealmResource realmResource = getInstance().realm(KEYCLOAK_REALM);

// Assign role
ObjectMapper objectMapper = new ObjectMapper();
RoleRepresentation roleRepresentation = realmResource.roles().get(roleName).toRepresentation();
String lineSeparator = System.getProperty("line.separator");
StringBuilder logStringBuilder = new StringBuilder();
logStringBuilder.append("=== roleRepresentation ===").append(lineSeparator);
try {
logStringBuilder.append("object: ")
.append("user ID: ")
.append(userId)
.append(objectMapper.writeValueAsString(roleRepresentation))
.append(lineSeparator);
} catch (JsonProcessingException e) {
log.warn("Could not parse object of role representation");
}
logStringBuilder.append("==========================");

UsersResource userRessource = realmResource.users();
UserResource user = userRessource.get(userId);

logStringBuilder.append("=== userRessource ===").append(lineSeparator);
try {
logStringBuilder.append("object: ")
.append(objectMapper.writeValueAsString(user.toRepresentation()))
.append(lineSeparator);
} catch (JsonProcessingException e) {
log.warn("Could not parse object of user resource");
}
logStringBuilder.append("==========================");

log.warn(logStringBuilder.toString());

boolean isRoleUpdated = false;

// Assign role
user.roles().realmLevel()
.add(Arrays.asList(realmResource.roles().get(roleName).toRepresentation()));
.add(Arrays.asList(roleRepresentation));

// Check if role has been assigned successfully
List<RoleRepresentation> userRoles = user.roles().realmLevel().listAll();
Expand All @@ -257,7 +290,7 @@ public void updateRole(final String userId, final String roleName) {
/**
* Updates the Keycloak password for a user.
*
* @param userId Keycloak user ID
* @param userId Keycloak user ID
* @param password user password
*/
@KeycloakAdminClientLogout
Expand All @@ -274,7 +307,7 @@ public void updatePassword(final String userId, final String password) throws Ex
* success/error status possible, because the Keycloak Client doesn't provide one either. *
*
* @param userId Keycloak user ID
* @param user {@link UserDTO}
* @param user {@link UserDTO}
* @return the (dummy) email address
* @throws Exception {@link Exception}
*/
Expand Down Expand Up @@ -385,4 +418,4 @@ public void closeInstance() {
// this.keycloakInstance = null;
// }
}
}
}

0 comments on commit 9a12454

Please sign in to comment.