Skip to content

Commit 68712e5

Browse files
committed
Re update Json event, Handle json error, Parameter, schema, event handling, rephase, non nullable search,Text Parameter fixes
1 parent 2028712 commit 68712e5

File tree

9 files changed

+90
-127
lines changed

9 files changed

+90
-127
lines changed

src/Gdpr/Attribute/Request/GdprRequestBody.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Pimcore\Bundle\StudioBackendBundle\Gdpr\Attribute\Request;
1515

1616
use Attribute;
17+
use OpenApi\Attributes\Items;
1718
use OpenApi\Attributes\JsonContent;
1819
use OpenApi\Attributes\Property;
1920
use OpenApi\Attributes\RequestBody;
@@ -29,13 +30,24 @@ public function __construct()
2930
parent::__construct(
3031
required: true,
3132
content: new JsonContent(
32-
required: ['providerName'],
33+
34+
required: ['providers', 'searchTerms'],
3335
properties: [
3436
new Property(
35-
property: 'providerName',
36-
description: 'The key of the single provider to search (e.g., pimcore_user)',
37-
type: 'string',
38-
example: 'pimcore_user'
37+
property: 'providers',
38+
description: 'A list of provider keys to search',
39+
type: 'array',
40+
items: new Items(
41+
type: 'string',
42+
example: 'pimcore_users'
43+
)
44+
),
45+
46+
new Property(
47+
property: 'searchTerms',
48+
description: 'The object containing the search values.',
49+
ref: SearchTerms::class,
50+
type: 'object'
3951
),
4052
],
4153
type: 'object',

src/Gdpr/Controller/ExportController.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ public function __construct(
5454
operationId: 'gdpr_export',
5555
summary: 'gdpr_export_summary',
5656
description: 'gdpr_export_description',
57-
tags: [Tags::Export->value],
58-
parameters: [
59-
new TextFieldParameter(
60-
name: 'providerKey',
61-
description: 'The key of the single provider to export',
62-
required: true,
63-
example: 'pimcore_user'
64-
),
65-
]
57+
tags: [Tags::Export->value]
58+
)]
59+
#[TextFieldParameter(
60+
name: 'providerKey',
61+
description: 'The key of the single provider to export',
62+
required: true,
63+
example: 'pimcore_user'
6664
)]
6765
#[SuccessResponse(
6866
description: 'gdpr_export_success_response',

src/Gdpr/Event/PreResponse/GdprExportDataEvent.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Gdpr/Event/PreResponse/GdprSearchResultEvent.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,23 @@
1313

1414
namespace Pimcore\Bundle\StudioBackendBundle\Gdpr\Event\PreResponse;
1515

16+
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;
1617
use Pimcore\Bundle\StudioBackendBundle\Gdpr\Schema\GdprSearchResultCollection;
17-
use Symfony\Contracts\EventDispatcher\Event;
1818

1919
/**
2020
* @internal
2121
*/
22-
final class GdprSearchResultEvent extends Event
22+
final class GdprSearchResultEvent extends AbstractPreResponseEvent
2323
{
2424
public const string EVENT_NAME = 'pre_response.gdpr_search_result';
2525

26-
public function __construct(private GdprSearchResultCollection $collection)
26+
public function __construct(private readonly GdprSearchResultCollection $collection)
2727
{
28-
28+
parent::__construct($this->collection);
2929
}
3030

3131
public function getCollection(): GdprSearchResultCollection
3232
{
3333
return $this->collection;
3434
}
35-
36-
public function setCollection(GdprSearchResultCollection $collection): void
37-
{
38-
$this->collection = $collection;
39-
}
4035
}

src/Gdpr/MappedParameter/GdprStructuredSearchRequest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Constraints\NotBlank;
1919
use Symfony\Component\Validator\Constraints\Type;
2020
use Symfony\Component\Validator\Constraints\Valid;
21+
use Symfony\Component\Validator\Constraints\NotNull;
2122

2223
/**
2324
* @internal
@@ -33,7 +34,8 @@ public function __construct(
3334
public array $providers,
3435

3536
#[Valid]
36-
public ?SearchTerms $searchTerms = null
37+
#[NotNull]
38+
public SearchTerms $searchTerms
3739
) {
3840
}
3941
}

src/Gdpr/Provider/DataProviderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface DataProviderInterface
2424
/**
2525
* @return GdprDataRow[]
2626
*/
27-
public function findData(?SearchTerms $terms): array;
27+
public function findData(SearchTerms $terms): array;
2828

2929
public function getName(): string;
3030

src/Gdpr/Provider/PimcoreUserProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function findData(?SearchTerms $terms): array
31+
public function findData(SearchTerms $terms): array
3232
{
3333
$listing = new Listing();
3434
$conditionParts = [];

src/Gdpr/Schema/GdprSearchResultCollection.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace Pimcore\Bundle\StudioBackendBundle\Gdpr\Schema;
1515

16+
use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
17+
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\AdditionalAttributesTrait;
1618
use OpenApi\Attributes\Items;
1719
use OpenApi\Attributes\Property;
1820
use OpenApi\Attributes\Schema;
@@ -27,8 +29,10 @@
2729
required: ['items']
2830
)]
2931

30-
final class GdprSearchResultCollection
32+
final class GdprSearchResultCollection implements AdditionalAttributesInterface
3133
{
34+
use AdditionalAttributesTrait;
35+
3236
/**
3337
* @param array<GdprSearchResult> $items
3438
*/
@@ -38,7 +42,8 @@ public function __construct(
3842
type: 'array',
3943
items: new Items(ref: GdprSearchResult::class)
4044
)]
41-
private array $items,
45+
46+
private readonly array $items,
4247
) {
4348
}
4449

