Created
September 18, 2014 19:51
-
-
Save ftdysa/835526b3c73617afed74 to your computer and use it in GitHub Desktop.
Custom login success handler
This file contains hidden or 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
<?php | |
namespace NationalNet\MynatnetBundle\Security\Authentication\Handler; | |
use NationalNet\MynatnetBundle\Manager\UserManager; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; | |
use Symfony\Component\Security\Http\HttpUtils; | |
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface { | |
/** | |
* @var \Symfony\Component\Security\Http\HttpUtils | |
*/ | |
private $httpUtils; | |
/** | |
* @var \NationalNet\MynatnetBundle\Manager\UserManager | |
*/ | |
private $userManager; | |
private $options; | |
/** | |
* @param HttpUtils $httpUtils | |
* @param UserManager $userManager | |
* @param array $options | |
*/ | |
public function __construct(HttpUtils $httpUtils, UserManager $userManager) { | |
$this->httpUtils = $httpUtils; | |
$this->userManager = $userManager; | |
// For now this is hardcoded to match the firewall map options | |
// https://github.com/symfony/symfony/issues/11926 | |
$this->options = array( | |
'always_use_default_target_path' => false, | |
'default_target_path' => '/', | |
'login_path' => '/login', | |
'target_path_parameter' => '_target_path', | |
'use_referer' => false, | |
); | |
} | |
public function onAuthenticationSuccess(Request $request, TokenInterface $token) { | |
/* @var $user \Entity\Core\User */ | |
$user = $token->getUser(); | |
if ($user->getLoginLast() === null) { | |
$response = new RedirectResponse($this->httpUtils->generateUri($request, 'new_account_wizard_start')); | |
} else { | |
// redirect the user to where they were before the login process begun. | |
$referer = $request->headers->get('referer'); | |
$loginPathUrl = $this->httpUtils->generateUri($request, $this->options['login_path']); | |
if ($referer !== $loginPathUrl) { | |
$response = new RedirectResponse($referer); | |
} else { | |
$response = new RedirectResponse($this->httpUtils->generateUri($request, $this->options['default_target_path'])); | |
} | |
} | |
$user->setLoginLast(new \DateTime()); | |
$this->userManager->update($user); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment