Search in sources :

Example 1 with UserGroupMembershipEntity

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

the class JpaUserProvider method getUsersCount.

@Override
public int getUsersCount(RealmModel realm, String search, Set<String> groupIds) {
    if (groupIds == null || groupIds.isEmpty()) {
        return 0;
    }
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> queryBuilder = builder.createQuery(Long.class);
    Root<UserGroupMembershipEntity> groupMembership = queryBuilder.from(UserGroupMembershipEntity.class);
    Join<UserGroupMembershipEntity, UserEntity> userJoin = groupMembership.join("user");
    queryBuilder.select(builder.count(userJoin));
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(userJoin.get("realmId"), realm.getId()));
    predicates.add(builder.or(getSearchOptionPredicateArray(search, builder, userJoin)));
    predicates.add(groupMembership.get("groupId").in(groupIds));
    queryBuilder.where(predicates.toArray(new Predicate[0]));
    return em.createQuery(queryBuilder).getSingleResult().intValue();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity) ArrayList(java.util.ArrayList) UserEntity(org.keycloak.models.jpa.entities.UserEntity) Predicate(javax.persistence.criteria.Predicate)

Example 2 with UserGroupMembershipEntity

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

the class JpaUserProvider method getUsersCount.

@Override
public int getUsersCount(RealmModel realm, Map<String, String> params, Set<String> groupIds) {
    if (groupIds == null || groupIds.isEmpty()) {
        return 0;
    }
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Long> userQuery = qb.createQuery(Long.class);
    Root<UserGroupMembershipEntity> from = userQuery.from(UserGroupMembershipEntity.class);
    Expression<Long> count = qb.count(from.get("user"));
    userQuery = userQuery.select(count);
    List<Predicate> restrictions = new ArrayList<>();
    restrictions.add(qb.equal(from.get("user").get("realmId"), realm.getId()));
    restrictions.add(from.get("groupId").in(groupIds));
    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("user").get("username"), "%" + value + "%"));
                break;
            case UserModel.FIRST_NAME:
                restrictions.add(qb.like(from.get("user").get("firstName"), "%" + value + "%"));
                break;
            case UserModel.LAST_NAME:
                restrictions.add(qb.like(from.get("user").get("lastName"), "%" + value + "%"));
                break;
            case UserModel.EMAIL:
                restrictions.add(qb.like(from.get("user").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) UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with UserGroupMembershipEntity

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

the class JpaUserProvider method searchForUserStream.

@Override
public Stream<UserModel> searchForUserStream(RealmModel realm, Map<String, String> attributes, Integer firstResult, Integer maxResults) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<UserEntity> queryBuilder = builder.createQuery(UserEntity.class);
    Root<UserEntity> root = queryBuilder.from(UserEntity.class);
    List<Predicate> predicates = new ArrayList<>();
    List<Predicate> attributePredicates = new ArrayList<>();
    predicates.add(builder.equal(root.get("realmId"), realm.getId()));
    if (!session.getAttributeOrDefault(UserModel.INCLUDE_SERVICE_ACCOUNT, true)) {
        predicates.add(root.get("serviceAccountClientLink").isNull());
    }
    Join<Object, Object> federatedIdentitiesJoin = null;
    for (Map.Entry<String, String> entry : attributes.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (value == null) {
            continue;
        }
        switch(key) {
            case UserModel.SEARCH:
                for (String stringToSearch : value.trim().split("\\s+")) {
                    predicates.add(builder.or(getSearchOptionPredicateArray(stringToSearch, builder, root)));
                }
                break;
            case USERNAME:
            case FIRST_NAME:
            case LAST_NAME:
            case EMAIL:
                if (Boolean.valueOf(attributes.getOrDefault(UserModel.EXACT, Boolean.FALSE.toString()))) {
                    predicates.add(builder.equal(builder.lower(root.get(key)), value.toLowerCase()));
                } else {
                    predicates.add(builder.like(builder.lower(root.get(key)), "%" + value.toLowerCase() + "%"));
                }
                break;
            case EMAIL_VERIFIED:
                predicates.add(builder.equal(root.get(key), Boolean.parseBoolean(value.toLowerCase())));
                break;
            case UserModel.ENABLED:
                predicates.add(builder.equal(root.get(key), Boolean.parseBoolean(value)));
                break;
            case UserModel.IDP_ALIAS:
                if (federatedIdentitiesJoin == null) {
                    federatedIdentitiesJoin = root.join("federatedIdentities");
                }
                predicates.add(builder.equal(federatedIdentitiesJoin.get("identityProvider"), value));
                break;
            case UserModel.IDP_USER_ID:
                if (federatedIdentitiesJoin == null) {
                    federatedIdentitiesJoin = root.join("federatedIdentities");
                }
                predicates.add(builder.equal(federatedIdentitiesJoin.get("userId"), value));
                break;
            case UserModel.EXACT:
                break;
            // All unknown attributes will be assumed as custom attributes
            default:
                Join<UserEntity, UserAttributeEntity> attributesJoin = root.join("attributes", JoinType.LEFT);
                attributePredicates.add(builder.and(builder.equal(builder.lower(attributesJoin.get("name")), key.toLowerCase()), builder.equal(builder.lower(attributesJoin.get("value")), value.toLowerCase())));
                break;
        }
    }
    if (!attributePredicates.isEmpty()) {
        predicates.add(builder.and(attributePredicates.toArray(new Predicate[0])));
    }
    Set<String> userGroups = (Set<String>) session.getAttribute(UserModel.GROUPS);
    if (userGroups != null) {
        Subquery subquery = queryBuilder.subquery(String.class);
        Root<UserGroupMembershipEntity> from = subquery.from(UserGroupMembershipEntity.class);
        subquery.select(builder.literal(1));
        List<Predicate> subPredicates = new ArrayList<>();
        subPredicates.add(from.get("groupId").in(userGroups));
        subPredicates.add(builder.equal(from.get("user").get("id"), root.get("id")));
        Subquery subquery1 = queryBuilder.subquery(String.class);
        subquery1.select(builder.literal(1));
        Root from1 = subquery1.from(ResourceEntity.class);
        List<Predicate> subs = new ArrayList<>();
        Expression<String> groupId = from.get("groupId");
        subs.add(builder.like(from1.get("name"), builder.concat("group.resource.", groupId)));
        subquery1.where(subs.toArray(new Predicate[subs.size()]));
        subPredicates.add(builder.exists(subquery1));
        subquery.where(subPredicates.toArray(new Predicate[subPredicates.size()]));
        predicates.add(builder.exists(subquery));
    }
    queryBuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get(UserModel.USERNAME)));
    TypedQuery<UserEntity> query = em.createQuery(queryBuilder);
    UserProvider users = session.users();
    return closing(paginateQuery(query, firstResult, maxResults).getResultStream()).map(userEntity -> users.getUserById(realm, userEntity.getId()));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserAttributeEntity(org.keycloak.models.jpa.entities.UserAttributeEntity) UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity) Set(java.util.Set) HashSet(java.util.HashSet) Root(javax.persistence.criteria.Root) ArrayList(java.util.ArrayList) Subquery(javax.persistence.criteria.Subquery) UserEntity(org.keycloak.models.jpa.entities.UserEntity) Predicate(javax.persistence.criteria.Predicate) UserProvider(org.keycloak.models.UserProvider) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with UserGroupMembershipEntity

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

