Gabriel Muñumel
Gabriel Muñumel

Reputation: 1876

Creating an admin user using datafixtures and fosuserbundle

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  

Upvotes: 10

Views: 13958

Answers (5)

laviku
laviku

Reputation: 541

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

Upvotes: 0

Kaizoku Gambare
Kaizoku Gambare

Reputation: 3413

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

Upvotes: 0

Anil
Anil

Reputation: 21910

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! :)

Upvotes: 27

cmgriffing
cmgriffing

Reputation: 117

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.

Upvotes: 2

Mun Mun Das
Mun Mun Das

Reputation: 15002

Follow this section of the documentation.

Upvotes: 3

Related Questions