Refactoring - Version stable
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -73,4 +73,5 @@ public interface UserServiceClient {
|
||||
void sendVerificationEmail(
|
||||
@PathParam("userId") String userId,
|
||||
@QueryParam("realm") String realmName);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.lions.unionflow.server.service;
|
||||
|
||||
import dev.lions.unionflow.server.client.RoleServiceClient;
|
||||
import dev.lions.unionflow.server.client.UserServiceClient;
|
||||
import dev.lions.unionflow.server.entity.Membre;
|
||||
import dev.lions.unionflow.server.repository.MembreRepository;
|
||||
@@ -53,6 +54,10 @@ public class MembreKeycloakSyncService {
|
||||
@RestClient
|
||||
UserServiceClient userServiceClient;
|
||||
|
||||
@Inject
|
||||
@RestClient
|
||||
RoleServiceClient roleServiceClient;
|
||||
|
||||
/**
|
||||
* Provisionne un compte Keycloak pour un Membre existant qui n'en a pas encore.
|
||||
*
|
||||
@@ -178,15 +183,12 @@ public class MembreKeycloakSyncService {
|
||||
userServiceClient.updateUser(keycloakUserId, user, DEFAULT_REALM);
|
||||
}
|
||||
|
||||
// Ajouter le rôle MEMBRE_ACTIF s'il n'est pas déjà présent
|
||||
List<String> roles = user.getRealmRoles() != null
|
||||
? new java.util.ArrayList<>(user.getRealmRoles())
|
||||
: new java.util.ArrayList<>();
|
||||
if (!roles.contains("MEMBRE_ACTIF")) {
|
||||
roles.add("MEMBRE_ACTIF");
|
||||
user.setRealmRoles(roles);
|
||||
userServiceClient.updateUser(keycloakUserId, user, DEFAULT_REALM);
|
||||
}
|
||||
// Assigner MEMBRE_ACTIF via l'endpoint dédié
|
||||
roleServiceClient.assignRealmRoles(
|
||||
keycloakUserId,
|
||||
DEFAULT_REALM,
|
||||
new RoleServiceClient.RoleNamesRequest(List.of("MEMBRE_ACTIF"))
|
||||
);
|
||||
|
||||
LOGGER.info("✅ Rôle MEMBRE_ACTIF assigné dans Keycloak pour " + membre.getNomComplet());
|
||||
|
||||
@@ -230,19 +232,26 @@ public class MembreKeycloakSyncService {
|
||||
// S'assurer que le compte est activé
|
||||
if (Boolean.FALSE.equals(user.getEnabled())) {
|
||||
user.setEnabled(true);
|
||||
userServiceClient.updateUser(keycloakUserId, user, DEFAULT_REALM);
|
||||
}
|
||||
|
||||
// Construire la liste de rôles : ajouter ADMIN_ORGANISATION, retirer MEMBRE et MEMBRE_ACTIF
|
||||
List<String> roles = user.getRealmRoles() != null
|
||||
? new java.util.ArrayList<>(user.getRealmRoles())
|
||||
: new java.util.ArrayList<>();
|
||||
roles.remove("MEMBRE");
|
||||
roles.remove("MEMBRE_ACTIF");
|
||||
if (!roles.contains("ADMIN_ORGANISATION")) {
|
||||
roles.add("ADMIN_ORGANISATION");
|
||||
// Révoquer MEMBRE et MEMBRE_ACTIF (non bloquant — peuvent ne pas exister)
|
||||
try {
|
||||
roleServiceClient.revokeRealmRoles(
|
||||
keycloakUserId,
|
||||
DEFAULT_REALM,
|
||||
new RoleServiceClient.RoleNamesRequest(List.of("MEMBRE", "MEMBRE_ACTIF"))
|
||||
);
|
||||
} catch (Exception e) {
|
||||
LOGGER.warning("⚠️ Révocation MEMBRE/MEMBRE_ACTIF non bloquante: " + e.getMessage());
|
||||
}
|
||||
user.setRealmRoles(roles);
|
||||
userServiceClient.updateUser(keycloakUserId, user, DEFAULT_REALM);
|
||||
|
||||
// Assigner ADMIN_ORGANISATION via l'endpoint dédié
|
||||
roleServiceClient.assignRealmRoles(
|
||||
keycloakUserId,
|
||||
DEFAULT_REALM,
|
||||
new RoleServiceClient.RoleNamesRequest(List.of("ADMIN_ORGANISATION"))
|
||||
);
|
||||
|
||||
LOGGER.info("✅ Rôle ADMIN_ORGANISATION assigné dans Keycloak pour " + membre.getNomComplet());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user