Skip to content

Commit

Permalink
Intercept pages for already logged in users
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jan 11, 2020
1 parent 967790b commit 888ae28
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
100 changes: 100 additions & 0 deletions src/EventListener/AlreadyLoggedinListener.php
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);
}
}
7 changes: 6 additions & 1 deletion src/Resources/config/listeners.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
<services>
<service id="Nucleos\ProfileBundle\EventListener\AuthenticationListener">
<tag name="kernel.event_subscriber"/>
<argument type="service" id="nucleos_profile.security.login_manager"/>
<argument type="service" id="nucleos_user.security.login_manager"/>
<argument>%nucleos_profile.firewall_name%</argument>
</service>
<service id="Nucleos\ProfileBundle\EventListener\AlreadyLoggedinListener">
<tag name="kernel.event_subscriber"/>
<argument type="service" id="security.helper"/>
<argument type="service" id="router"/>
</service>
</services>
</container>

0 comments on commit 888ae28

Please sign in to comment.