简体   繁体   English

如何使用Sonata Admin使用灯具创建用户

[英]How to create user with fixtures with sonata admin

I started my application in Symfony 4.2 with FOSUserBundle and made a fixture file to create the admin. 我使用FOSUserBundle在Symfony 4.2中启动了我的应用程序,并制作了一个夹具文件来创建管理员。

use FOS\UserBundle\Doctrine\UserManager;
...
/** @var UserManager $userManager */
$userManager = $this->getContainer()->get('fos_user.user_manager');
/** @var User $user */
$user = $userManager->createUser();
$user
    ->setUsername('username')
    ->setEmail('email')
    ->setPlainPassword('password')
    ->setSuperAdmin(true);
$manager->persist($user);
$manager->flush();

It worked fine, but then I decided to user SonataAdmin and ACL. 它工作正常,但后来我决定使用SonataAdmin和ACL。

Since then, I can't login anymore with the admin. 从那时起,我无法再使用管理员登录。

But if I create the admin user with the bin/console fos:user:create --super-admin command, instead the one from the fixture file, I can login normally. 但是,如果我使用bin/console fos:user:create --super-admin命令(而不是bin/console fos:user:create --super-admin命令)创建admin用户,则可以正常登录。

Checking the database, the only differences between the two users are the salt and the password hash, but that's expected. 检查数据库,两个用户之间的唯一区别是salt和密码哈希,但这是可以预期的。

That said, can anybody explain what I need to change in the fixture file for it to create a working admin user? 就是说,谁能解释一下我需要在Fixture文件中进行哪些更改才能创建一个正常的管理员用户?

Thanks in advance. 提前致谢。

The answer is in FOS\\UserBundle\\Command\\CreateUserCommand . 答案在FOS\\UserBundle\\Command\\CreateUserCommand You need to use the FOS\\UserBundle\\Util\\UserManipulator to create the user. 您需要使用FOS\\UserBundle\\Util\\UserManipulator创建用户。

use FOS\UserBundle\Util\UserManipulator;

...

/** @var UserManipulator $userManipulator */
$userManipulator = $this->getContainer()->get('fos_user.util.user_manipulator');

$user = $userManipulator->create( 'username', 'password', 'email', true, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM