Extends UserManager in Symfony2 with FOSUserBundle
Asked Answered
S

3

5

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;
    }
}
Sleepwalk answered 21/3, 2012 at 14:14 Comment(4)
Have you found a workaround ? I have exactly the same problem. Thanks.Spiros
Not really, i just stop using FOS and I simply use Symfony2 login system.Sleepwalk
Did you try my solution?Athenian
At least, your solution doesn't work for me :'(Spiros
C
18

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);
    }

}
Clintonclintonia answered 20/2, 2013 at 16:36 Comment(1)
You can use the parameter value %fos_user.model.user.class% instead of the hard coded entity class name as the last constructor argument.Sardinian
A
4

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);
    }
}
Athenian answered 21/3, 2012 at 16:34 Comment(0)
S
2

Sorry my English is poor.

May be your custom manager service's arguments is wrong 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 config:

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]
Splitlevel answered 18/10, 2012 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.