FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

does not accept MailerInterface type hinting on Guard Authenticator

Open BernardA opened this issue 6 years ago • 5 comments

Symfony FOSUserBundle versions:

2.1.1 on Symfony 4.0.4

Description of the problem including expected versus actual behavior:

On Guard Authenticator, attempting to include FOS's services for, among other things, re-send e-mail confirmation in case user attempts to login but has still not been enabled on db.

It does accept UserManagerInterface and TokenGeneratorInterface, but for MailerInterface it throws the following error:

Cannot autowire service "app.form_login_authenticator": argument "$mailer" of method "App\Security\FormLoginA
uthenticator::__construct()" references interface "FOS\UserBundle\Mailer\MailerInterface" but no such service
exists. You should maybe alias this interface to one of these existing services: "fos_user.mailer.default",
"fos_user.mailer.twig_swift", "fos_user.mailer.noop". Did you create a class that implements this interface?

The mentioned mailer services ( default, twig_swift, noop ) do not show on the list of available services, if one tries to go that route.

Not sure what is particular about the MailerInterface as compared to the other 2 FOS's interfaces on that script.

Note that the sending of email on the 'regular' ON REGISTRATION SUCCESS event through FOSUserBundle works fine.

Here is the authenticator:

`
namespace App\Security;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use App\Entity\Login;

class FormLoginAuthenticator extends AbstractGuardAuthenticator
{
    const ALLOWED_FAILED_ALL = 100;
    const ALLOWED_FAILED_USER = 10;
    private $userManager;
    private $mailer;
    private $tokenGenerator;
    private $container;

    public function __construct(ContainerInterface $container, UserManagerInterface $userManager, MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator)
{        
    $this->userManager = $userManager;
    $this->container = $container;
    $this->mailer = $mailer;
    $this->tokenGenerator = $tokenGenerator;
}
   ......

`

BernardA avatar Apr 14 '18 17:04 BernardA

BernardA, did you ever figure this out? I'm experiencing the exact same problem, getting the exact same error, but only for MailerInterface.

MichaelBrock avatar Aug 21 '18 13:08 MichaelBrock

Sorry, but I'm afraid not, and as you can see the so called 'friends of symfony' seem to be busy somewhere else.

BernardA avatar Aug 21 '18 17:08 BernardA

I don’t have the code near now, but it seems that there is no alias for MailerInterface. You should inject the Mailer class not MailerInterface or you can add a new alias.

Btw. Friends of Symfony are never busy.

Tomsgu avatar Aug 21 '18 18:08 Tomsgu

Tomsgu, that makes sense and I'm pretty confident that path leads to success. I just ran into a similar situation with a different service.

MichaelBrock avatar Aug 21 '18 19:08 MichaelBrock

Under some circumstances a fos_user.mailer alias is set which refers to the mailer to be used (see https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/DependencyInjection/FOSUserExtension.php#L133). It could make sense to also register an alias here whose name is the FQCN of the MailerInterface. But as long as that is not part of the bundle you do that yourself as @Tomsgu suggested.

xabbuh avatar Aug 21 '18 20:08 xabbuh

Autowiring support for the MailerInterface is provided in the bundle since the 3.0.0 release so this is fixed.

stof avatar Jul 04 '24 09:07 stof