Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -198,8 +198,8 @@ public void setSingleAttribute(String name, String value) {
value = KeycloakModelUtils.toLowerCaseSafe(value);
}
if (updated == null) {
Set<String> oldEntries = getAttributeStream(name).collect(Collectors.toSet());
Set<String> newEntries = value != null ? Set.of(value) : Collections.emptySet();
List<String> oldEntries = getAttributeStream(name).sorted().collect(Collectors.toList());
List<String> newEntries = value == null ? List.of() : List.of(value);
if (CollectionUtil.collectionEquals(oldEntries, newEntries)) {
return;
}
Expand All @@ -215,13 +215,8 @@ public void setAttribute(String name, List<String> values) {
if (lowerCasedFirstValue != null) values = Collections.singletonList(lowerCasedFirstValue);
}
if (updated == null) {
Set<String> oldEntries = getAttributeStream(name).collect(Collectors.toSet());
Set<String> newEntries;
if (values == null) {
newEntries = new HashSet<>();
} else {
newEntries = new HashSet<>(values);
}
List<String> oldEntries = getAttributeStream(name).sorted().collect(Collectors.toList());
List<String> newEntries = values == null ? List.of() : values.stream().sorted().toList();
if (CollectionUtil.collectionEquals(oldEntries, newEntries)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
package org.keycloak.models.jpa;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
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;

Expand Down Expand Up @@ -154,8 +152,8 @@ public void setSingleAttribute(String name, String value) {
if (value == null) {
removeAttribute(name);
} else {
Set<String> oldEntries = getAttributeStream(name).collect(Collectors.toSet());
Set<String> newEntries = Set.of(value);
List<String> oldEntries = getAttributeStream(name).sorted().collect(Collectors.toList());
List<String> newEntries = List.of(value);
if (CollectionUtil.collectionEquals(oldEntries, newEntries)) {
return;
}
Expand Down Expand Up @@ -205,13 +203,8 @@ public void setAttribute(String name, List<String> values) {
return;
}

Set<String> oldEntries = getAttributeStream(name).collect(Collectors.toSet());
Set<String> newEntries;
if (values == null) {
newEntries = new HashSet<>();
} else {
newEntries = new HashSet<>(values);
}
List<String> oldEntries = getAttributeStream(name).sorted().collect(Collectors.toList());
List<String> newEntries = values == null ? List.of() : values.stream().sorted().toList();
if (CollectionUtil.collectionEquals(oldEntries, newEntries)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,54 @@ public void testUpdateUserSingleAttribute(KeycloakSession session) {
});
}

@TestOnServer
public void testUpdateUserAttributeMultipleSameValues(KeycloakSession session) {
String key = "foo";

// Testing "setAttribute"
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession currentSession) -> {
RealmModel realm = currentSession.realms().getRealmByName("original");
UserModel user = currentSession.users().addUser(realm, "user");

user.setAttribute(key, List.of("bar", "bar"));

List<String> expected = List.of("bar", "bar");
assertThat(user.getAttributes().get(key), equalTo(expected));
});

KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession currentSession) -> {
RealmModel realm = currentSession.realms().getRealmByName("original");
UserModel user = currentSession.users().getUserByUsername(realm, "user");

user.setAttribute(key, List.of("bar"));

List<String> expected = List.of("bar");
assertThat(user.getAttributes().get(key), equalTo(expected));
});

// Testing "setSingleAttribute"
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession currentSession) -> {
RealmModel realm = currentSession.realms().getRealmByName("original");
UserModel user = currentSession.users().getUserByUsername(realm, "user");

user.setAttribute(key, List.of("bar", "bar"));

List<String> expected = List.of("bar", "bar");
assertThat(user.getAttributes().get(key), equalTo(expected));
});

KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession currentSession) -> {
RealmModel realm = currentSession.realms().getRealmByName("original");
UserModel user = currentSession.users().getUserByUsername(realm, "user");

user.setSingleAttribute(key, "bar");

List<String> expected = List.of("bar");
assertThat(user.getAttributes().get(key), equalTo(expected));
});

}

@TestOnServer
public void testSearchByString(KeycloakSession session) {

Expand Down
Loading