Search in sources :

Example 1 with ResourceEntity

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

the class JPAResourceStore method findByScope.

@Override
public void findByScope(List<String> scopes, String resourceServerId, Consumer<Resource> consumer) {
    TypedQuery<ResourceEntity> query = entityManager.createNamedQuery("findResourceIdByScope", ResourceEntity.class);
    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("scopeIds", scopes);
    query.setParameter("serverId", resourceServerId);
    StoreFactory storeFactory = provider.getStoreFactory();
    query.getResultList().stream().map(id -> new ResourceAdapter(id, entityManager, storeFactory)).forEach(consumer);
}
Also used : ResourceServer(org.keycloak.authorization.model.ResourceServer) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) KeycloakModelUtils(org.keycloak.models.utils.KeycloakModelUtils) StreamsUtil.closing(org.keycloak.utils.StreamsUtil.closing) StoreFactory(org.keycloak.authorization.store.StoreFactory) NoResultException(javax.persistence.NoResultException) FlushModeType(javax.persistence.FlushModeType) EntityManager(javax.persistence.EntityManager) ResourceStore(org.keycloak.authorization.store.ResourceStore) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) PaginationUtils.paginateQuery(org.keycloak.models.jpa.PaginationUtils.paginateQuery) List(java.util.List) Predicate(javax.persistence.criteria.Predicate) ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) Expression(javax.persistence.criteria.Expression) LinkedList(java.util.LinkedList) Root(javax.persistence.criteria.Root) Resource(org.keycloak.authorization.model.Resource) ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity) StoreFactory(org.keycloak.authorization.store.StoreFactory)

Example 2 with ResourceEntity

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

the class JPAResourceStore method findByTypeInstance.

@Override
public void findByTypeInstance(String type, String resourceServerId, Consumer<Resource> consumer) {
    TypedQuery<ResourceEntity> query = entityManager.createNamedQuery("findResourceIdByTypeInstance", ResourceEntity.class);
    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("type", type);
    query.setParameter("serverId", resourceServerId);
    StoreFactory storeFactory = provider.getStoreFactory();
    query.getResultList().stream().map(entity -> new ResourceAdapter(entity, entityManager, storeFactory)).forEach(consumer);
}
Also used : ResourceServer(org.keycloak.authorization.model.ResourceServer) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) KeycloakModelUtils(org.keycloak.models.utils.KeycloakModelUtils) StreamsUtil.closing(org.keycloak.utils.StreamsUtil.closing) StoreFactory(org.keycloak.authorization.store.StoreFactory) NoResultException(javax.persistence.NoResultException) FlushModeType(javax.persistence.FlushModeType) EntityManager(javax.persistence.EntityManager) ResourceStore(org.keycloak.authorization.store.ResourceStore) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) PaginationUtils.paginateQuery(org.keycloak.models.jpa.PaginationUtils.paginateQuery) List(java.util.List) Predicate(javax.persistence.criteria.Predicate) ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) Expression(javax.persistence.criteria.Expression) LinkedList(java.util.LinkedList) Root(javax.persistence.criteria.Root) Resource(org.keycloak.authorization.model.Resource) ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity) StoreFactory(org.keycloak.authorization.store.StoreFactory)

Example 3 with ResourceEntity

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

the class JPAResourceStore method create.

@Override
public Resource create(String id, String name, ResourceServer resourceServer, String owner) {
    ResourceEntity entity = new ResourceEntity();
    if (id == null) {
        entity.setId(KeycloakModelUtils.generateId());
    } else {
        entity.setId(id);
    }
    entity.setName(name);
    entity.setResourceServer(ResourceServerAdapter.toEntity(entityManager, resourceServer).getId());
    entity.setOwner(owner);
    this.entityManager.persist(entity);
    this.entityManager.flush();
    return new ResourceAdapter(entity, entityManager, provider.getStoreFactory());
}
Also used : ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity)