the class UserAdapter method createGetGroupsQuery.

private TypedQuery<String> createGetGroupsQuery() {
    // we query ids only as the group  might be cached and following the @ManyToOne will result in a load
    // even if we're getting just the id.
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<String> queryBuilder = builder.createQuery(String.class);
    Root<UserGroupMembershipEntity> root = queryBuilder.from(UserGroupMembershipEntity.class);
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(root.get("user"), getEntity()));
    queryBuilder.select(root.get("groupId"));
    queryBuilder.where(predicates.toArray(new Predicate[0]));
    return em.createQuery(queryBuilder);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Example 5 with UserGroupMembershipEntity

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

the class UserAdapter method joinGroupImpl.

protected void joinGroupImpl(GroupModel group) {
    UserGroupMembershipEntity entity = new UserGroupMembershipEntity();
    entity.setUser(getEntity());
    entity.setGroupId(group.getId());
    em.persist(entity);
    em.flush();
    em.detach(entity);
}
Also used : UserGroupMembershipEntity(org.keycloak.models.jpa.entities.UserGroupMembershipEntity)

Aggregations

UserGroupMembershipEntity (org.keycloak.models.jpa.entities.UserGroupMembershipEntity)7 ArrayList (java.util.ArrayList)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 Predicate (javax.persistence.criteria.Predicate)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UserEntity (org.keycloak.models.jpa.entities.UserEntity)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Root (javax.persistence.criteria.Root)1 Subquery (javax.persistence.criteria.Subquery)1 UserProvider (org.keycloak.models.UserProvider)1 UserAttributeEntity (org.keycloak.models.jpa.entities.UserAttributeEntity)1