src/Gdpr/Service/GdprManagerService.php

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
1717
use Pimcore\Bundle\StudioBackendBundle\Gdpr\Event\PreResponse\GdprDataProviderEvent;
18-
use Pimcore\Bundle\StudioBackendBundle\Gdpr\Event\PreResponse\GdprExportDataEvent;
1918
use Pimcore\Bundle\StudioBackendBundle\Gdpr\Event\PreResponse\GdprSearchResultEvent;
2019
use Pimcore\Bundle\StudioBackendBundle\Gdpr\MappedParameter\GdprStructuredSearchRequest;
2120
use Pimcore\Bundle\StudioBackendBundle\Gdpr\Provider\DataProviderInterface;
@@ -27,8 +26,10 @@
2726
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
2827
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseHeaders;
2928
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\StreamedResponseTrait;
29+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
3030
use Symfony\Component\HttpFoundation\StreamedResponse;
3131
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
32+
use JsonException;
3233
use function count;
3334
use function sprintf;
3435
use function strlen;
@@ -63,36 +64,11 @@ public function getAvailableProviders(): Collection
6364
public function search(GdprStructuredSearchRequest $request): GdprSearchResultCollection
6465
{
6566
$allResults = [];
66-
$currentUser = $this->securityService->getCurrentUser();
6767

6868
foreach ($request->providers as $providerKey) {
6969
$provider = $this->loader->resolve($providerKey);
70-
71-
$permissions = $provider->getRequiredPermissions();
72-
$isGranted = false;
73-
74-
if (empty($permissions)) {
75-
$isGranted = true; // No permissions required
76-
} else {
77-
foreach ($permissions as $permission) {
78-
if ($currentUser->isAllowed($permission)) {
79-
$isGranted = true;
80-
81-
break;
82-
}
83-
}
84-
}
85-
86-
// Check if the current user has the required permission
87-
if (!$isGranted) {
88-
throw new ForbiddenException(
89-
sprintf(
90-
'Not allowed to access the targeted provider "%s". Required permission(s): "%s"',
91-
$providerKey,
92-
implode(', ', $permissions)
93-
)
94-
);
95-
}
70+
71+
$this->checkProviderPermission($provider);
9672

9773
$results = $provider->findData($request->searchTerms);
9874

@@ -113,34 +89,9 @@ public function search(GdprStructuredSearchRequest $request): GdprSearchResultCo
11389
*/
11490
public function getExportDataAsJson(int $id, string $providerKey): StreamedResponse
11591
{
116-
$currentUser = $this->securityService->getCurrentUser();
117-
11892
$provider = $this->loader->resolve($providerKey);
119-
120-
$permissions = $provider->getRequiredPermissions();
121-
$isGranted = false;
122-
123-
if (empty($permissions)) {
124-
$isGranted = true; // No permissions required
125-
} else {
126-
foreach ($permissions as $permission) {
127-
if ($currentUser->isAllowed($permission)) {
128-
$isGranted = true;
129-
130-
break;
131-
}
132-
}
133-
}
134-
135-
if (!$isGranted) {
136-
throw new ForbiddenException(
137-
sprintf(
138-
'Not allowed for provider: %s. Required permission(s): %s',
139-
$provider->getKey(),
140-
implode(', ', $permissions)
141-
)
142-
);
143-
}
93+
94+
$this->checkProviderPermission($provider);
14495

14596
$data = $provider->getSingleItemForDownload($id); //id is a single item of a particular provider
14697

@@ -194,11 +145,19 @@ private function getSearchResultCollection(array $results): GdprSearchResultColl
194145
*/
195146
private function createExportResponse(mixed $data, string $providerKey, int $id): StreamedResponse
196147
{
197-
$event = new GdprExportDataEvent($data);
198-
$this->eventDispatcher->dispatch($event, GdprExportDataEvent::EVENT_NAME);
199-
$finalData = $event->getData();
200-
201-
$jsonData = json_encode($finalData, JSON_THROW_ON_ERROR);
148+
try {
149+
$jsonData = json_encode($data, JSON_THROW_ON_ERROR);
150+
} catch (JsonException $e) {
151+
throw new InvalidArgumentException(
152+
sprintf(
153+
'JSON encode failed for "%s" (ID: %d): %s',
154+
$providerKey,
155+
$id,
156+
$e->getMessage()
157+
),
158+
previous: $e
159+
);
160+
}
202161

203162
$filename = sprintf('gdpr-export-%s-%d.json', $providerKey, $id);
204163
$fileSize = strlen($jsonData);
@@ -234,4 +193,35 @@ private function sortProviders(array $providers): array
234193

235194
return $providers;
236195
}
196+
197+
/**
198+
* @throws ForbiddenException
199+
*/
200+
private function checkProviderPermission(DataProviderInterface $provider): void
201+
{
202+
$currentUser = $this->securityService->getCurrentUser();
203+
$permissions = $provider->getRequiredPermissions();
204+
205+
// Check if user has at least one of the required permissions in order to access the provider
206+
$isGranted = false;
207+
208+
if ($currentUser !== null) {
209+
foreach ($permissions as $permission) {
210+
if ($currentUser->isAllowed($permission)) {
211+
$isGranted = true;
212+
break;
213+
}
214+
}
215+
}
216+
217+
if (!$isGranted) {
218+
throw new ForbiddenException(
219+
sprintf(
220+
'Not allowed for provider: %s. Required permission(s): %s',
221+
$provider->getKey(),
222+
implode(', ', $permissions)
223+
)
224+
);
225+
}
226+
}
237227
}

0 commit comments

Comments
 (0)