Skip to content

Commit

Permalink
bugfix: usersearchindex without corresponding user object should be i…
Browse files Browse the repository at this point in the history
…gnored when searching users by email or username
  • Loading branch information
opdt committed Nov 6, 2024
1 parent a079ec9 commit 93ecb99
Showing 1 changed file with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import de.arbeitsagentur.opdt.keycloak.cassandra.user.persistence.entities.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import lombok.extern.jbosslog.JBossLog;

@JBossLog
public class CassandraUserRepository extends TransactionalRepository<User, UserDao>
implements UserRepository {
private static final String USERNAME = "username";
Expand Down Expand Up @@ -54,13 +57,22 @@ public User findUserByEmail(String realmId, String email) {
return null;
}

UserSearchIndex user =
dao.findUsers(realmId, EMAIL, email).all().stream().findFirst().orElse(null);
if (user == null) {
List<User> users =
dao.findUsers(realmId, EMAIL, email).all().stream()
.map(idx -> findUserById(realmId, idx.getUserId()))
.filter(Objects::nonNull)
.toList();

if (users.size() > 1) {
log.warn("Found multiple users with email: " + email);
return null;
}

return findUserById(realmId, user.getUserId());
if (users.isEmpty()) {
return null;
}

return users.getFirst();
}

@Override
Expand All @@ -69,13 +81,22 @@ public User findUserByUsername(String realmId, String username) {
return null;
}

UserSearchIndex user =
dao.findUsers(realmId, USERNAME, username).all().stream().findFirst().orElse(null);
if (user == null) {
List<User> users =
dao.findUsers(realmId, USERNAME, username).all().stream()
.map(idx -> findUserById(realmId, idx.getUserId()))
.filter(Objects::nonNull)
.toList();

if (users.size() > 1) {
log.warn("Found multiple users with username: " + username);
return null;
}

return findUserById(realmId, user.getUserId());
if (users.isEmpty()) {
return null;
}

return users.getFirst();
}

@Override
Expand All @@ -84,16 +105,22 @@ public User findUserByUsernameCaseInsensitive(String realmId, String username) {
return null;
}

UserSearchIndex user =
List<User> users =
dao.findUsers(realmId, USERNAME_CASE_INSENSITIVE, username).all().stream()
.findFirst()
.orElse(null);
.map(idx -> findUserById(realmId, idx.getUserId()))
.filter(Objects::nonNull)
.toList();

if (user == null) {
if (users.size() > 1) {
log.warn("Found multiple users with username (case insensitive): " + username);
return null;
}

return findUserById(realmId, user.getUserId());
if (users.isEmpty()) {
return null;
}

return users.getFirst();
}

@Override
Expand Down

0 comments on commit 93ecb99

Please sign in to comment.