Symfony2 $ user-> setPassword()更新密码为纯文本
我试图用一些用户对象预先填充数据库,但是当我调用$user->setPassword('some-password');
然后保存用户对象,string“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('system@example.com'); $userAdmin->setPassword('test'); $manager->persist($userAdmin); $manager->flush(); } }
和相关的数据库输出:
id username email salt password 1 System system@example.com 3f92m2tqa2kg8cookg84s4sow80880g test
由于您使用的是FOSUserBundle,因此您可以使用UserManager
来执行此操作。 我会使用这个代码(假设你有$this->container
集):
public function load(ObjectManager $manager) { $userManager = $this->container->get('fos_user.user_manager'); $userAdmin = $userManager->createUser(); $userAdmin->setUsername('System'); $userAdmin->setEmail('system@example.com'); $userAdmin->setPlainPassword('test'); $userAdmin->setEnabled(true); $userManager->updateUser($userAdmin, true); }
改为调用setPlainPassword。
<?php namespace Acme\SecurityBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Doctrine\Common\Persistence\ObjectManager; use Acme\SecurityBundle\Entity\User; class LoadUserData implements FixtureInterface, ContainerAwareInterface { private $container; public function setContainer(ContainerInterface $container = null) { $this->container = $container; } public function load(ObjectManager $manager) { $userAdmin = new User(); $userAdmin->setUsername('System'); $userAdmin->setEmail('system@example.com'); $userAdmin->setPlainPassword('test'); $userAdmin->setRoles(array('ROLE_SUPER_ADMIN')); $manager->persist($userAdmin); $manager->flush(); } }
四行代码,你完成了。 它会处理你的一切:
$userManager = $this->container->get('fos_user.user_manager'); $user->setPlainPassword($password); $userManager->updatePassword($user);
这对我有效
public function load(ObjectManager $manager){ $userAdmin = new User(); $userAdmin->setUsername('admin'); $userAdmin->setPlainPassword('admin'); $userAdmin->setEmail('admin@gmail.com'); $userAdmin->setEnabled(true); $manager->persist($userAdmin); $manager->flush(); }
请注意设置密码时的差异。 查询您find的数据库
id username username_canonical email email_canonical enabled salt password 2 admin admin admin@gmail.com admin@gmail.com 1 4gm0bx6jzocgksw0wws8kck04kg40o8 m2ZyJM2+oBIzt/NZdnOX4nFvjV/SWTU1qJqe6dWZ0UwLF5gB8N...
$userAdmin->setUsername('System'); $userAdmin->setEmail('system@example.com'); $userAdmin->setPlainPassword('test'); $userAdmin->setEnabled(true);
setPlainPassword适用于我。
/** * 添加用户* @param $param * @return int */ public function doAdd($param) { $entity = new User(); $em = $this->getEntityManager(); $entity->setUsername($param['username']) ->setPlainPassword($param['password']) ->setEmail($param['email']) ->setEnabled(true) ->setRealName($param['realName']); $em->persist($entity); $em->flush(); return $entity->getId(); }
以上为我工作,所以我得到了一些结论:
1.必须使用setPlainPassword
2.必须setEnabled(true)
这里有一个示例类,通过ORM Fixtures创build一个pipe理员用户:
<?php namespace Acme\SecurityBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Acme\SecurityBundle\Entity\User; class LoadFOSAdminUser extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface { private $container; public function setContainer(ContainerInterface $container = null) { $this->container = $container; } public function load(ObjectManager $manager) { $userManager = $this->container->get('fos_user.user_manager'); $userAdmin = $userManager->createUser(); $userAdmin->setUsername('admin'); $userAdmin->setEmail('admin@example.com'); $userAdmin->setPlainPassword('admin'); $userAdmin->setEnabled(true); $userAdmin->setRoles(array('ROLE_ADMIN')); $userManager->updateUser($userAdmin, true); } public function getOrder() { return 1; } }