Symfony2 $user->setPassword() 将密码更新为纯文本 [DataFixtures + FOS用户包]
我正在尝试使用一些 User 对象预填充数据库,但是当我调用 $user->setPassword('some-password');
然后保存用户对象时,字符串 ' some-password' 直接存储在数据库中,而不是散列+加盐的密码。
我的 DataFixture 类:
// Acme/SecurityBundle/DataFixtures/ORM/LoadUserData.php
<?php
namespace Acme\SecurityBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Acme\SecurityBundle\Entity\User;
class LoadUserData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$userAdmin = new User();
$userAdmin->setUsername('System');
$userAdmin->setEmail('[email protected]');
$userAdmin->setPassword('test');
$manager->persist($userAdmin);
$manager->flush();
}
}
以及相关的数据库输出:
id username email salt password
1 System [email protected] 3f92m2tqa2kg8cookg84s4sow80880g test
I'm trying to pre-populate a database with some User objects, but when I call $user->setPassword('some-password');
and then save the user object, the string 'some-password' is stored directly in the database, instead of the hashed+salted password.
My DataFixture class:
// Acme/SecurityBundle/DataFixtures/ORM/LoadUserData.php
<?php
namespace Acme\SecurityBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Acme\SecurityBundle\Entity\User;
class LoadUserData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$userAdmin = new User();
$userAdmin->setUsername('System');
$userAdmin->setEmail('[email protected]');
$userAdmin->setPassword('test');
$manager->persist($userAdmin);
$manager->flush();
}
}
And the relevant database output:
id username email salt password
1 System [email protected] 3f92m2tqa2kg8cookg84s4sow80880g test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于您使用的是 FOSUserBundle,因此可以使用
UserManager
来执行此操作。我将使用此代码(假设您设置了$this->container
):Since you are using FOSUserBundle, you can use
UserManager
to do this. I would use this code (assuming you have$this->container
set):请改为调用 setPlainPassword。
Call setPlainPassword instead.
四行代码就完成了。它会为你处理一切:
Four lines of code and you are done. It will handle everything for you:
这对我有用
请注意设置密码时的区别。查询找到的数据库
This worked for me
Note the difference when setting the password. Querying the database you find
setPlainPassword 对我有用。
setPlainPassword works for me.
以上对我有用,所以我得到了一些结论:
1.必须使用 setPlainPassword
2。必须设置启用(true)
Above worked for me, so I got some conclusion:
1. must use the setPlainPassword
2. must setEnabled(true)
这里是一个通过 ORM Fixtures 创建管理员用户的示例类:
Here a sample class to create an admin user via ORM Fixtures: