Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HELLODATA-1295 - starting portal may result 409 fix #50

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Changes from 1 commit
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 @@ -71,27 +71,48 @@ public class DefaultUserInitializer {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public boolean initDefaultUsers() {
boolean defaultUsersInitiated = false;
if (defaultAdminProperties.getEmail() == null) {
String username = defaultAdminProperties.getUsername();
String email = defaultAdminProperties.getEmail();
if (email == null) {
log.warn("No default admin properties set, omitting");
return defaultUsersInitiated;
}
List<UserRepresentation> allUsersFromKeycloak = getAllUsersFromKeycloak();
Optional<UserRepresentation> userByEmail = allUsersFromKeycloak.stream().filter(user -> user.getEmail().equalsIgnoreCase(email)).findFirst();
Optional<UserRepresentation> userByUsername = allUsersFromKeycloak.stream().filter(user -> user.getUsername().equalsIgnoreCase(username)).findFirst();
boolean theSameUser = userByEmail.isPresent() && userByUsername.isPresent() && userByUsername.get().getId().equalsIgnoreCase(userByEmail.get().getId());

if (!theSameUser) {
log.info("Users fetched from the keycloak:");
allUsersFromKeycloak.forEach(
wieczorslawo marked this conversation as resolved.
Show resolved Hide resolved
userRepresentation -> log.info("Usr {}, username: {}, email: {}", userRepresentation.getId(), userRepresentation.getUsername(), userRepresentation.getEmail()));
throw new IllegalStateException(
String.format("There are already two different users in the keycloak for the provided username: %s and the email: %s. Please change the configuration",
username, email));
}

// Check if the default user exists in Keycloak
boolean userExistsInKeycloak = getAllUsersFromKeycloak().stream().anyMatch(user -> user.getEmail().equals(defaultAdminProperties.getEmail()));
boolean userExistsInKeycloak = allUsersFromKeycloak.stream().anyMatch(user -> user.getEmail().equals(email));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use here userByEmail.exists() (or if not, adjust to equalsIgnoreCase, like above)?


// Check if the user has already been created in a previous run
boolean userMarkedAsDefault = !defaultUserRepository.findByEmail(defaultAdminProperties.getEmail()).isEmpty();

if (!userExistsInKeycloak && !userMarkedAsDefault) {
defaultUsersInitiated = createDefaultAdmin();
boolean userMarkedAsDefault = !defaultUserRepository.findByEmail(email).isEmpty();

//different email but duplicated username
if (!userByUsername.get().getEmail().equalsIgnoreCase(email)) {
defaultUsersInitiated = createDefaultAdmin(userByUsername.get().getUsername(), defaultAdminProperties.getFirstName(), defaultAdminProperties.getLastName(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this create an exception upon calling createUserInKeycloak() as this user already exists in KC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i does and it's good, now we'll have more logs to see where the problem is (most likely bad config)

userByUsername.get().getEmail());
} else if (!userExistsInKeycloak && !userMarkedAsDefault) {
defaultUsersInitiated = createDefaultAdmin(defaultAdminProperties.getUsername(), defaultAdminProperties.getFirstName(), defaultAdminProperties.getLastName(),
defaultAdminProperties.getEmail());
} else if (userExistsInKeycloak && !userMarkedAsDefault) {
defaultUsersInitiated = markAsDefaultUser();
defaultUsersInitiated = markAsDefaultUser(defaultAdminProperties.getEmail());
}
return defaultUsersInitiated;
}

private boolean markAsDefaultUser() {
private boolean markAsDefaultUser(String email) {
boolean defaultUsersInitiated;
Optional<UserEntity> userEntityByEmail = userRepository.findUserEntityByEmailIgnoreCase(defaultAdminProperties.getEmail());
Optional<UserEntity> userEntityByEmail = userRepository.findUserEntityByEmailIgnoreCase(email);
if (userEntityByEmail.isPresent()) {
//set as superuser
UserEntity user = userEntityByEmail.get();
Expand All @@ -106,9 +127,15 @@ private boolean markAsDefaultUser() {
return defaultUsersInitiated;
}

private boolean createDefaultAdmin() {
private boolean createDefaultAdmin(String username, String firstName, String lastName, String email) {
boolean defaultUsersInitiated;
UserRepresentation user = generateDefaultAdmin();
UserRepresentation user = new UserRepresentation();
user.setUsername(username);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setEnabled(true);
user.setEmailVerified(true);
setDefaultAdminPassword(user);
String userId = createUserInKeycloak(user);
UserEntity userEntity = saveUserToDatabase(userId);
Expand Down Expand Up @@ -198,23 +225,6 @@ private void setUserPassword(UserRepresentation user, String password) {
user.setCredentials(List.of(credential));
}

@NotNull
private UserRepresentation generateDefaultAdmin() {
return generateUser(defaultAdminProperties.getUsername(), defaultAdminProperties.getFirstName(), defaultAdminProperties.getLastName(), defaultAdminProperties.getEmail());
}

private UserRepresentation generateUser(String username, String firstName, String lastName, String email) {
// Create a new user
UserRepresentation user = new UserRepresentation();
user.setUsername(username);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setEnabled(true);
user.setEmailVerified(true);
return user;
}

private List<UserRepresentation> getAllUsersFromKeycloak() {
UsersResource usersResource = keycloak.realm(realmName).users();
return usersResource.search(null, null, null, true, null).stream().filter(userRepresentation -> userRepresentation.getEmail() != null).toList();
Expand Down