Search in sources :

Example 6 with UserGroupMembershipEntity

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

the class UserAdapter method createCountGroupsQuery.

private TypedQuery<Long> createCountGroupsQuery() {
    // 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<Long> queryBuilder = builder.createQuery(Long.class);
    Root<UserGroupMembershipEntity> root = queryBuilder.from(UserGroupMembershipEntity.class);
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(root.get("user"), getEntity()));
    queryBuilder.select(builder.count(root));
    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 7 with UserGroupMembershipEntity

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

the class UserAdapter method leaveGroup.

@Override
public void leaveGroup(GroupModel group) {
    if (user == null || group == null)
        return;
    TypedQuery<UserGroupMembershipEntity> query = getUserGroupMappingQuery(group);
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    List<UserGroupMembershipEntity> results = query.getResultList();
    if (results.size() == 0)
        return;
    for (UserGroupMembershipEntity entity : results) {
        em.remove(entity);
    }
    em.flush();
}
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