diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java index f35acea820..83bfac4a24 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java @@ -678,14 +678,14 @@ public class JpaUserProvider implements UserProvider, UserCredentialStore { @Override public List searchForUserByUserAttribute(String attrName, String attrValue, RealmModel realm) { - TypedQuery query = em.createNamedQuery("getAttributesByNameAndValue", UserAttributeEntity.class); + TypedQuery query = em.createNamedQuery("getRealmUsersByAttributeNameAndValue", UserEntity.class); query.setParameter("name", attrName); query.setParameter("value", attrValue); - List results = query.getResultList(); + query.setParameter("realmId", realm.getId()); + List results = query.getResultList(); List users = new ArrayList(); - for (UserAttributeEntity attr : results) { - UserEntity user = attr.getUser(); + for (UserEntity user : results) { users.add(new UserAdapter(session, realm, em, user)); } return users; diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java index 7fa3de0e67..bbb0f0fa84 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserAttributeEntity.java @@ -34,7 +34,6 @@ import javax.persistence.Table; * @version $Revision: 1 $ */ @NamedQueries({ - @NamedQuery(name="getAttributesByNameAndValue", query="select attr from UserAttributeEntity attr where attr.name = :name and attr.value = :value"), @NamedQuery(name="deleteUserAttributesByRealm", query="delete from UserAttributeEntity attr where attr.user IN (select u from UserEntity u where u.realmId=:realmId)"), @NamedQuery(name="deleteUserAttributesByNameAndUser", query="delete from UserAttributeEntity attr where attr.user.id = :userId and attr.name = :name"), @NamedQuery(name="deleteUserAttributesByNameAndUserOtherThan", query="delete from UserAttributeEntity attr where attr.user.id = :userId and attr.name = :name and attr.id <> :attrId"), diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java index f069988d4c..50b77607a6 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java @@ -49,6 +49,8 @@ import java.util.Collection; @NamedQuery(name="getRealmUserByFirstLastName", query="select u from UserEntity u where u.firstName = :first and u.lastName = :last and u.realmId = :realmId"), @NamedQuery(name="getRealmUserByServiceAccount", query="select u from UserEntity u where u.serviceAccountClientLink = :clientInternalId and u.realmId = :realmId"), @NamedQuery(name="getRealmUserCount", query="select count(u) from UserEntity u where u.realmId = :realmId"), + @NamedQuery(name="getRealmUsersByAttributeNameAndValue", query="select u from UserEntity u join u.attributes attr " + + "where u.realmId = :realmId and attr.name = :name and attr.value = :value"), @NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId"), @NamedQuery(name="deleteUsersByRealmAndLink", query="delete from UserEntity u where u.realmId = :realmId and u.federationLink=:link"), @NamedQuery(name="unlinkUsers", query="update UserEntity u set u.federationLink = null where u.realmId = :realmId and u.federationLink=:link") diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java index 27547d8614..191a39af89 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java @@ -278,6 +278,8 @@ public class UserModelTest extends AbstractModelTest { UserModel user1 = session.users().addUser(realm, "user1"); UserModel user2 = session.users().addUser(realm, "user2"); UserModel user3 = session.users().addUser(realm, "user3"); + RealmModel otherRealm = realmManager.createRealm("other"); + UserModel otherRealmUser = session.users().addUser(otherRealm, "user1"); user1.setSingleAttribute("key1", "value1"); user1.setSingleAttribute("key2", "value21"); @@ -287,6 +289,8 @@ public class UserModelTest extends AbstractModelTest { user3.setSingleAttribute("key2", "value21"); + otherRealmUser.setSingleAttribute("key2", "value21"); + commit(); realm = session.realms().getRealmByName("original");