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

[Task] Update subscribers and docs #499

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/event_subscribers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ services:
#Subscriber
Pimcore\Bundle\StudioBackendBundle\EventSubscriber\CorsSubscriber:
tags: [ 'kernel.event_subscriber' ]
arguments: ['%pimcore_studio_backend.url_prefix%']

Pimcore\Bundle\StudioBackendBundle\EventSubscriber\ApiExceptionSubscriber:
tags: [ 'kernel.event_subscriber' ]
arguments: ["%kernel.environment%"]
arguments: ["%kernel.environment%", '%pimcore_studio_backend.url_prefix%']
25 changes: 22 additions & 3 deletions doc/00_Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ composer require pimcore/studio-backend-bundle
2) Enable Firewall settings

To enable the firewall settings in your project, add the following configuration to your `config/packages/security.yaml` file:

Keep in mind that the prefix part pimcore-studio/api can be changed to any other value in the config.
You need to adapt your access_control settings accordingly.
```yaml
security:
firewalls:
pimcore_studio: '%pimcore_studio_backend.firewall_settings%'
access_control:
- { path: ^/studio/api/(docs|docs.json|translations)$, roles: PUBLIC_ACCESS }
- { path: ^/studio, roles: ROLE_PIMCORE_USER }
- { path: ^/pimcore-studio/api/(docs|docs.json|translations)$, roles: PUBLIC_ACCESS }
- { path: ^/pimcore-studio, roles: ROLE_PIMCORE_USER }
```

3) Make sure the bundle is enabled in the `config/bundles.php` file. The following lines should be added:
Expand Down Expand Up @@ -110,3 +111,21 @@ pimcore_studio_backend:
# Optional configuration
cookie_lifetime: 3600
```

## Changing the prefix of the Studio Backend
It is possible to change the route where you can reach the API. By default, the route is `/pimcore-studio/api/`.
If you want to change the prefix, you can do so by changing the configuration like the following:
Keep in mind that you need to update your access_control settings accordingly.
```yaml
pimcore_studio_backend:
url_prefix: '/your-prefix/api/'
```

```yaml
security:
firewalls:
pimcore_studio: '%pimcore_studio_backend.firewall_settings%'
mattamon marked this conversation as resolved.
Show resolved Hide resolved
access_control:
- { path: ^/your-prefix/api/(docs|docs.json|translations)$, roles: PUBLIC_ACCESS }
- { path: ^/your-prefix, roles: ROLE_PIMCORE_USER }
```
7 changes: 5 additions & 2 deletions src/DependencyInjection/PimcoreStudioBackendExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ public function load(array $configs, ContainerBuilder $container): void

$this->checkValidOpenApiScanPaths($config['open_api_scan_paths']);
$this->checkValidUrlPrefix($config['url_prefix']);

$definition = $container->getDefinition(OpenApiServiceInterface::class);
$definition->setArgument('$routePrefix', rtrim($config['url_prefix'], '/'));
$definition->setArgument('$openApiScanPaths', $config['open_api_scan_paths']);
$definition->setArguments([
'$routePrefix' => $config['url_prefix'],
'$openApiScanPaths' => $config['open_api_scan_paths'],
]);

$definition = $container->getDefinition(CorsSubscriber::class);
$definition->setArgument('$allowedHosts', $config['allowed_hosts_for_cors']);
Expand Down
6 changes: 3 additions & 3 deletions src/EventSubscriber/ApiExceptionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
/**
* @internal
*/
final class ApiExceptionSubscriber implements EventSubscriberInterface
final readonly class ApiExceptionSubscriber implements EventSubscriberInterface
{
use StudioBackendPathTrait;

public function __construct(private readonly string $environment)
public function __construct(private string $environment, private string $urlPrefix)
{
}

Expand All @@ -46,7 +46,7 @@ public function onKernelException(ExceptionEvent $event): void
$exception = $event->getThrowable();
$request = $event->getRequest();

if (!$this->isStudioBackendPath($request->getPathInfo())) {
if (!$this->isStudioBackendPath($request->getPathInfo(), $this->urlPrefix)) {
return;
}

Expand Down
13 changes: 7 additions & 6 deletions src/EventSubscriber/CorsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
use Symfony\Component\Routing\RouterInterface;
use function in_array;

final class CorsSubscriber implements EventSubscriberInterface
final readonly class CorsSubscriber implements EventSubscriberInterface
{
use StudioBackendPathTrait;

public function __construct(
private readonly RouterInterface $router,
private readonly UrlMatcherInterface $urlMatcher,
private readonly array $allowedHosts = []
private string $urlPrefix,
private RouterInterface $router,
private UrlMatcherInterface $urlMatcher,
private array $allowedHosts = []
) {
}

Expand All @@ -55,7 +56,7 @@ public function onKernelRequest(RequestEvent $event): void

$request = $event->getRequest();

if (!$this->isStudioBackendPath($request->getPathInfo())) {
if (!$this->isStudioBackendPath($request->getPathInfo(), $this->urlPrefix)) {
return;
}

Expand Down Expand Up @@ -90,7 +91,7 @@ public function onKernelResponse(ResponseEvent $event): void
{
$request = $event->getRequest();

if (!$this->isStudioBackendPath($request->getPathInfo())) {
if (!$this->isStudioBackendPath($request->getPathInfo(), $this->urlPrefix)) {
return;
}
// Run CORS check in here to ensure domain is in the system
Expand Down
6 changes: 2 additions & 4 deletions src/Util/Trait/StudioBackendPathTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

namespace Pimcore\Bundle\StudioBackendBundle\Util\Trait;

use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;

/**
* @internal
*/
trait StudioBackendPathTrait
{
private function isStudioBackendPath(string $path): bool
private function isStudioBackendPath(string $path, string $urlPrefix): bool
{
return str_starts_with($path, AbstractApiController::PREFIX);
return str_starts_with($path, $urlPrefix);
}
}
Loading