Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

import java.util.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.unistuttgart.iste.meitrex.gamification_service.aspects.logging.Loggable;
import de.unistuttgart.iste.meitrex.gamification_service.aspects.resiliency.Retryable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.*;
import org.springframework.web.reactive.function.*;
Expand All @@ -14,6 +17,7 @@


@Component
@Slf4j
public class DefaultKeycloakClient implements IKeycloakClient {

private static Map<String, List<String>> extractAttributeMap(JsonNode node) {
Expand Down Expand Up @@ -45,17 +49,21 @@ private static Map<String, List<String>> extractAttributeMap(JsonNode node) {

private final WebClient webClient;

private final ObjectMapper objectMapper;


public DefaultKeycloakClient(
@Value("${keycloak.realm}") String realm,
@Value("${keycloak.clientId}") String clientId,
@Value("${keycloak.clientSecret}") String clientSecret,
@Autowired @Qualifier("keycloakServiceClient") WebClient webClient
@Autowired @Qualifier("keycloakServiceClient") WebClient webClient,
@Autowired ObjectMapper objectMapper
) {
this.realm = Objects.requireNonNull(realm);
this.clientId = Objects.requireNonNull(clientId);
this.clientSecret = Objects.requireNonNull(clientSecret);
this.webClient = Objects.requireNonNull(webClient);
this.objectMapper = Objects.requireNonNull(objectMapper);
}

@Override
Expand Down Expand Up @@ -110,13 +118,26 @@ private void removeValues(UUID userId, String attrName) {

private void saveAttributeMap(UUID userId, Map<String, List<String>> valueMap) {
final String token = getAccessToken();
Map<String, Object> payload = new HashMap<>();
payload.put("attributes", valueMap);
JsonNode userNode = webClient.get()
.uri("/admin/realms/" + this.realm + "/users/" + userId)
.headers(headers -> headers.setBearerAuth(token))
.retrieve()
.bodyToMono(JsonNode.class)
.block();

if (userNode == null || !userNode.isObject()) {
throw new IllegalStateException("User not found or invalid json for id=" + userId);
}

ObjectNode fullUser = (ObjectNode) userNode;
JsonNode attributesNode = objectMapper.valueToTree(valueMap);
fullUser.set("attributes", attributesNode);

webClient.put()
.uri("/admin/realms/" + realm + "/users/{id}", userId)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(payload)
.bodyValue(fullUser)
.retrieve()
.toBodilessEntity()
.block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.UserEntity;
import de.unistuttgart.iste.meitrex.gamification_service.persistence.repository.IPlayerHexadScoreQuestionRepository;
import de.unistuttgart.iste.meitrex.gamification_service.service.internal.IUserCreator;
import io.dapr.client.DaprClient;
import org.springframework.stereotype.Service;

import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.PlayerHexadScoreEntity;
Expand Down Expand Up @@ -90,7 +91,6 @@ public PlayerHexadScore calculateDefault(UUID userId, boolean notification) {
final String message = "You skipped the questionnaire so we set a default type. Tap to resume.";
final String link = "/?resumeSurvey=1";

final ServerSource source = ServerSource.GAMIFICATION;
if (notification) {
topicPublisher.notificationEvent(
null,
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# for deployment or when using docker compose = prod, for local development = dev
spring.profiles.active=dev
spring.profiles.active=prod
# enable graphiql (graphiql is a web interface for exploring GraphQL)
spring.graphql.graphiql.enabled=true
spring.graphql.graphiql.path=/graphiql
Expand Down
Loading