Creating an admin user using datafixtures and fosuserbundle
Asked Answered
I

5

10

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  
Intussusception answered 4/8, 2012 at 18:35 Comment(2)
Why don't you use fos:user:promote ?Postilion
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.Pebble
B
27

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

Brittneybrittni answered 1/6, 2014 at 21:25 Comment(2)
Cool its work for me too :) Tnx. github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/… - This is the userManager documentation if someone want to do something diffrent :)Hallerson
This works but how can we load a user from database if we are going to make an '--append' loading and we dont wana create a new user but use an existing one.Sickler
T
3

Follow this section of the documentation.

Toadstool answered 5/8, 2012 at 22:43 Comment(0)
F
2

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.

Fiddlefaddle answered 6/3, 2013 at 19:55 Comment(0)
M
0

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);
        }
    }
Maidservant answered 6/12, 2018 at 11:57 Comment(0)
N
0

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();
    }
}
Nurmi answered 11/5, 2019 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.