Search in sources :

Example 1 with ResourceServerEntity

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

the class JPAResourceServerStore method create.

@Override
public ResourceServer create(ClientModel client) {
    String clientId = client.getId();
    if (!StorageId.isLocalStorage(clientId)) {
        throw new ModelException("Creating resource server from federated ClientModel not supported");
    }
    ResourceServerEntity entity = new ResourceServerEntity();
    entity.setId(clientId);
    this.entityManager.persist(entity);
    return new ResourceServerAdapter(entity, entityManager, provider.getStoreFactory());
}
Also used : ResourceServerEntity(org.keycloak.authorization.jpa.entities.ResourceServerEntity) ModelException(org.keycloak.models.ModelException)

Example 2 with ResourceServerEntity

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

the class JPAResourceServerStore method delete.

@Override
public void delete(ClientModel client) {
    String id = client.getId();
    ResourceServerEntity entity = entityManager.find(ResourceServerEntity.class, id);
    if (entity == null)
        return;
    // This didn't work, had to loop through and remove each policy individually
    // entityManager.createNamedQuery("deletePolicyByResourceServer")
    // .setParameter("serverId", id).executeUpdate();
    {
        TypedQuery<String> query = entityManager.createNamedQuery("findPolicyIdByServerId", String.class);
        query.setParameter("serverId", id);
        List<String> result = query.getResultList();
        for (String policyId : result) {
            entityManager.remove(entityManager.getReference(PolicyEntity.class, policyId));
        }
    }
    {
        TypedQuery<String> query = entityManager.createNamedQuery("findPermissionTicketIdByServerId", String.class);
        query.setParameter("serverId", id);
        List<String> result = query.getResultList();
        for (String permissionId : result) {
            entityManager.remove(entityManager.getReference(PermissionTicketEntity.class, permissionId));
        }
    }
    // entityManager.createNamedQuery("deleteResourceByResourceServer")
    // .setParameter("serverId", id).executeUpdate();
    {
        TypedQuery<String> query = entityManager.createNamedQuery("findResourceIdByServerId", String.class);
        query.setParameter("serverId", id);
        List<String> result = query.getResultList();
        for (String resourceId : result) {
            entityManager.remove(entityManager.getReference(ResourceEntity.class, resourceId));
        }
    }
    // entityManager.createNamedQuery("deleteScopeByResourceServer")
    // .setParameter("serverId", id).executeUpdate();
    {
        TypedQuery<String> query = entityManager.createNamedQuery("findScopeIdByResourceServer", String.class);
        query.setParameter("serverId", id);
        List<String> result = query.getResultList();
        for (String scopeId : result) {
            entityManager.remove(entityManager.getReference(ScopeEntity.class, scopeId));
        }
    }
    this.entityManager.remove(entity);
    entityManager.flush();
    entityManager.detach(entity);
}
Also used : ResourceServerEntity(org.keycloak.authorization.jpa.entities.ResourceServerEntity) TypedQuery(javax.persistence.TypedQuery) List(java.util.List)

Aggregations

ResourceServerEntity (org.keycloak.authorization.jpa.entities.ResourceServerEntity)2 List (java.util.List)1 TypedQuery (javax.persistence.TypedQuery)1 ModelException (org.keycloak.models.ModelException)1