Home » Php » php – Symfony2 $user->setPassword() updates password as plain text [DataFixtures + FOSUserBundle]

php – Symfony2 $user->setPassword() updates password as plain text [DataFixtures + FOSUserBundle]

Posted by: admin April 23, 2020 Leave a comment

Questions:

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
How to&Answers:

Since you are using FOSUserBundle, you can use UserManager to do this. I would use this code (assuming you have $this->container set):

public function load(ObjectManager $manager)
{
    $userManager = $this->container->get('fos_user.user_manager');

    $userAdmin = $userManager->createUser();

    $userAdmin->setUsername('System');
    $userAdmin->setEmail('[email protected]');
    $userAdmin->setPlainPassword('test');
    $userAdmin->setEnabled(true);

    $userManager->updateUser($userAdmin, true);
}

Answer:

Call setPlainPassword instead.

<?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('[email protected]');            
        $userAdmin->setPlainPassword('test');
        $userAdmin->setRoles(array('ROLE_SUPER_ADMIN'));

        $manager->persist($userAdmin);
        $manager->flush();
    }
}

Answer:

Four lines of code and you are done. It will handle everything for you:

        $userManager = $this->container->get('fos_user.user_manager');
        $user->setPlainPassword($password);
        $userManager->updatePassword($user);  

Answer:

This worked for me

  public function load(ObjectManager $manager){
    $userAdmin = new User();
    $userAdmin->setUsername('admin');
    $userAdmin->setPlainPassword('admin');
    $userAdmin->setEmail('[email protected]');
    $userAdmin->setEnabled(true);

    $manager->persist($userAdmin);
    $manager->flush();
  }

Note the difference when setting the password. Querying the database you find

id  username    username_canonical  email              email_canonical  enabled salt                            password    
  2 admin       admin               [email protected]    [email protected]  1       4gm0bx6jzocgksw0wws8kck04kg40o8 m2ZyJM2+oBIzt/NZdnOX4nFvjV/SWTU1qJqe6dWZ0UwLF5gB8N...

Answer:

$userAdmin->setUsername('System');
$userAdmin->setEmail('[email protected]');
$userAdmin->setPlainPassword('test');
$userAdmin->setEnabled(true);

setPlainPassword works for me.

Answer:

/**
 * 添加用户
 * @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();
}

Above worked for me, so I got some conclusion:

1. must use the setPlainPassword

2. must setEnabled(true)

Answer:

Here a sample class to create an admin user via ORM Fixtures:

<?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('[email protected]');
        $userAdmin->setPlainPassword('admin');
        $userAdmin->setEnabled(true);
        $userAdmin->setRoles(array('ROLE_ADMIN'));

        $userManager->updateUser($userAdmin, true);
    }

    public function getOrder()
    {
        return 1;
    }
}