KEYCLOAK-4559 Filter users by realm id when searching for user by user attribute

This commit is contained in:
Johannes Knutsen 2017-03-14 11:05:12 +01:00
parent a555f99b1a
commit 9a0de676c4
4 changed files with 10 additions and 5 deletions

View file

@ -678,14 +678,14 @@ public class JpaUserProvider implements UserProvider, UserCredentialStore {
@Override
public List<UserModel> searchForUserByUserAttribute(String attrName, String attrValue, RealmModel realm) {
TypedQuery<UserAttributeEntity> query = em.createNamedQuery("getAttributesByNameAndValue", UserAttributeEntity.class);
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUsersByAttributeNameAndValue", UserEntity.class);
query.setParameter("name", attrName);
query.setParameter("value", attrValue);
List<UserAttributeEntity> results = query.getResultList();
query.setParameter("realmId", realm.getId());
List<UserEntity> results = query.getResultList();
List<UserModel> users = new ArrayList<UserModel>();
for (UserAttributeEntity attr : results) {
UserEntity user = attr.getUser();
for (UserEntity user : results) {
users.add(new UserAdapter(session, realm, em, user));
}
return users;

View file

@ -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"),

View file

@ -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")

View file

@ -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");