Symfony 2 & Fosuserbundle: autenticare l'utente dopo aver reimpostato la password
-
13-12-2019 - |
Domanda
Durante il reimpostazione del reimpostazione di FosuserBundle sovrascrittura, è presente una chiamata di funzione su metodo "AuthenteDateguser" (linea 104):
....
$this->authenticateUser($user);
....
.
Il mio problema è che ho già sovrascritto il gestore di autenticazione Symfony e ho già la mia logica quando un utente accede.
Modifica Ecco il mio gestore di autenticazione:
<?php
/* ... all includes ... */
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, LogoutSuccessHandlerInterface
{
private $router;
private $container;
public function __construct(Router $router, ContainerInterface $container)
{
$this->router = $router;
$this->container = $container;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
// retrieve user and session id
$user = $token->getUser();
/* ... here I do things in database when logging in, and dont want to write it again and again ... */
// prepare redirection URL
if($targetPath = $request->getSession()->get('_security.target_path')) {
$url = $targetPath;
}
else {
$url = $this->router->generate('my_route');
}
return new RedirectResponse($url);
}
}
.
Allora, come potrei chiamare il metodo "Onauthenticationsuccess" dal mio gestore di autenticazione nel resetController? Per evitare di riscrivere lo stesso codice ...
Grazie per il tuo aiuto!
Aurel
Soluzione
Dovresti chiamare il tuo metodo OnauthenticationSuccess lo caricando come servizio.Nel tuo config.yml:
authentication_handler:
class: Acme\Bundle\Service\AuthenticationHandler
arguments:
container: "@service_container"
.
E poi, chiamalo nella funzione AuthenticateSer:
protected function authenticateUser(UserInterface $user) {
try {
$this->container->get('fos_user.user_checker')->checkPostAuth($user);
} catch (AccountStatusException $e) {
// Don't authenticate locked, disabled or expired users
return;
}
$providerKey = $this->container->getParameter('fos_user.firewall_name');
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->container->get('security.context')->setToken($token);
$request = $this->container->get('request');
$this->container->get('authentication_handler')->onAuthenticationSuccess($request, $token);
}
.
Questo fa il trucco e passa attraverso il tuo gestore di autenticità personalizzato. Ulteriori informazioni .