Skip to content

[Routing] Allow using services in the route condition #44405

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* Deprecate the `reset_on_message` config option. It can be set to `true` only and does nothing now
* Add `trust_x_sendfile_type_header` option
* Add support for first-class callable route controller in `MicroKernelTrait`
* Add tag `routing.condition_service` to autoconfigure routing condition services.

6.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class UnusedTagsPass implements CompilerPassInterface
'routing.expression_language_provider',
'routing.loader',
'routing.route_loader',
'routing.condition_service',
'security.authenticator.login_linker',
'security.expression_language_provider',
'security.remember_me_aware',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Bridge\Twig\Extension\CsrfExtension;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\MercureBundle\MercureBundle;
Expand Down Expand Up @@ -666,6 +667,10 @@ public function load(array $configs, ContainerBuilder $container)
'kernel.locale_aware',
'kernel.reset',
]);

$container->registerAttributeForAutoconfiguration(AsRoutingConditionService::class, static function (ChildDefinition $definition, AsRoutingConditionService $attribute): void {
$definition->addTag('routing.condition_service', (array) $attribute);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
])
->tag('routing.expression_language_function', ['function' => 'env'])

->set('container.get_routing_condition_service', \Closure::class)
->public()
->factory([\Closure::class, 'fromCallable'])
->args([
[tagged_locator('routing.condition_service', 'alias'), 'get'],
])
->tag('routing.expression_language_function', ['function' => 'service'])

// inherit from this service to lazily access env vars
->set('container.env', LazyString::class)
->abstract()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Routing\Attribute;

/**
* Service tag to autoconfigure routing condition services.
*
* You can tag a service:
*
* #[AsRoutingConditionService('foo')]
* class SomeFooService
* {
* public function bar(): bool
* {
* // ...
* }
* }
*
* Then you can use the tagged service in the routing condition:
*
* class PageController
* {
* #[Route('/page', condition: "service('foo').bar()")]
* public function page(): Response
* {
* // ...
* }
* }
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class AsRoutingConditionService
{
public function __construct(
public ?string $alias = null,
public int $priority = 0,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController
{
#[Route(
path: '/allowed/manually-tagged',
condition: 'service("manually_tagged").giveMeTrue()',
)]
public function allowedByManuallyTagged(): Response
{
return new Response();
}

#[Route(
path: '/allowed/auto-configured',
condition: 'service("auto_configured").flip(false)',
)]
public function allowedByAutoConfigured(): Response
{
return new Response();
}

#[Route(
path: '/allowed/auto-configured-non-aliased',
condition: 'service("auto_configured_non_aliased").alwaysTrue()',
)]
public function allowedByAutoConfiguredNonAliased(): Response
{
return new Response();
}

#[Route(
path: '/denied/manually-tagged',
condition: 'service("manually_tagged").giveMeFalse()',
)]
public function deniedByManuallyTagged(): Response
{
return new Response();
}

#[Route(
path: '/denied/auto-configured',
condition: 'service("auto_configured").flip(true)',
)]
public function deniedByAutoConfigured(): Response
{
return new Response();
}

#[Route(
path: '/denied/auto-configured-non-aliased',
condition: 'service("auto_configured_non_aliased").alwaysFalse()',
)]
public function deniedByAutoConfiguredNonAliased(): Response
{
return new Response();
}

#[Route(
path: '/denied/overridden',
condition: 'service("foo").isAllowed()',
)]
public function deniedByOverriddenAlias(): Response
{
return new Response();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class RoutingConditionServiceBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service;

use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;

#[AsRoutingConditionService]
class AutoConfiguredNonAliasedService
{
public function alwaysFalse(): bool
{
return false;
}

public function alwaysTrue(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service;

use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;

#[AsRoutingConditionService('auto_configured')]
class AutoConfiguredService
{
public function flip(bool $value): bool
{
return !$value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service;

use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;

#[AsRoutingConditionService('foo')]
class FooOriginalService
{
public function isAllowed(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service;

use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService;

#[AsRoutingConditionService(alias: 'foo', priority: -1)]
class FooReplacementService
{
public function isAllowed(): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service;

class ManuallyTaggedService
{
public function giveMeTrue(): bool
{
return true;
}

public function giveMeFalse(): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

class RoutingConditionServiceTest extends AbstractWebTestCase
{
/**
* @dataProvider provideRoutes
*/
public function testCondition(int $code, string $path)
{
$client = static::createClient(['test_case' => 'RoutingConditionService']);

$client->request('GET', $path);
$this->assertSame($code, $client->getResponse()->getStatusCode());
}

public function provideRoutes(): iterable
{
yield 'allowed by an autoconfigured service' => [
200,
'/allowed/manually-tagged',
];

yield 'allowed by a manually tagged service' => [
200,
'/allowed/auto-configured',
];

yield 'allowed by a manually tagged non aliased service' => [
200,
'/allowed/auto-configured-non-aliased',
];

yield 'denied by an autoconfigured service' => [
404,
'/denied/manually-tagged',
];

yield 'denied by a manually tagged service' => [
404,
'/denied/auto-configured',
];

yield 'denied by a manually tagged non aliased service' => [
404,
'/denied/auto-configured-non-aliased',
];

yield 'denied by an overridden service' => [
404,
'/denied/overridden',
];
}
}
Loading