Example 4 with ResourceEntity

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

the class JPAResourceStore method delete.

@Override
public void delete(String id) {
    ResourceEntity resource = entityManager.getReference(ResourceEntity.class, id);
    if (resource == null)
        return;
    resource.getScopes().clear();
    this.entityManager.remove(resource);
}
Also used : ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity)

Example 5 with ResourceEntity

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

the class JPAResourceStore method findByResourceServer.

@Override
public List<Resource> findByResourceServer(Map<Resource.FilterOption, String[]> attributes, String resourceServerId, int firstResult, int maxResult) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ResourceEntity> querybuilder = builder.createQuery(ResourceEntity.class);
    Root<ResourceEntity> root = querybuilder.from(ResourceEntity.class);
    querybuilder.select(root.get("id"));
    List<Predicate> predicates = new ArrayList();
    if (resourceServerId != null) {
        predicates.add(builder.equal(root.get("resourceServer"), resourceServerId));
    }
    attributes.forEach((filterOption, value) -> {
        switch(filterOption) {
            case ID:
            case OWNER:
                predicates.add(root.get(filterOption.getName()).in(value));
                break;
            case SCOPE_ID:
                predicates.add(root.join("scopes").get("id").in(value));
                break;
            case OWNER_MANAGED_ACCESS:
                predicates.add(builder.equal(root.get(filterOption.getName()), Boolean.valueOf(value[0])));
                break;
            case URI:
                predicates.add(builder.lower(root.join("uris")).in(value[0].toLowerCase()));
                break;
            case URI_NOT_NULL:
                // predicates.add(builder.isNotEmpty(root.get("uris"))); looks like there is a bug in hibernate and this line doesn't work: https://hibernate.atlassian.net/browse/HHH-6686
                // Workaround
                Expression<Integer> urisSize = builder.size(root.get("uris"));
                predicates.add(builder.notEqual(urisSize, 0));
                break;
            case NAME:
            case TYPE:
                predicates.add(builder.like(builder.lower(root.get(filterOption.getName())), "%" + value[0].toLowerCase() + "%"));
                break;
            case EXACT_NAME:
                predicates.add(builder.equal(builder.lower(root.get(filterOption.getName())), value[0].toLowerCase()));
                break;
            default:
                throw new IllegalArgumentException("Unsupported filter [" + filterOption + "]");
        }
    });
    querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("name")));
    TypedQuery query = entityManager.createQuery(querybuilder);
    List<String> result = paginateQuery(query, firstResult, maxResult).getResultList();
    List<Resource> list = new LinkedList<>();
    ResourceStore resourceStore = provider.getStoreFactory().getResourceStore();
    for (String id : result) {
        Resource resource = resourceStore.findById(id, resourceServerId);
        if (resource != null) {
            list.add(resource);
        }
    }
    return list;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) Resource(org.keycloak.authorization.model.Resource) ResourceStore(org.keycloak.authorization.store.ResourceStore) LinkedList(java.util.LinkedList) Predicate(javax.persistence.criteria.Predicate) ResourceEntity(org.keycloak.authorization.jpa.entities.ResourceEntity)

Aggregations

ResourceEntity (org.keycloak.authorization.jpa.entities.ResourceEntity)7 ResourceStore (org.keycloak.authorization.store.ResourceStore)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 TypedQuery (javax.persistence.TypedQuery)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 Resource (org.keycloak.authorization.model.Resource)4 List (java.util.List)3 Map (java.util.Map)3 Consumer (java.util.function.Consumer)3 EntityManager (javax.persistence.EntityManager)3 FlushModeType (javax.persistence.FlushModeType)3 NoResultException (javax.persistence.NoResultException)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 Expression (javax.persistence.criteria.Expression)3 Root (javax.persistence.criteria.Root)3 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)3 ResourceServer (org.keycloak.authorization.model.ResourceServer)3 StoreFactory (org.keycloak.authorization.store.StoreFactory)3