Extends UserManager in Symfony2 with FOSUserBundle

I tried to expand UserManager FOS according to this link

My service is correctly detected, but I have an error that I cannot solve:

ErrorException: Catchable Fatal Error: Argument 1 passed to FOS\UserBundle\Entity\UserManager::__construct() must be an instance of Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface, none given, called in 

MyUserManager:

namespace ChoupsCommerce\UserBundle\Model;

use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class MyUserManager extends UserManager
{
    public function loadUserByUsername($username)
    {
        $user = $this->findUserByUsernameOrEmail($username);

        if (!$user) {
            throw new UsernameNotFoundException(sprintf('No user with name "%s" was found.', $username));
        }

        return $user;
    }
}
+5
source share
3 answers

I had the same problem, here is my solution (basically a combination of the first two answers):

Configure the service in config.yml, do not forget the arguments

services:
    custom_user_manager:
        class: Acme\UserBundle\Model\CustomUserManager
        arguments: [@security.encoder_factory, @fos_user.util.username_canonicalizer, @fos_user.util.email_canonicalizer, @fos_user.entity_manager, Acme\UserBundle\Entity\User]

Then connect the service to the FOS user_manager (also in config.yml):

fos_user:
    service:
        user_manager: custom_user_manager

In the CustomUserManager answer, the build method, e.g. gilden, said:

class CustomUserManager extends UserManager{

    public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, ObjectManager $om, $class)
    {
        parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer, $om, $class);
    }

}
+18
source

You need to add the constructor ( __construct) method and pass the necessary arguments to the base class:

namespace ChoupsCommerce\UserBundle\Model;

use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use FOS\UserBundle\Util\CanonicalizerInterface;

class MyUserManager extends UserManager
{
    public function __construct(
        EncoderFactoryInterface $encoderFactory,
        CanonicalizerInterface $usernameCanonicalizer,
        CanonicalizerInterface $emailCanonicalizer,
        EntityManager $em,
        $class
    ) {
        parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer, $em, $class);
    }
}
+4
source

Sorry, my English is bad.

Maybe your custom manager service arguments are incorrect because it says

Argument 1 passed to FOS\UserBundle\Entity\UserManager::__construct() must be an instance of Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface, none given, called in

Here is my configuration:

iep_core.iep_user_manager:   
     class: Iep\CoreBundle\Service\IepUserManager
     arguments: [@security.encoder_factory, @fos_user.util.username_canonicalizer, @fos_user.util.email_canonicalizer, @fos_user.entity_manager, Iep\CoreBundle\Entity\User]
+2
source

All Articles