|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace App\Security; |
| 4 | + |
| 5 | +use Ibexa\Contracts\Core\Repository\ContentService; |
| 6 | +use Ibexa\Contracts\Core\Repository\PermissionResolver; |
| 7 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; |
| 8 | +use Ibexa\Contracts\FormBuilder\FieldType\Model\Form; |
| 9 | +use Ibexa\Contracts\FormBuilder\FieldType\Model\FormSubmission; |
| 10 | +use Ibexa\Contracts\FormBuilder\FieldType\Model\FormSubmissionList; |
| 11 | +use Ibexa\Contracts\FormBuilder\FormSubmission\FormSubmissionServiceInterface; |
| 12 | +use Ibexa\Core\Base\Exceptions\NotFoundException; |
| 13 | +use Ibexa\Core\Base\Exceptions\UnauthorizedException; |
| 14 | +use Ibexa\FormBuilder\FormSubmission\Gateway\FormSubmissionGateway; |
| 15 | + |
| 16 | +class FormSubmissionServiceDecorator implements FormSubmissionServiceInterface |
| 17 | +{ |
| 18 | + public FormSubmissionServiceInterface $innerService; |
| 19 | + public PermissionResolver $permissionResolver; |
| 20 | + public ContentService $contentService; |
| 21 | + public FormSubmissionGateway $gateway; |
| 22 | + public function __construct(FormSubmissionServiceInterface $innerService, PermissionResolver $permissionResolver, ContentService $contentService, FormSubmissionGateway $gateway) |
| 23 | + { |
| 24 | + $this->innerService = $innerService; |
| 25 | + $this->permissionResolver = $permissionResolver; |
| 26 | + $this->contentService = $contentService; |
| 27 | + $this->gateway = $gateway; |
| 28 | + } |
| 29 | + |
| 30 | + public function create(ContentInfo $content, string $languageCode, Form $form, array $data): FormSubmission |
| 31 | + { |
| 32 | + return $this->innerService->create($content, $languageCode, $form, $data); |
| 33 | + } |
| 34 | + |
| 35 | + public function loadById(int $id): FormSubmission |
| 36 | + { |
| 37 | + $submissions = $this->gateway->loadById($id); // First manual data fetch |
| 38 | + |
| 39 | + if (empty($submissions)) { |
| 40 | + throw new NotFoundException('FormSubmission', $id); |
| 41 | + } |
| 42 | + |
| 43 | + $content = $this->contentService->loadContent($submissions[0]['content_id']); |
| 44 | + if (!$this->permissionResolver->canUser('form', 'read_submissions', $content)) { |
| 45 | + throw new UnauthorizedException('form', 'read_submissions', ['contentId' => $content->getId()]); // Permission check |
| 46 | + } |
| 47 | + |
| 48 | + return $this->innerService->loadById($id); // Second data fetch through inner service |
| 49 | + } |
| 50 | + |
| 51 | + // The same permission check pattern is repeated in the methods below |
| 52 | + |
| 53 | + public function delete(FormSubmission $submission): void |
| 54 | + { |
| 55 | + $submissionId = $submission->getId(); |
| 56 | + $submissions = $this->gateway->loadById($submissionId); |
| 57 | + |
| 58 | + if (empty($submissions)) { |
| 59 | + throw new NotFoundException('FormSubmission', $submissionId); |
| 60 | + } |
| 61 | + |
| 62 | + $content = $this->contentService->loadContent($submissions[0]['content_id']); |
| 63 | + if (!$this->permissionResolver->canUser('form', 'read_submissions', $content)) { |
| 64 | + throw new UnauthorizedException('form', 'read_submissions', ['contentId' => $content->getId()]); |
| 65 | + } |
| 66 | + |
| 67 | + $this->innerService->delete($submission); |
| 68 | + } |
| 69 | + |
| 70 | + public function loadByContent(ContentInfo $content, ?string $languageCode = null, int $offset = 0, int $limit = 25): FormSubmissionList |
| 71 | + { |
| 72 | + if (!$this->permissionResolver->canUser('form', 'read_submissions', $content)) { |
| 73 | + throw new UnauthorizedException('form', 'read_submissions', ['contentId' => $content->getId()]); |
| 74 | + } |
| 75 | + |
| 76 | + return $this->innerService->loadByContent($content, $languageCode, $offset, $limit); |
| 77 | + } |
| 78 | + |
| 79 | + public function loadAllByContentForExport(ContentInfo $content, ?string $languageCode = null): array |
| 80 | + { |
| 81 | + if (!$this->permissionResolver->canUser('form', 'read_submissions', $content)) { |
| 82 | + throw new UnauthorizedException('form', 'read_submissions', ['contentId' => $content->getId()]); |
| 83 | + } |
| 84 | + |
| 85 | + return $this->innerService->loadAllByContentForExport($content, $languageCode); |
| 86 | + } |
| 87 | + |
| 88 | + public function loadHeaders(ContentInfo $content, ?string $languageCode = null): array |
| 89 | + { |
| 90 | + if (!$this->permissionResolver->canUser('form', 'read_submissions', $content)) { |
| 91 | + throw new UnauthorizedException('form', 'read_submissions', ['contentId' => $content->getId()]); |
| 92 | + } |
| 93 | + |
| 94 | + return $this->innerService->loadHeaders($content, $languageCode); |
| 95 | + } |
| 96 | + |
| 97 | + public function getCount(ContentInfo $content, ?string $languageCode = null): int |
| 98 | + { |
| 99 | + if (!$this->permissionResolver->canUser('form', 'read_submissions', $content)) { |
| 100 | + throw new UnauthorizedException('form', 'read_submissions', ['contentId' => $content->getId()]); |
| 101 | + } |
| 102 | + |
| 103 | + return $this->innerService->getCount($content, $languageCode); |
| 104 | + } |
| 105 | +} |
0 commit comments