Carrie Kendall ha lavorato, grazie!
Ecco piena attuazione in Symfony:
services.yml:
login_handler:
class: Project\BaseBundle\Service\loginHandler
arguments: ['@router', '@doctrine.orm.entity_manager', '@service_container']
e in Project \ BaseBundle \ Service \ loginHandler:
namespace Project\BaseBundle\Service;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\EntityManager;
class loginHandler implements AuthenticationSuccessHandlerInterface {
private $router;
private $container;
private static $key;
public function __construct(RouterInterface $router, EntityManager $em, $container) {
self::$key = '_security.secured_area.target_path';
$this->router = $router;
$this->em = $em;
$this->session = $container->get('session');
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
$user_entity = $token->getUser();
if(!$user_entity->getChangePassword()) {
$route = $this->router->generate('BaseBundle_home_page');
} else {
$this->session->getFlashBag()->add('error', 'Your password must be changed now');
$route = $this->router->generate('BaseBundle_account_page');
}
//check if the referer session key has been set
if ($this->session->has(self::$key)) {
//set the url based on the link they were trying to access before being authenticated
$route = $this->session->get(self::$key);
//remove the session key
$this->session->remove(self::$key);
//if the referer key was never set, redirect to a default route
} else{
$url = $this->generateUrl('BaseBundle_home_page');
return new RedirectResponse($route);
}
return new RedirectResponse($route);
}
}
fonte
2013-11-14 15:08:20
Hai qualche consiglio per questo caso? Qui, abbiamo bisogno del referrer del referrer ... –
@aminejallouli probabilmente starai meglio a conservare quell'informazione da qualche parte (ad esempio la sessione) e ad accedervi quando ne hai bisogno. –
Ecco come ho fatto! Funziona ora! :) Grazie –