Symfony2 estende DefaultAuthenticationSuccessHandler

Voglio modificare il processo di autenticazione predefinito subito dopo il successo dell’autenticazione. Ho creato un servizio che viene chiamato dopo il successo dell’autenticazione e prima del reindirizzamento.

namespace Pkr\BlogUserBundle\Handler; use Doctrine\ORM\EntityManager; use Pkr\BlogUserBundle\Service\Encoder\WpTransitionalEncoder; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; use Symfony\Component\Security\Http\Authentication\Response; class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface { protected $entityManager = null; protected $logger = null; protected $encoder = null; public function __construct(EntityManager $entityManager, LoggerInterface $logger, WpTransitionalEncoder $encoder) { $this->entityManager = $entityManager; $this->logger = $logger; $this->encoder = $encoder; } /** * This is called when an interactive authentication attempt succeeds. This * is called by authentication listeners inheriting from * AbstractAuthenticationListener. * * @param Request $request * @param TokenInterface $token * * @return Response never null */ public function onAuthenticationSuccess(Request $request, TokenInterface $token) { $user = $token->getUser(); $newPass = $request->get('_password'); $user->setUserPassword($this->encoder->encodePassword($newPass, null)); $this->entityManager->persist($user); $this->entityManager->flush(); //do redirect } } 

in services.yml

 services: pkr_blog_user.wp_transitional_encoder: class: "%pkr_blog_user.wp_transitional_encoder.class%" arguments: cost: "%pkr_blog_user.wp_transitional_encoder.cost%" logger: @logger pkr_blog_user.login_success_handler: class: Pkr\BlogUserBundle\Handler\AuthenticationSuccessHandler arguments: entity_manager: @doctrine.orm.entity_manager logger: @logger encoder: @pkr_blog_user.wp_transitional_encoder 

e in security.yml

 firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false secured_area: pattern: ^/ anonymous: ~ form_login: login_path: pkr_blog_admin_login check_path: pkr_blog_admin_login_check success_handler: pkr_blog_user.login_success_handler logout: path: pkr_blog_admin_logout target: / 

Quello che sto cercando di ottenere è semplicemente modificare un po ‘il comportamento predefinito quindi penso di non estendere DefaultAuthenticationSuccessHandler , aggiungere qualcosa a onSuccessHandler() e chiamare parent::onSucessHandler() . Ho provato e il problema è che non ho idea di come aggiungere i parametri di sicurezza (impostati in security.yml) al mio costruttore di classi esteso. DefaultAuthenticationSuccessHandler utilizza HttpUtils e $ options array:

 /** * Constructor. * * @param HttpUtils $httpUtils * @param array $options Options for processing a successful authentication attempt. */ public function __construct(HttpUtils $httpUtils, array $options) { $this->httpUtils = $httpUtils; $this->options = array_merge(array( 'always_use_default_target_path' => false, 'default_target_path' => '/', 'login_path' => '/login', 'target_path_parameter' => '_target_path', 'use_referer' => false, ), $options); } 

Quindi il mio costruttore di class estesa dovrebbe avere il seguente aspetto:

  // class extends DefaultAuthenticationSuccessHandler protected $entityManager = null; protected $logger = null; protected $encoder = null; public function __construct(HttpUtils $httpUtils, array $options, EntityManager $entityManager, LoggerInterface $logger, WpTransitionalEncoder $encoder) { $this->entityManager = $entityManager; $this->logger = $logger; $this->encoder = $encoder; } 

È abbastanza facile aggiungere il servizio HttpUtils al mio services.yml , ma con l’argomento delle opzioni?

 services: pkr_blog_user.wp_transitional_encoder: class: "%pkr_blog_user.wp_transitional_encoder.class%" arguments: cost: "%pkr_blog_user.wp_transitional_encoder.cost%" logger: @logger pkr_blog_user.login_success_handler: class: Pkr\BlogUserBundle\Handler\AuthenticationSuccessHandler arguments: httputils: @security.http_utils options: [] #WHAT TO ADD HERE ? entity_manager: @doctrine.orm.entity_manager logger: @logger encoder: @pkr_blog_user.wp_transitional_encoder