[KEYCLOAK-7939] - Startup logs warning instead of error when admin user already exists

This commit is contained in:
Pedro Igor 2019-03-19 15:34:56 -03:00 committed by Bruno Oliveira da Silva
parent a795bc0e3a
commit d2275ca563
2 changed files with 15 additions and 3 deletions

View file

@ -455,4 +455,8 @@ public interface ServicesLogger extends BasicLogger {
@Message(id=103, value="Failed to reset password. User is temporarily disabled") @Message(id=103, value="Failed to reset password. User is temporarily disabled")
void passwordResetFailed(@Cause Throwable t); void passwordResetFailed(@Cause Throwable t);
@LogMessage(level = WARN)
@Message(id=104, value="Not creating user %s. It already exists.")
void notCreatingExistingUser(String userName);
} }

View file

@ -33,6 +33,7 @@ import org.keycloak.models.KeycloakSessionTask;
import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.UserProvider;
import org.keycloak.models.dblock.DBLockManager; import org.keycloak.models.dblock.DBLockManager;
import org.keycloak.models.dblock.DBLockProvider; import org.keycloak.models.dblock.DBLockProvider;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
@ -427,21 +428,28 @@ public class KeycloakApplication extends Application {
for (RealmRepresentation realmRep : realms) { for (RealmRepresentation realmRep : realms) {
for (UserRepresentation userRep : realmRep.getUsers()) { for (UserRepresentation userRep : realmRep.getUsers()) {
KeycloakSession session = sessionFactory.create(); KeycloakSession session = sessionFactory.create();
try { try {
session.getTransactionManager().begin(); session.getTransactionManager().begin();
RealmModel realm = session.realms().getRealmByName(realmRep.getRealm()); RealmModel realm = session.realms().getRealmByName(realmRep.getRealm());
if (realm == null) { if (realm == null) {
ServicesLogger.LOGGER.addUserFailedRealmNotFound(userRep.getUsername(), realmRep.getRealm()); ServicesLogger.LOGGER.addUserFailedRealmNotFound(userRep.getUsername(), realmRep.getRealm());
}
UserProvider users = session.users();
if (users.getUserByUsername(userRep.getUsername(), realm) != null) {
ServicesLogger.LOGGER.notCreatingExistingUser(userRep.getUsername());
} else { } else {
UserModel user = session.users().addUser(realm, userRep.getUsername()); UserModel user = users.addUser(realm, userRep.getUsername());
user.setEnabled(userRep.isEnabled()); user.setEnabled(userRep.isEnabled());
RepresentationToModel.createCredentials(userRep, session, realm, user, false); RepresentationToModel.createCredentials(userRep, session, realm, user, false);
RepresentationToModel.createRoleMappings(userRep, user, realm); RepresentationToModel.createRoleMappings(userRep, user, realm);
ServicesLogger.LOGGER.addUserSuccess(userRep.getUsername(), realmRep.getRealm());
} }
session.getTransactionManager().commit(); session.getTransactionManager().commit();
ServicesLogger.LOGGER.addUserSuccess(userRep.getUsername(), realmRep.getRealm());
} catch (ModelDuplicateException e) { } catch (ModelDuplicateException e) {
session.getTransactionManager().rollback(); session.getTransactionManager().rollback();
ServicesLogger.LOGGER.addUserFailedUserExists(userRep.getUsername(), realmRep.getRealm()); ServicesLogger.LOGGER.addUserFailedUserExists(userRep.getUsername(), realmRep.getRealm());