10
votes

I'm trying to create a new User Admin from a fixture. I'm using FOSUserBundle and Symfony2.

$userManager = $this->container->get('fos_user.user_manager');

//$userAdmin = $userManager->createUser();

$userAdmin = new UserAdmin();
$userAdmin->setUsername('francis');
$userAdmin->setEmail('[email protected]');
$userAdmin->setEnabled(true);
$userAdmin->setRoles(array('ROLE_ADMIN'));

$userManager->updateUser($userAdmin, true);

I'm always getting this error:

[ErrorException]                                         
Notice: Undefined property:     
INCES\ComedorBundle\DataFixtures\ORM\LoadUserAdminData::$container in 
/public_html/Symfony/src/INCES/ComedorBundle/DataFixtures/ORM/LoadUserAdminData.php line 16  
5
Why don't you use fos:user:promote ? - Jérôme Boé
I would like to create an admin user the first time i run my application on the production server. If i know well promote works to change the role for a already created user, but not exactly what i want. - Gabriel Muñumel

5 Answers

27
votes

This worked for me (i'm also using FOSUserBundle):

// Change the namespace!
namespace Acme\DemoBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    //.. $container declaration & setter

    public function load(ObjectManager $manager)
    {
        // Get our userManager, you must implement `ContainerAwareInterface`
        $userManager = $this->container->get('fos_user.user_manager');

        // Create our user and set details
        $user = $userManager->createUser();
        $user->setUsername('username');
        $user->setEmail('[email protected]');
        $user->setPlainPassword('password');
        //$user->setPassword('3NCRYPT3D-V3R51ON');
        $user->setEnabled(true);
        $user->setRoles(array('ROLE_ADMIN'));

        // Update the user
        $userManager->updateUser($user, true);
    }
}

Hope this helps someone! :)

3
votes

Follow this section of the documentation.

2
votes

The Error is because the $container is currently undefined. To fix this, add the ContainerAwareInterface to your class definition.

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    ...
}

This will not completely get you what you want though, since you are creating the user without the UserManager. Instead you should be using the line you have commented out.

It seems to me that you don't need the UserAdmin class. The admin users should be a subset of the User distinguished only by the roles that they have.

You should use the UserManager to create a User(not UserAdmin) and set the roles. If you need to keep an index of all admin users, a MySQL VIEW could accomplish this, or you could create your own custom "cache" table and use Doctrine Listeners to update it when needed.

This question is fairly old, so I am guessing you found the answer or at least a workaround. Would you please provide that? It is ok to answer your own questions.

0
votes

This is the updated version for SF3+ This answer is based on Anil and Mun Mun Das answers.

namespace App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use FOS\UserBundle\Model\UserManagerInterface;

    class AdminFixtures extends Fixture
    {

        private $userManager;

        public function __construct(UserManagerInterface $userManager)
        {
            $this->userManager = $userManager;
        }

        public function load(ObjectManager $manager)
        {
            $user = $this->userManager->createUser();
            $user->setUsername('username');
            $user->setEmail('[email protected]');
            $user->setPlainPassword('password');
            $user->setEnabled(true);
            $user->setRoles(array('ROLE_ADMIN'));
            $this->userManager->updateUser($user);
        }
    }
0
votes

This is what I did using Symfony 4, SonataAdmin and FosUserBundle

namespace App\DataFixtures;

use App\Application\Sonata\UserBundle\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class UserFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setUsername('yourusername');
        $user->setEmail('[email protected]');
        $user->setEnabled(true);
        $user->setPlainPassword('yourpassword');
        $user->setRoles(array('ROLE_SUPER_ADMIN'));
        $manager->persist($user);

        $manager->flush();
    }
}