php Symfony2 $ user-> setPassword() aggiorna la password come testo normale



doctrine fosuserbundle (5)

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

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

https://src-bin.com

Sto provando a pre-compilare un database con alcuni oggetti User, ma quando chiamo $user->setPassword('some-password'); e quindi salva l'oggetto utente, la stringa 'some-password' è memorizzata direttamente nel database, invece della password hash + salata.

La mia classe 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();
    }
}

E l'output del database pertinente:

id  username    email               salt                                password
1   System      [email protected]  3f92m2tqa2kg8cookg84s4sow80880g     test

Answer #1

Dato che stai usando FOSUserBundle, puoi usare UserManager per farlo. Vorrei usare questo codice (assumendo che tu abbia $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 #2

Quattro righe di codice e hai finito. Gestirà tutto per te:

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

Answer #3

Questo ha funzionato per 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();
  }

Notare la differenza quando si imposta la password. Interrogare il database che trovi

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

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

Sopra ha funzionato per me, quindi ho ottenuto qualche conclusione:
1. deve usare setPlainPassword
2. must setEnabled (true)