From d2275ca563bc9f1f26a293b374ce9eb45fc8eed7 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Tue, 19 Mar 2019 15:34:56 -0300 Subject: [PATCH] [KEYCLOAK-7939] - Startup logs warning instead of error when admin user already exists --- .../java/org/keycloak/services/ServicesLogger.java | 4 ++++ .../services/resources/KeycloakApplication.java | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/services/src/main/java/org/keycloak/services/ServicesLogger.java b/services/src/main/java/org/keycloak/services/ServicesLogger.java index 98420abc29..0cd06a5764 100644 --- a/services/src/main/java/org/keycloak/services/ServicesLogger.java +++ b/services/src/main/java/org/keycloak/services/ServicesLogger.java @@ -455,4 +455,8 @@ public interface ServicesLogger extends BasicLogger { @Message(id=103, value="Failed to reset password. User is temporarily disabled") void passwordResetFailed(@Cause Throwable t); + @LogMessage(level = WARN) + @Message(id=104, value="Not creating user %s. It already exists.") + void notCreatingExistingUser(String userName); + } diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java index cd6daf56c3..f6f49bf8fa 100644 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -33,6 +33,7 @@ import org.keycloak.models.KeycloakSessionTask; import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; +import org.keycloak.models.UserProvider; import org.keycloak.models.dblock.DBLockManager; import org.keycloak.models.dblock.DBLockProvider; import org.keycloak.models.utils.KeycloakModelUtils; @@ -427,21 +428,28 @@ public class KeycloakApplication extends Application { for (RealmRepresentation realmRep : realms) { for (UserRepresentation userRep : realmRep.getUsers()) { KeycloakSession session = sessionFactory.create(); + try { session.getTransactionManager().begin(); - RealmModel realm = session.realms().getRealmByName(realmRep.getRealm()); + if (realm == null) { ServicesLogger.LOGGER.addUserFailedRealmNotFound(userRep.getUsername(), realmRep.getRealm()); + } + + UserProvider users = session.users(); + + if (users.getUserByUsername(userRep.getUsername(), realm) != null) { + ServicesLogger.LOGGER.notCreatingExistingUser(userRep.getUsername()); } else { - UserModel user = session.users().addUser(realm, userRep.getUsername()); + UserModel user = users.addUser(realm, userRep.getUsername()); user.setEnabled(userRep.isEnabled()); RepresentationToModel.createCredentials(userRep, session, realm, user, false); RepresentationToModel.createRoleMappings(userRep, user, realm); + ServicesLogger.LOGGER.addUserSuccess(userRep.getUsername(), realmRep.getRealm()); } session.getTransactionManager().commit(); - ServicesLogger.LOGGER.addUserSuccess(userRep.getUsername(), realmRep.getRealm()); } catch (ModelDuplicateException e) { session.getTransactionManager().rollback(); ServicesLogger.LOGGER.addUserFailedUserExists(userRep.getUsername(), realmRep.getRealm());