Merge pull request #3935 from knutz3n/KEYCLOAK-4559

KEYCLOAK-4559 Filter users by realm id when search for user by attribute
This commit is contained in:
Stian Thorgersen 2017-03-15 08:44:43 +01:00 committed by GitHub
commit 44907503a4
4 changed files with 10 additions and 5 deletions

View file

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

View file

@ -34,7 +34,6 @@ import javax.persistence.Table;
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@NamedQueries({ @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="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="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"), @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="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="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="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="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="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") @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 user1 = session.users().addUser(realm, "user1");
UserModel user2 = session.users().addUser(realm, "user2"); UserModel user2 = session.users().addUser(realm, "user2");
UserModel user3 = session.users().addUser(realm, "user3"); 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("key1", "value1");
user1.setSingleAttribute("key2", "value21"); user1.setSingleAttribute("key2", "value21");
@ -287,6 +289,8 @@ public class UserModelTest extends AbstractModelTest {
user3.setSingleAttribute("key2", "value21"); user3.setSingleAttribute("key2", "value21");
otherRealmUser.setSingleAttribute("key2", "value21");
commit(); commit();
realm = session.realms().getRealmByName("original"); realm = session.realms().getRealmByName("original");