-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intercept pages for already logged in users
- Loading branch information
Showing
2 changed files
with
106 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NucleosProfileBundle package. | ||
* | ||
* (c) Christian Gripp <mail@core23.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\ProfileBundle\EventListener; | ||
|
||
use Nucleos\ProfileBundle\Event\GetResponseRegistrationEvent; | ||
use Nucleos\ProfileBundle\NucleosProfileEvents; | ||
use Nucleos\UserBundle\Event\GetResponseLoginEvent; | ||
use Nucleos\UserBundle\Event\GetResponseUserEvent; | ||
use Nucleos\UserBundle\Model\UserInterface; | ||
use Nucleos\UserBundle\NucleosUserEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
|
||
final class AlreadyLoggedinListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var Security | ||
*/ | ||
private $security; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* AlreadyLoggedinListener constructor. | ||
*/ | ||
public function __construct(Security $security, RouterInterface $router) | ||
{ | ||
$this->security = $security; | ||
$this->router = $router; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
NucleosProfileEvents::REGISTRATION_INITIALIZE => 'checkRegistration', | ||
NucleosUserEvents::SECURITY_LOGIN_INITIALIZE => 'checkLogin', | ||
NucleosUserEvents::SECURITY_LOGIN_COMPLETED => 'redirectToProfile', | ||
]; | ||
} | ||
|
||
public function redirectToProfile(GetResponseUserEvent $event): void | ||
{ | ||
$event->setResponse(new RedirectResponse($this->router->generate('nucleos_profile_profile_show'))); | ||
} | ||
|
||
public function checkRegistration(GetResponseRegistrationEvent $event): void | ||
{ | ||
$user = $this->security->getUser(); | ||
|
||
if (!$user instanceof UserInterface) { | ||
return; | ||
} | ||
|
||
$event->setResponse($this->getRedirect($event->getRequest())); | ||
} | ||
|
||
public function checkLogin(GetResponseLoginEvent $event): void | ||
{ | ||
$user = $this->security->getUser(); | ||
|
||
if (!$user instanceof UserInterface) { | ||
return; | ||
} | ||
|
||
$event->setResponse($this->getRedirect($event->getRequest())); | ||
} | ||
|
||
private function getRedirect(Request $request): RedirectResponse | ||
{ | ||
$url = $request->server->get('HTTP_REFERER'); | ||
|
||
if ($request->getUri() === $url) { | ||
$url = null; | ||
} | ||
|
||
if (null === $url) { | ||
$url = $this->router->generate('nucleos_profile_profile_show'); | ||
} | ||
|
||
return new RedirectResponse($url); | ||
} | ||
} |
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