Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extends UserManager in Symfony2 with FOSUserBundle

I tried to extend the UserManager of FOS according to this link

My service is correctly detected but i have an error i can't resolved :

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;
    }
}
like image 482
Wifsimster Avatar asked Mar 21 '12 14:03

Wifsimster


2 Answers

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

Set up service in config.yml, don't 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 service to FOS user_manager (also in config.yml):

fos_user:
    service:
        user_manager: custom_user_manager

In the CustomUserManager recall the construct-method, like 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);
    }

}
like image 200
chris.berlin Avatar answered Sep 20 '22 13:09

chris.berlin


You need to add the constructor method (__construct) and pass the required arguments down 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);
    }
}
like image 25
kgilden Avatar answered Sep 20 '22 13:09

kgilden