-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[UX] Allow to redirect logged-in user on any routes and add a redirection for registration page #15418
Open
Jibbarth
wants to merge
10
commits into
Sylius:1.14
Choose a base branch
from
Jibbarth:feature/redirect-logged-in-user-when-visit-registration-page
base: 1.14
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−29
Open
[UX] Allow to redirect logged-in user on any routes and add a redirection for registration page #15418
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d214510
[UX][Shop] Redirect to dashboard a logged-in user that reach register…
Jibbarth 586e3f0
Switch to a less strict "role"
Jibbarth 290b57b
Switch also to less strict role in SecurityController to prevent reac…
Jibbarth 4182590
Fix behat tests
Jibbarth d9d8195
Use Property promotion constructor, remove EventSubscriberInterface
Jibbarth d8d19b1
Behat - stick to customer_account tag for visit registration page
Jibbarth 6d3e16c
Move Listener in UiBundle as it can concern both Admin and Shop routes
Jibbarth a616bb0
Delegate redirection to Event listener instead of having it in Securi…
Jibbarth 1d72f12
Behat - Assert we have a redirection to dashboard when try to reach a…
Jibbarth b6f21d7
Spec: add AlreadyLoggedInUserRedirectionListenerSpec
Jibbarth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@admin_dashboard | ||
Feature: Redirect when already signed in | ||
In order to be aware that I am already logged in | ||
As an Administrator | ||
I want to be redirected to the administration dashboard by using when accessing the login page | ||
|
||
Background: | ||
Given the store operates on a single channel in "United States" | ||
And I am logged in as an administrator | ||
|
||
@ui @no-api | ||
Scenario: Trying to access login page as logged in administrator | ||
When I want to log in | ||
Then I should be redirected to the administration dashboard | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Sylius/Bundle/UiBundle/EventListener/AlreadyLoggedInUserRedirectionListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\UiBundle\EventListener; | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
|
||
/** | ||
* Allow to redirect already logged-in user to a specific route. | ||
* | ||
* To do so, add in your route configuration a 'logged_in_route' attribute under the '_sylius' key. | ||
* You can provide directly the route name or an array with 'name' and 'parameters' keys. | ||
*/ | ||
final class AlreadyLoggedInUserRedirectionListener | ||
{ | ||
public function __construct( | ||
private AuthorizationCheckerInterface $authorizationChecker, | ||
private RequestStack $requestStack, | ||
private RouterInterface $router | ||
) { | ||
} | ||
|
||
public function handleAlreadyConnectedUser(RequestEvent $event): void | ||
{ | ||
if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { | ||
return; | ||
} | ||
|
||
$alreadyLoggedInRedirectRoute = $this->requestStack->getMainRequest()->attributes->get('_sylius')['logged_in_route'] ?? null; | ||
if (null === $alreadyLoggedInRedirectRoute) { | ||
return; | ||
} elseif (is_string($alreadyLoggedInRedirectRoute)) { | ||
// case logged_in_route: 'app_route' | ||
$event->setResponse(new RedirectResponse($this->router->generate($alreadyLoggedInRedirectRoute))); | ||
return; | ||
} | ||
|
||
// case logged_in_route: | ||
// name: 'app_route' | ||
// parameters: | ||
// param1: 'value1' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tell me if such comment are not needed |
||
$event->setResponse(new RedirectResponse($this->router->generate( | ||
$alreadyLoggedInRedirectRoute['name'] ?? throw new \InvalidArgumentException('The "name" key must be set for the "logged_in_route" attribute.'), | ||
$alreadyLoggedInRedirectRoute['parameters'] ?? [] | ||
))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Sylius/Bundle/UiBundle/Resources/config/services/listeners.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
|
||
This file is part of the Sylius package. | ||
|
||
(c) Sylius Sp. z o.o. | ||
|
||
For the full copyright and license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
|
||
--> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<defaults public="true" /> | ||
<service id="sylius.listener.already_logged_in_user_redirection" class="Sylius\Bundle\UiBundle\EventListener\AlreadyLoggedInUserRedirectionListener"> | ||
<argument type="service" id="security.authorization_checker"/> | ||
<argument type="service" id="request_stack" /> | ||
<argument type="service" id="router" /> | ||
<tag name="kernel.event_listener" event="kernel.request" method="handleAlreadyConnectedUser" /> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/Sylius/Bundle/UiBundle/spec/EventListener/AlreadyLoggedInUserRedirectionListenerSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\UiBundle\EventListener; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
|
||
|
||
final class AlreadyLoggedInUserRedirectionListenerSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
AuthorizationCheckerInterface $authorizationChecker, | ||
RequestStack $requestStack, | ||
RouterInterface $router | ||
): void { | ||
$this->beConstructedWith($authorizationChecker, $requestStack, $router); | ||
} | ||
|
||
function it_add_response_to_event_when_user_is_already_logged_in( | ||
RequestEvent $event, | ||
Request $request, | ||
ParameterBag $requestAttributes, | ||
AuthorizationCheckerInterface $authorizationChecker, | ||
RequestStack $requestStack, | ||
RouterInterface $router | ||
): void { | ||
$authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')->willReturn(true); | ||
|
||
$requestStack->getMainRequest()->willReturn($request); | ||
$request->attributes = $requestAttributes; | ||
$requestAttributes->get('_sylius')->willReturn(['logged_in_route' => 'app_route']); | ||
|
||
$router->generate('app_route')->willReturn('/app_route'); | ||
|
||
$event->setResponse(new RedirectResponse('/app_route'))->shouldBeCalled(); | ||
|
||
$this->handleAlreadyConnectedUser($event); | ||
} | ||
|
||
function it_add_response_to_event_when_user_is_already_logged_in_with_parameters( | ||
RequestEvent $event, | ||
Request $request, | ||
ParameterBag $requestAttributes, | ||
AuthorizationCheckerInterface $authorizationChecker, | ||
RequestStack $requestStack, | ||
RouterInterface $router | ||
): void { | ||
$authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')->willReturn(true); | ||
|
||
$requestStack->getMainRequest()->willReturn($request); | ||
$request->attributes = $requestAttributes; | ||
$requestAttributes->get('_sylius')->willReturn([ | ||
'logged_in_route' => [ | ||
'name' => 'app_route', | ||
'parameters' => ['param1' => 'value1'] | ||
] | ||
]); | ||
|
||
$router->generate('app_route', ['param1' => 'value1'])->willReturn('/app_route?param1=value1'); | ||
|
||
$event->setResponse(new RedirectResponse('/app_route?param1=value1'))->shouldBeCalled(); | ||
|
||
$this->handleAlreadyConnectedUser($event); | ||
} | ||
|
||
function it_does_not_alter_event_when_not_logged_in( | ||
RequestEvent $event, | ||
AuthorizationCheckerInterface $authorizationChecker | ||
): void { | ||
$authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')->willReturn(false); | ||
|
||
$event->setResponse(new RedirectResponse('/app_route'))->shouldNotBeCalled(); | ||
|
||
$this->handleAlreadyConnectedUser($event); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tell me if we should gather PageInterface together in order's declaration