Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation endpoint #10

Merged
merged 16 commits into from
Mar 5, 2024
12 changes: 12 additions & 0 deletions config/api_platform/resources/translation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resources:
Pimcore\Bundle\StudioApiBundle\Dto\Translation:
#security: 'is_granted("PUBLIC_API_PLATFORM", "translation")'
operations:
ApiPlatform\Metadata\Post:
processor: Pimcore\Bundle\StudioApiBundle\State\TranslationProcessor
uriTemplate: '/translations'
openapiContext:
summary: 'Getting translations based on context'

normalizationContext:
groups: [ 'translation:read' ]
6 changes: 6 additions & 0 deletions config/serialization/translation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Pimcore\Bundle\StudioApiBundle\Dto\Translation:
attributes:
locale:
groups: ['translation:read']
keys:
groups: ['translation:read']
10 changes: 9 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
Pimcore\Bundle\StudioApiBundle\State\ResetPasswordProcessor: ~
Pimcore\Bundle\StudioApiBundle\State\Token\Create\Processor: ~
Pimcore\Bundle\StudioApiBundle\State\Token\Refresh\Processor: ~
Pimcore\Bundle\StudioApiBundle\State\TranslationProcessor: ~

# Filters
Pimcore\Bundle\StudioApiBundle\Filter\AssetParentIdFilter:
Expand Down Expand Up @@ -85,19 +86,26 @@ services:
Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProvider

Pimcore\Bundle\StudioApiBundle\Service\TranslatorServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\TranslatorService

Pimcore\Bundle\StudioApiBundle\Service\TokenServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\TokenService

Pimcore\Bundle\StudioApiBundle\Service\SecurityServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\SecurityService


#Voters
Pimcore\Bundle\StudioApiBundle\Security\Voter\TokenVoter:
arguments: ['@request_stack']
tags:
- { name: security.voter }

Pimcore\Bundle\StudioApiBundle\Security\Voter\PublicTokenVoter:
arguments: [ '@request_stack' ]
tags:
- { name: security.voter }

#Decorators
Pimcore\Bundle\StudioApiBundle\ApiPlatform\OpenApiFactoryDecorator:
decorates: 'api_platform.openapi.factory'
39 changes: 39 additions & 0 deletions src/Dto/Translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Dto;

/**
* @internal
*/
final readonly class Translation
{
public function __construct(
private string $locale = 'en',
private array $keys = []
) {
}

public function getLocale(): string
{
return $this->locale;
}

public function getKeys(): array
{
return $this->keys;
}
}
23 changes: 23 additions & 0 deletions src/Exception/NonPublicTranslationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Exception;

use RuntimeException;

final class NonPublicTranslationException extends RuntimeException
{
}
50 changes: 50 additions & 0 deletions src/Security/Trait/PublicTranslationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Security\Trait;

use Pimcore\Bundle\StudioApiBundle\Exception\NonPublicTranslationException;
use Pimcore\Bundle\StudioApiBundle\Util\Constants\PublicTranslations;
use Symfony\Component\HttpFoundation\InputBag;

trait PublicTranslationTrait
{
private const ARRAY_KEYS_INDEX = 'keys';

private function voteOnTranslation(InputBag $payload): bool
{
$parameters = $payload->all();
if (!array_key_exists(self::ARRAY_KEYS_INDEX, $parameters)) {
return false;
}

$nonPublicTranslations = array_diff(
$parameters[self::ARRAY_KEYS_INDEX],
PublicTranslations::PUBLIC_KEYS
);

if (!empty($nonPublicTranslations)) {
throw new NonPublicTranslationException(
sprintf(
'You have requested non public keys: %s',
implode(',', $nonPublicTranslations)
)
);
}

return true;
}
}
68 changes: 68 additions & 0 deletions src/Security/Voter/PublicTokenVoter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Security\Voter;

use Pimcore\Bundle\StudioApiBundle\Security\Trait\PublicTranslationTrait;
use Pimcore\Bundle\StudioApiBundle\Security\Trait\RequestTrait;
use Pimcore\Bundle\StudioApiBundle\Service\SecurityServiceInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

final class PublicTokenVoter extends Voter
{
use RequestTrait;
use PublicTranslationTrait;

private const SUPPORTED_ATTRIBUTE = 'PUBLIC_API_PLATFORM';

private const SUPPORTED_SUBJECTS = ['translation'];

public function __construct(
private readonly RequestStack $requestStack,
private readonly SecurityServiceInterface $securityService
) {
}

protected function supports(string $attribute, mixed $subject): bool
{
return $attribute === self::SUPPORTED_ATTRIBUTE && in_array((string)$subject, self::SUPPORTED_SUBJECTS, true);
}

protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{

$request = $this->getCurrentRequest($this->requestStack);

$authToken = $this->getAuthToken($request);

if ($this->securityService->checkAuthToken($authToken)) {
return true;
}

return $this->voteOnRequest($request, $subject);
}

private function voteOnRequest(Request $request, string $subject): bool
{
return match ($subject) {
'translation' => $this->voteOnTranslation($request->getPayload()),
default => false,
};
}
}
60 changes: 60 additions & 0 deletions src/Service/TranslatorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Service;

use InvalidArgumentException;
use Pimcore\Bundle\StudioApiBundle\Dto\Translation;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @internal
*/
final class TranslatorService implements TranslatorServiceInterface
{
private TranslatorBagInterface $translator;

private const DOMAIN = 'admin';

public function __construct(
TranslatorInterface $translator
) {
if(!$translator instanceof TranslatorBagInterface) {
throw new InvalidArgumentException('Translator must implement TranslatorBagInterface');
}

$this->translator = $translator;
}

public function getAllTranslations(string $locale): Translation
{
return new Translation(
$locale,
$this->translator->getCatalogue($locale)->all(self::DOMAIN)
);
}

public function getTranslationsForKeys(string $locale, array $keys): Translation
{
$translations = [];
foreach ($keys as $key) {
$translations[$key] = $this->translator->getCatalogue($locale)->get($key, self::DOMAIN);
}

return new Translation($locale, $translations);
}
}
29 changes: 29 additions & 0 deletions src/Service/TranslatorServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Service;

use Pimcore\Bundle\StudioApiBundle\Dto\Translation;

/**
* @internal
*/
interface TranslatorServiceInterface
{
public function getAllTranslations(string $locale): Translation;

public function getTranslationsForKeys(string $locale, array $keys): Translation;
}
59 changes: 59 additions & 0 deletions src/State/TranslationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\State;

use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Translation;
use Pimcore\Bundle\StudioApiBundle\Service\TranslatorServiceInterface;

/**
* @internal
*/
final class TranslationProcessor implements ProcessorInterface
{
private const OPERATION_URI_TEMPLATE = '/translations';

public function __construct(
private readonly TranslatorServiceInterface $translatorService
) {
}

public function process(
mixed $data,
Operation $operation,
array $uriVariables = [],
array $context = []
): Translation {
if (
!$operation instanceof Post ||
!$data instanceof Translation ||
$operation->getUriTemplate() !== self::OPERATION_URI_TEMPLATE
) {
// wrong operation
throw new OperationNotFoundException();
}

if(empty($data->getKeys())) {
return $this->translatorService->getAllTranslations($data->getLocale());
}

return $this->translatorService->getTranslationsForKeys($data->getLocale(), $data->getKeys());
}
}
Loading
Loading