Search in sources :

Example 16 with UserEntity

use of org.keycloak.models.jpa.entities.UserEntity in project keycloak by keycloak.

the class JpaUserProvider method getUsersCount.

@Override
public int getUsersCount(RealmModel realm, Map<String, String> params) {
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Long> userQuery = qb.createQuery(Long.class);
    Root<UserEntity> from = userQuery.from(UserEntity.class);
    Expression<Long> count = qb.count(from);
    userQuery = userQuery.select(count);
    List<Predicate> restrictions = new ArrayList<>();
    restrictions.add(qb.equal(from.get("realmId"), realm.getId()));
    for (Map.Entry<String, String> entry : params.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (key == null || value == null) {
            continue;
        }
        switch(key) {
            case UserModel.USERNAME:
                restrictions.add(qb.like(from.get("username"), "%" + value + "%"));
                break;
            case UserModel.FIRST_NAME:
                restrictions.add(qb.like(from.get("firstName"), "%" + value + "%"));
                break;
            case UserModel.LAST_NAME:
                restrictions.add(qb.like(from.get("lastName"), "%" + value + "%"));
                break;
            case UserModel.EMAIL:
                restrictions.add(qb.like(from.get("email"), "%" + value + "%"));
                break;
            case UserModel.EMAIL_VERIFIED:
                restrictions.add(qb.equal(from.get("emailVerified"), Boolean.parseBoolean(value.toLowerCase())));
                break;
        }
    }
    userQuery = userQuery.where(restrictions.toArray(new Predicate[0]));
    TypedQuery<Long> query = em.createQuery(userQuery);
    Long result = query.getSingleResult();
    return result.intValue();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) UserEntity(org.keycloak.models.jpa.entities.UserEntity) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

UserEntity (org.keycloak.models.jpa.entities.UserEntity)16 ArrayList (java.util.ArrayList)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 CredentialEntity (org.keycloak.models.jpa.entities.CredentialEntity)4 FederatedIdentityEntity (org.keycloak.models.jpa.entities.FederatedIdentityEntity)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UserGroupMembershipEntity (org.keycloak.models.jpa.entities.UserGroupMembershipEntity)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Root (javax.persistence.criteria.Root)1 Subquery (javax.persistence.criteria.Subquery)1 FederatedIdentityModel (org.keycloak.models.FederatedIdentityModel)1 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)1 ModelException (org.keycloak.models.ModelException)1 RequiredActionProviderModel (org.keycloak.models.RequiredActionProviderModel)1 UserProvider (org.keycloak.models.UserProvider)1 UserAttributeEntity (org.keycloak.models.jpa.entities.UserAttributeEntity)1