From 1b90aa14a1f83e50407b663d89c4e39f67fa073f Mon Sep 17 00:00:00 2001 From: mattamon Date: Tue, 5 Mar 2024 08:39:39 +0100 Subject: [PATCH] Add public voter --- .../api_platform/resources/translation.yaml | 1 + config/services.yaml | 8 ++- src/Exception/NoRequestException.php | 25 +++++++ src/Security/Trait/PublicTranslationTrait.php | 41 +++++++++++ src/Security/Voter/PublicTokenVoter.php | 70 +++++++++++++++++++ 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/Exception/NoRequestException.php create mode 100644 src/Security/Trait/PublicTranslationTrait.php create mode 100644 src/Security/Voter/PublicTokenVoter.php diff --git a/config/api_platform/resources/translation.yaml b/config/api_platform/resources/translation.yaml index c10adbbea..4f2d2d77c 100644 --- a/config/api_platform/resources/translation.yaml +++ b/config/api_platform/resources/translation.yaml @@ -1,5 +1,6 @@ resources: Pimcore\Bundle\StudioApiBundle\Dto\Translation: + security: 'is_granted("PUBLIC_API_PLATFORM", "translation")' operations: ApiPlatform\Metadata\Post: processor: Pimcore\Bundle\StudioApiBundle\State\TranslationProcessor diff --git a/config/services.yaml b/config/services.yaml index e128801a5..5eb9c4be8 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -85,4 +85,10 @@ services: class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProvider Pimcore\Bundle\StudioApiBundle\Service\TranslatorServiceInterface: - class: Pimcore\Bundle\StudioApiBundle\Service\TranslatorService \ No newline at end of file + class: Pimcore\Bundle\StudioApiBundle\Service\TranslatorService + + #Voters + Pimcore\Bundle\StudioApiBundle\Security\Voter\PublicTokenVoter: + arguments: [ '@request_stack' ] + tags: + - { name: security.voter } \ No newline at end of file diff --git a/src/Exception/NoRequestException.php b/src/Exception/NoRequestException.php new file mode 100644 index 000000000..d86875cd4 --- /dev/null +++ b/src/Exception/NoRequestException.php @@ -0,0 +1,25 @@ +all(); + if(!array_key_exists(self::ARRAY_KEYS_INDEX, $parameters)) { + return false; + } + + foreach($parameters[self::ARRAY_KEYS_INDEX] as $key) { + // Allow only public keys + if(!in_array($key, PublicTranslations::PUBLIC_KEYS, true)) { + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/src/Security/Voter/PublicTokenVoter.php b/src/Security/Voter/PublicTokenVoter.php new file mode 100644 index 000000000..c76e78517 --- /dev/null +++ b/src/Security/Voter/PublicTokenVoter.php @@ -0,0 +1,70 @@ +getCurrentRequest(); + + // TODO Add security service once merged with PR#5 + return $this->voteOnRequest($request, $subject); + } + + private function getCurrentRequest(): Request + { + $request = $this->requestStack->getCurrentRequest(); + if(!$request) { + throw new NoRequestException('No request found'); + } + + return $request; + } + + private function voteOnRequest(Request $request, string $subject): bool + { + return match ($subject) { + 'translation' => $this->voteOnTranslation($request->getPayload()), + default => false, + }; + } +} \ No newline at end of file