1515
1616use Pimcore \Bundle \StudioBackendBundle \Exception \Api \ForbiddenException ;
1717use Pimcore \Bundle \StudioBackendBundle \Gdpr \Event \PreResponse \GdprDataProviderEvent ;
18- use Pimcore \Bundle \StudioBackendBundle \Gdpr \Event \PreResponse \GdprExportDataEvent ;
1918use Pimcore \Bundle \StudioBackendBundle \Gdpr \Event \PreResponse \GdprSearchResultEvent ;
2019use Pimcore \Bundle \StudioBackendBundle \Gdpr \MappedParameter \GdprStructuredSearchRequest ;
2120use Pimcore \Bundle \StudioBackendBundle \Gdpr \Provider \DataProviderInterface ;
2726use Pimcore \Bundle \StudioBackendBundle \Util \Constant \HttpResponseCodes ;
2827use Pimcore \Bundle \StudioBackendBundle \Util \Constant \HttpResponseHeaders ;
2928use Pimcore \Bundle \StudioBackendBundle \Util \Trait \StreamedResponseTrait ;
29+ use Pimcore \Bundle \StudioBackendBundle \Exception \Api \InvalidArgumentException ;
3030use Symfony \Component \HttpFoundation \StreamedResponse ;
3131use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
32+ use JsonException ;
3233use function count ;
3334use function sprintf ;
3435use 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