Search in sources :

Example 1 with PermissionTicketEntity

use of org.keycloak.authorization.jpa.entities.PermissionTicketEntity in project keycloak by keycloak.

the class JPAPermissionTicketStore method find.

@Override
public List<PermissionTicket> find(Map<PermissionTicket.FilterOption, String> attributes, String resourceServerId, int firstResult, int maxResult) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<PermissionTicketEntity> querybuilder = builder.createQuery(PermissionTicketEntity.class);
    Root<PermissionTicketEntity> root = querybuilder.from(PermissionTicketEntity.class);
    querybuilder.select(root.get("id"));
    List<Predicate> predicates = getPredicates(builder, root, resourceServerId, attributes);
    querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id")));
    TypedQuery query = entityManager.createQuery(querybuilder);
    List<String> result = paginateQuery(query, firstResult, maxResult).getResultList();
    List<PermissionTicket> list = new LinkedList<>();
    PermissionTicketStore ticketStore = provider.getStoreFactory().getPermissionTicketStore();
    for (String id : result) {
        PermissionTicket ticket = ticketStore.findById(id, resourceServerId);
        if (Objects.nonNull(ticket)) {
            list.add(ticket);
        }
    }
    return list;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) TypedQuery(javax.persistence.TypedQuery) LinkedList(java.util.LinkedList) Predicate(javax.persistence.criteria.Predicate) PermissionTicketStore(org.keycloak.authorization.store.PermissionTicketStore) PermissionTicketEntity(org.keycloak.authorization.jpa.entities.PermissionTicketEntity)

Example 2 with PermissionTicketEntity

use of org.keycloak.authorization.jpa.entities.PermissionTicketEntity in project keycloak by keycloak.

the class JPAPermissionTicketStore method count.

@Override
public long count(Map<PermissionTicket.FilterOption, String> attributes, String resourceServerId) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> querybuilder = builder.createQuery(Long.class);
    Root<PermissionTicketEntity> root = querybuilder.from(PermissionTicketEntity.class);
    querybuilder.select(root.get("id"));
    List<Predicate> predicates = getPredicates(builder, root, resourceServerId, attributes);
    querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id")));
    TypedQuery query = entityManager.createQuery(querybuilder);
    return closing(query.getResultStream()).count();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TypedQuery(javax.persistence.TypedQuery) PermissionTicketEntity(org.keycloak.authorization.jpa.entities.PermissionTicketEntity) Predicate(javax.persistence.criteria.Predicate)

Example 3 with PermissionTicketEntity

use of org.keycloak.authorization.jpa.entities.PermissionTicketEntity in project keycloak by keycloak.

the class JPAPermissionTicketStore method create.

@Override
public PermissionTicket create(String resourceId, String scopeId, String requester, ResourceServer resourceServer) {
    PermissionTicketEntity entity = new PermissionTicketEntity();
    entity.setId(KeycloakModelUtils.generateId());
    entity.setResource(ResourceAdapter.toEntity(entityManager, provider.getStoreFactory().getResourceStore().findById(resourceId, resourceServer.getId())));
    entity.setRequester(requester);
    entity.setCreatedTimestamp(System.currentTimeMillis());
    if (scopeId != null) {
        entity.setScope(ScopeAdapter.toEntity(entityManager, provider.getStoreFactory().getScopeStore().findById(scopeId, resourceServer.getId())));
    }
    entity.setOwner(entity.getResource().getOwner());
    entity.setResourceServer(ResourceServerAdapter.toEntity(entityManager, resourceServer));
    this.entityManager.persist(entity);
    this.entityManager.flush();
    PermissionTicket model = new PermissionTicketAdapter(entity, entityManager, provider.getStoreFactory());
    return model;
}
Also used : PermissionTicket(org.keycloak.authorization.model.PermissionTicket) PermissionTicketEntity(org.keycloak.authorization.jpa.entities.PermissionTicketEntity)

Aggregations

PermissionTicketEntity (org.keycloak.authorization.jpa.entities.PermissionTicketEntity)3 TypedQuery (javax.persistence.TypedQuery)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Predicate (javax.persistence.criteria.Predicate)2 PermissionTicket (org.keycloak.authorization.model.PermissionTicket)2 LinkedList (java.util.LinkedList)1 PermissionTicketStore (org.keycloak.authorization.store.PermissionTicketStore)1