diff --git a/src/Controller/PostSubmitRequest.php b/src/ApiPlatform/PostSubmitRequest.php similarity index 95% rename from src/Controller/PostSubmitRequest.php rename to src/ApiPlatform/PostSubmitRequest.php index 79cd7c9..7ee4891 100644 --- a/src/Controller/PostSubmitRequest.php +++ b/src/ApiPlatform/PostSubmitRequest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Dbp\Relay\DispatchBundle\Controller; +namespace Dbp\Relay\DispatchBundle\ApiPlatform; use Dbp\Relay\DispatchBundle\Authorization\AuthorizationService; use Dbp\Relay\DispatchBundle\Entity\Request; diff --git a/src/DataPersister/RequestDataPersister.php b/src/ApiPlatform/RequestProcessor.php similarity index 70% rename from src/DataPersister/RequestDataPersister.php rename to src/ApiPlatform/RequestProcessor.php index 49bcf02..77cdb32 100644 --- a/src/DataPersister/RequestDataPersister.php +++ b/src/ApiPlatform/RequestProcessor.php @@ -2,9 +2,11 @@ declare(strict_types=1); -namespace Dbp\Relay\DispatchBundle\DataPersister; +namespace Dbp\Relay\DispatchBundle\ApiPlatform; -use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; +use ApiPlatform\Metadata\DeleteOperationInterface; +use ApiPlatform\Metadata\Operation; +use ApiPlatform\State\ProcessorInterface; use Dbp\Relay\CoreBundle\Exception\ApiError; use Dbp\Relay\DispatchBundle\Authorization\AuthorizationService; use Dbp\Relay\DispatchBundle\Entity\Request; @@ -12,7 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; -class RequestDataPersister extends AbstractController implements ContextAwareDataPersisterInterface +class RequestProcessor extends AbstractController implements ProcessorInterface { /** * @var DispatchService @@ -30,11 +32,6 @@ public function __construct(DispatchService $dispatchService, AuthorizationServi $this->auth = $auth; } - public function supports($data, array $context = []): bool - { - return $data instanceof Request; - } - /** * @param mixed $data * @@ -71,25 +68,22 @@ public function persist($data, array $context = []) return $request; } - /** - * @param mixed $data - * - * @return void - */ - public function remove($data, array $context = []) + public function process($data, Operation $operation, array $uriVariables = [], array $context = []) { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); $this->auth->checkCanUse(); - $request = $data; - assert($request instanceof Request); + if ($operation instanceof DeleteOperationInterface) { + $request = $data; + assert($request instanceof Request); - $this->auth->checkCanWrite($request->getGroupId()); + $this->auth->checkCanWrite($request->getGroupId()); - if ($request->isSubmitted()) { - throw ApiError::withDetails(Response::HTTP_BAD_REQUEST, 'Submitted requests cannot be modified!', 'dispatch:request-submitted-read-only'); - } + if ($request->isSubmitted()) { + throw ApiError::withDetails(Response::HTTP_BAD_REQUEST, 'Submitted requests cannot be modified!', 'dispatch:request-submitted-read-only'); + } - $this->dispatchService->removeRequestById($request->getIdentifier()); + $this->dispatchService->removeRequestById($request->getIdentifier()); + } } } diff --git a/src/ApiPlatform/RequestProvider.php b/src/ApiPlatform/RequestProvider.php new file mode 100644 index 0000000..132b5d6 --- /dev/null +++ b/src/ApiPlatform/RequestProvider.php @@ -0,0 +1,68 @@ +dispatchService = $dispatchService; + $this->auth = $auth; + } + + /** + * @return PartialPaginatorInterface|Request + */ + public function provide(Operation $operation, array $uriVariables = [], array $context = []) + { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $this->auth->checkCanUse(); + + if ($operation instanceof CollectionOperationInterface) { + $filters = $context['filters'] ?? []; + + $groupId = $filters['groupId'] ?? null; + if ($groupId === null) { + throw ApiError::withDetails(Response::HTTP_BAD_REQUEST, 'groupId query parameter missing'); + } + $this->auth->checkCanReadMetadata($groupId); + + return new WholeResultPaginator( + $this->dispatchService->getRequestsForGroupId($groupId), + Pagination::getCurrentPageNumber($filters), + Pagination::getMaxNumItemsPerPage($filters)); + } else { + $id = $uriVariables['identifier']; + assert(is_string($id)); + $request = $this->dispatchService->getRequestById($id); + $groupId = $request->getGroupId(); + $this->auth->checkCanReadMetadata($groupId); + + return $request; + } + } +} diff --git a/src/DataProvider/RequestCollectionDataProvider.php b/src/DataProvider/RequestCollectionDataProvider.php deleted file mode 100644 index b976661..0000000 --- a/src/DataProvider/RequestCollectionDataProvider.php +++ /dev/null @@ -1,59 +0,0 @@ -dispatchService = $dispatchService; - $this->auth = $auth; - } - - public function supports(string $resourceClass, string $operationName = null, array $context = []): bool - { - return Request::class === $resourceClass; - } - - public function getCollection(string $resourceClass, string $operationName = null, array $context = []): Paginator - { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); - $this->auth->checkCanUse(); - - $filters = $context['filters'] ?? []; - - $groupId = $filters['groupId'] ?? null; - if ($groupId === null) { - throw ApiError::withDetails(Response::HTTP_BAD_REQUEST, 'groupId query parameter missing'); - } - $this->auth->checkCanReadMetadata($groupId); - - return new WholeResultPaginator( - $this->dispatchService->getRequestsForGroupId($groupId), - Pagination::getCurrentPageNumber($filters), - Pagination::getMaxNumItemsPerPage($filters)); - } -} diff --git a/src/DataProvider/RequestItemDataProvider.php b/src/DataProvider/RequestItemDataProvider.php deleted file mode 100644 index 0f5e86e..0000000 --- a/src/DataProvider/RequestItemDataProvider.php +++ /dev/null @@ -1,47 +0,0 @@ -dispatchService = $dispatchService; - $this->auth = $auth; - } - - public function supports(string $resourceClass, string $operationName = null, array $context = []): bool - { - return Request::class === $resourceClass; - } - - public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Request - { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); - $this->auth->checkCanUse(); - - $request = $this->dispatchService->getRequestById($id); - $groupId = $request->getGroupId(); - $this->auth->checkCanReadMetadata($groupId); - - return $request; - } -} diff --git a/src/DependencyInjection/DbpRelayDispatchExtension.php b/src/DependencyInjection/DbpRelayDispatchExtension.php index 07c7521..1153925 100644 --- a/src/DependencyInjection/DbpRelayDispatchExtension.php +++ b/src/DependencyInjection/DbpRelayDispatchExtension.php @@ -21,8 +21,6 @@ class DbpRelayDispatchExtension extends ConfigurableExtension implements Prepend public function loadInternal(array $mergedConfig, ContainerBuilder $container) { - $this->addResourceClassDirectory($container, __DIR__.'/../Entity'); - $pathsToHide = [ '/dispatch/pre-addressing-requests/{identifier}', '/dispatch/pre-addressing-requests', diff --git a/src/Entity/Request.php b/src/Entity/Request.php index 5b860ae..fa7eb14 100644 --- a/src/Entity/Request.php +++ b/src/Entity/Request.php @@ -6,9 +6,8 @@ date_default_timezone_set('UTC'); -use ApiPlatform\Core\Annotation\ApiProperty; use ApiPlatform\Core\Annotation\ApiResource; -use Dbp\Relay\DispatchBundle\Controller\PostSubmitRequest; +use Dbp\Relay\DispatchBundle\ApiPlatform\PostSubmitRequest; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; @@ -57,27 +56,6 @@ * } * }, * itemOperations={ - * "get" = { - * "security" = "is_granted('IS_AUTHENTICATED_FULLY')", - * "path" = "/dispatch/requests/{identifier}", - * "openapi_context" = { - * "tags" = {"Dispatch"} - * }, - * }, - * "put" = { - * "security" = "is_granted('IS_AUTHENTICATED_FULLY')", - * "path" = "/dispatch/requests/{identifier}", - * "openapi_context" = { - * "tags" = {"Dispatch"} - * }, - * }, - * "delete" = { - * "security" = "is_granted('IS_AUTHENTICATED_FULLY')", - * "path" = "/dispatch/requests/{identifier}", - * "openapi_context" = { - * "tags" = {"Dispatch"} - * }, - * }, * "post_submit" = { * "security" = "is_granted('IS_AUTHENTICATED_FULLY')", * "method" = "POST", @@ -103,14 +81,6 @@ * } * }, * }, - * }, - * iri="https://schema.org/Action", - * shortName="DispatchRequest", - * normalizationContext={ - * "groups" = {"DispatchRequest:output"} - * }, - * denormalizationContext={ - * "groups" = {"DispatchRequest:input"} * } * ) */ @@ -119,14 +89,12 @@ class Request /** * @ORM\Id * @ORM\Column(type="string", length=50) - * @ApiProperty(identifier=true) * @Groups({"DispatchRequest:output"}) */ private $identifier; /** * @ORM\Column(type="string", length=255) - * @ApiProperty(iri="https://schema.org/name") * @Groups({"DispatchRequest:output:name", "DispatchRequest:input"}) * @Assert\Length( * max=255, @@ -139,7 +107,6 @@ class Request /** * @ORM\Column(type="datetime") - * @ApiProperty(iri="https://schema.org/dateCreated") * @Groups({"DispatchRequest:output"}) * * @var \DateTimeInterface @@ -148,7 +115,6 @@ class Request /** * @ORM\Column(type="string", length=50) - * @ApiProperty(iri="https://schema.org/identifier") * @Groups({"DispatchRequest:output"}) * * @var string @@ -157,7 +123,6 @@ class Request /** * @ORM\Column(type="string", length=255) - * @ApiProperty(iri="https://schema.org/alternateName") * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=255, @@ -170,7 +135,6 @@ class Request /** * @ORM\Column(type="string", length=255) - * @ApiProperty(iri="https://schema.org/alternateName") * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=255, @@ -183,7 +147,6 @@ class Request /** * @ORM\Column(type="datetime") - * @ApiProperty * @Groups({"DispatchRequest:output"}) * * @var \DateTimeInterface @@ -192,7 +155,6 @@ class Request /** * @ORM\Column(type="string", length=2) - * @ApiProperty(iri="https://schema.org/addressCountry") * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=2, @@ -205,7 +167,6 @@ class Request /** * @ORM\Column(type="string", length=20) - * @ApiProperty(iri="https://schema.org/postalCode") * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=20, @@ -218,7 +179,6 @@ class Request /** * @ORM\Column(type="string", length=120) - * @ApiProperty(iri="https://schema.org/addressLocality") * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=120, @@ -231,7 +191,6 @@ class Request /** * @ORM\Column(type="string", length=120) - * @ApiProperty(iri="https://schema.org/streetAddress") * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=120, @@ -244,7 +203,6 @@ class Request /** * @ORM\Column(type="string", length=10) - * @ApiProperty * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=10, @@ -257,7 +215,6 @@ class Request /** * @ORM\Column(type="string", length=255) - * @ApiProperty * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=255, @@ -271,7 +228,6 @@ class Request /** * @ORM\Column(type="string", length=25) - * @ApiProperty * @Groups({"DispatchRequest:output", "DispatchRequest:input"}) * @Assert\Length( * max=25, diff --git a/src/Resources/config/api_resources.yaml b/src/Resources/config/api_resources.yaml index 190e5c1..4a53c84 100644 --- a/src/Resources/config/api_resources.yaml +++ b/src/Resources/config/api_resources.yaml @@ -276,3 +276,87 @@ resources: iri: 'https://schema.org/fileFormat' contentSize: iri: 'https://schema.org/contentSize' + + Dbp\Relay\DispatchBundle\Entity\Request: + types: ['https://schema.org/Action'] + shortName: 'DispatchRequest' + normalizationContext: + groups: ['DispatchRequest:output'] + denormalizationContext: + groups: ["DispatchRequest:input"] + + operations: + ApiPlatform\Metadata\Get: + security: "is_granted('IS_AUTHENTICATED_FULLY')" + provider: Dbp\Relay\DispatchBundle\ApiPlatform\RequestProvider + uriTemplate: '/dispatch/requests/{identifier}' + openapiContext: + tags: ["Dispatch"] + + ApiPlatform\Metadata\Put: + security: "is_granted('IS_AUTHENTICATED_FULLY')" + provider: Dbp\Relay\DispatchBundle\ApiPlatform\RequestProvider + processor: Dbp\Relay\DispatchBundle\ApiPlatform\RequestProcessor + uriTemplate: '/dispatch/requests/{identifier}"' + openapiContext: + tags: ["Dispatch"] + + ApiPlatform\Metadata\Delete: + security: "is_granted('IS_AUTHENTICATED_FULLY')" + provider: Dbp\Relay\DispatchBundle\ApiPlatform\RequestProvider + processor: Dbp\Relay\DispatchBundle\ApiPlatform\RequestProcessor + uriTemplate: '/dispatch/requests/{identifier}"' + openapiContext: + tags: ["Dispatch"] + + ApiPlatform\Metadata\Post: + security: "is_granted('IS_AUTHENTICATED_FULLY')" + controller: Dbp\Relay\DispatchBundle\ApiPlatform\CreateRequestFileAction + uriTemplate: "/dispatch/request-files" + method: 'POST' + deserialize: false + openapiContext: + tags: ["Dispatch"] + responses: + 415: + description: "Unsupported Media Type - Only PDF files can be added!" + requestBody: + content: + multipart/form-data: + schema: + type: "object" + required: ["file", "dispatchRequestIdentifier"] + properties: + dispatchRequestIdentifier: + description: "ID of the request" + type: "string" + example: "4d553985-d44f-404f-acf3-cd0eac7ae9c2" + file: + type: "string" + format: "binary" + + properties: + identifier: + identifier: true + name: + iri: 'https://schema.org/name' + dateCreated: + iri: 'https://schema.org/dateCreated' + personIdentifier: + iri: 'https://schema.org/identifier' + senderFullName: + iri: 'https://schema.org/alternateName' + senderOrganizationName: + iri: 'https://schema.org/alternateName' + dateSubmitted: + senderAddressCountry: + iri: 'https://schema.org/addressCountry' + senderPostalCode: + iri: 'https://schema.org/postalCode' + senderAddressLocality: + iri: 'https://schema.org/addressLocality' + senderStreetAddress: + iri: 'https://schema.org/streetAddress' + senderBuildingNumber: + groupId: + referenceNumber: \ No newline at end of file diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 0ef923d..7404523 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -1,14 +1,4 @@ services: - Dbp\Relay\DispatchBundle\DataPersister\: - resource: '../../DataPersister' - autowire: true - autoconfigure: true - - Dbp\Relay\DispatchBundle\DataProvider\: - resource: '../../DataProvider' - autowire: true - autoconfigure: true - Dbp\Relay\DispatchBundle\ApiPlatform\: resource: '../../ApiPlatform' autowire: true @@ -28,12 +18,6 @@ services: autowire: true autoconfigure: true - Dbp\Relay\DispatchBundle\Controller\: - tags: ['controller.service_arguments'] - resource: '../../Controller' - autowire: true - autoconfigure: true - Dbp\Relay\DispatchBundle\Command\: resource: '../../Command' autowire: true