Dimitri Danilov
Dimitri Danilov

Reputation: 1352

Override a complicated class with multiple parameters

I am trying to override the class UsernamePasswordFormAuthenticationListener.

parameters:
    security.authentication.listener.form.class: AppBundle\Listener\LoginFormListener

class LoginFormListener extends UsernamePasswordFormAuthenticationListener
{
    /**
    * {@inheritdoc}
    */
    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, CsrfProviderInterface $csrfProvider = null)
    {
        parent::__construct($securityContext, $authenticationManager, $sessionStrategy, $httpUtils, $providerKey, $successHandler, $failureHandler, $options, $logger, $dispatcher, $csrfProvider);
    }

    protected function attemptAuthentication(Request $request)
    {

        if (null !== $this->csrfTokenManager) {
            $csrfToken = $request->get($this->options['csrf_parameter'], null, true);

            if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
                throw new InvalidCsrfTokenException('Invalid CSRF token.');
            }
        }

        if ($this->options['post_only']) {
            $username = trim($request->request->get($this->options['username_parameter'], null, true));
            $password = $request->request->get($this->options['password_parameter'], null, true);
        } else {
            $username = trim($request->get($this->options['username_parameter'], null, true));
            $password = $request->get($this->options['password_parameter'], null, true);
        }

        $request->getSession()->set(Security::LAST_USERNAME, $username);

        $apiRequest = new ApiRequest();
        $apiRequest->addMethod('login', array('email' => $username, 'password' => $password));
        $response = $apiRequest->sendRequest();
        dump($response);
        exit;
    }
}

But when I execute it, I have this error :

Catchable Fatal Error: Argument 1 passed to AppBundle\Listener\LoginFormListener::__construct() must implement interface Symfony\Component\Security\Core\SecurityContextInterface, instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage given, called in /Users/dimitri/Sites/rennes/app/cache/dev/appDevDebugProjectContainer.php on line 4039 and defined

Any idea how I can make this work ?

Upvotes: 2

Views: 138

Answers (1)

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10890

You can simply change SecurityContextInterface type hint to TokenStorageInterface in your class and all should work fine - this class service has been changed recently (deprecated in 2.7) , so your example code might be outdated.

Check blog post entry to get more info about SecurityContextInterface vs TokenStorageInterface

Upvotes: 2

Related Questions