Tworzenie użytkownika administratora przy użyciu datafixtures i fosuserbundle

Próbuję utworzyć nowego Użytkownika Admin z oprawy. Używam FOSUserBundle i 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);

Zawsze dostaję ten błąd:

[ErrorException]                                         
Notice: Undefined property:     
INCES\ComedorBundle\DataFixtures\ORM\LoadUserAdminData::$container in 
/public_html/Symfony/src/INCES/ComedorBundle/DataFixtures/ORM/LoadUserAdminData.php line 16  
Author: Anil, 2012-08-04

3 answers

To działało dla mnie (używam również 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);
    }
}
Mam nadzieję, że to komuś pomoże! :)
 26
Author: Anil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-01 21:25:53

Wykonaj sekcję dokumentacji.

 3
Author: Mun Mun Das,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-05 22:43:20

Błąd jest spowodowany tym, że $container jest obecnie niezdefiniowany. Aby to naprawić, Dodaj interfejs ContainerAwareInterface do definicji klasy.

class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
    ...
}

To nie da ci całkowicie tego, czego chcesz, ponieważ tworzysz użytkownika bez Usermanagera. Zamiast tego powinieneś używać linii, którą skomentowałeś.

Wydaje mi się, że nie potrzebujesz klasy UserAdmin. Administratorzy powinni być podzbiorem użytkownika wyróżniającym się tylko rolami, które mam.

Powinieneś użyć UserManager, aby utworzyć użytkownika (nie UserAdmin) i ustawić role. Jeśli chcesz zachować indeks wszystkich użytkowników adminów, może to osiągnąć Widok MySQL lub możesz utworzyć własną tabelę "cache" i użyć Doctrine Listeners, aby ją zaktualizować w razie potrzeby.

To pytanie jest dość stare, więc zgaduję, że znalazłeś odpowiedź lub przynajmniej obejście. Czy mógłby pan to zapewnić? Możesz odpowiadać na własne pytania.

 2
Author: cmgriffing,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-06 20:10:38