更新symfony2用户而不会丢失密码

时间:2016-01-13 20:31:30

标签: php symfony

如何在不丢失密码的情况下更新symfony2用户?

这是自定义用户实体

namespace Nbois\UserBundle\Entity;

use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="system_user")
 * @ORM\Entity(repositoryClass="Nbois\UserBundle\Repository\UserRepository")
 */
class User implements AdvancedUserInterface, \Serializable
{
  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @ORM\Column(name="username", type="string", length=25, unique=true)
   * @Assert\NotBlank()
   * @Assert\Length(min=3)
   */
  private $username;

  /**
   * @ORM\Column(name="salt", type="string", length=40)
   */
  private $salt;

  /**
   * @ORM\Column(name="password", type="string", length=255)
   * @Assert\Length(min=6)
   */
  private $password;

  /**
   * @Assert\NotBlank()
   * @Assert\Length(max = 4096)
   */
  private $plainPassword;

  /**
   * @ORM\Column(name="email", type="string", length=60, unique=true)
   * @Assert\NotBlank()
   */
  private $email;

  /**
   * @ORM\Column(type="string", length=25)
   * @Assert\NotBlank()
   */
  private $firstName;

  /**
   * @ORM\Column(type="string", length=25)
   * @Assert\NotBlank()
   */
  private $lastName;

  /**
   * @ORM\Column(type="string", length=12, unique=true)
   * @Assert\NotBlank()
   */
  private $phone;

  /**
   * @ORM\Column(type="string", length=2)
   */
  private $defaultLanguage;

  /**
   * @ORM\Column(name="is_active", type="boolean")
   */
  private $isActive;

  /**
   * @ORM\Column(type="string", length=255, nullable=true)
   */
  private $confirmationToken;

  /**
   * @ORM\Column(type="datetime")
   */
  private $createdAt;

  /**
   * @ORM\Column(type="datetime")
   */
  private $updatedAt;

  /**
  * @ORM\OneToMany(targetEntity="RoleUser", mappedBy="user")
  */
  private $roles;

  // constructor
  public function __construct()
  {
    $this->isActive = true;
    $this->roles = new ArrayCollection();
    $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  }

//----- GET METHODS
  public function getId(){
    return $this->id;
  }

  public function getUsername() {
    return $this->username;
  }

  public function getPassword() {
    return $this->password;
  }

  public function getSalt() {
    return $this->salt;
  }

  public function getEmail()
  {
      return $this->email;
  }

  public function getPlainPassword()
  {
      return $this->plainPassword;
  }

  public function getFirstName()
  {
      return $this->firstName;
  }

  public function getLastName(){
      return $this->lastName;
  }

  public function getPhone(){
      return $this->phone;
  }

  public function getDefaultLanguage(){
      return $this->defaultLanguage;
  }

  public function getCreatedAt(){
      return $this->createdAt;
  }

  public function getUpdatedAt(){
      return $this->updatedAt;
  }

  public function getIsActive(){
    return $this->isActive;
  }

  public function getConfirmationToken(){
    return $this->confirmationToken;
  }

  public function getRoles()
  {
    if(count($this->roles) == 0){
      return array("ROLE_USER");
    }else{

      $result = array();
      foreach($this->roles as $ru){
        array_push($result, $ru->getRole());
      }

      return $result;
    }
  }


//------ SET METHODS

  public function setUsername($username){
    $this->username = $username;
    return $this;
  }

  public function setPassword($password){
    $this->password = $password;
  }

  public function setFirstName($firstName){
      $this->firstName = $firstName;
      return $this;
  }

  public function setLastName($lastName){
      $this->lastName = $lastName;
      return $this;
  }

  public function setEmail($email){
    $this->email = $email;
    return $this;
  }

  public function setPlainPassword($password)
  {
      $this->plainPassword = $password;
  }

  public function setPhone($phone){
      $this->phone = $phone;
      return $this;
  }

  public function setDefaultLanguage($language){
      $this->defaultLanguage = $language;
      return $this;
  }

  public function setCreatedAt(\DateTime $createdAt){
      $this->createdAt = $createdAt;
      return $this;
  }

  public function setUpdatedAt(\DateTime $updatedAt){
      $this->updatedAt = $updatedAt;
      return $this;
  }

  public function setConfirmationToken($token){
    $this->confirmationToken = md5($token);
    return $this;
  }

  public function addRole(Role $role){
    $roles = $this->getRoles();
    array_push($roles, $role);
  }


  /** @see \Serializable::serialize() */
  public function serialize()
  {
    return serialize(array(
      $this->id,
      $this->username,
      $this->email,
      $this->password,
      $this->salt,
      $this->firstName,
      $this->lastName,
      $this->phone,
      $this->defaultLanguage,
      $this->isActive,
      $this->confirmationToken,
      $this->createdAt,
      $this->updatedAt
    ));
  }

  /** @see \Serializable::unserialize() */
  public function unserialize($serialized)
  {
    list (
        $this->id,
        $this->username,
        $this->email,
        $this->password,
        $this->salt,
        $this->firstName,
        $this->lastName,
        $this->phone,
        $this->defaultLanguage,
        $this->isActive,
        $this->confirmationToken,
        $this->createdAt,
        $this->updatedAt
      ) = unserialize($serialized);
  }

  public function eraseCredentials() {
    $this->password = null;
  }

  public function __toString() {
    return $this->username;
  }

  public function isAccountNonExpired() {
    return true;
  }

  public function isAccountNonLocked() {
    return true;
  }

  public function isCredentialsNonExpired() {
    return true;
  }

  public function isEnabled() {
    return $this->isActive;
  }

}

这就是我更新 defaultLanguage 字段的方式:

$em = $this->getDoctrine()->getEntityManager();
$user= $em->getRepository('NboisUserBundle:User')->find(2);
$user->setDefaultLanguage('en');
$em->persist($user);
$em->flush();
  

问题是当我执行此代码并查看数据库时   通知中的更改密码字段为空。我认为这是因为密码没有存储在用户对象中以进行某些安全措施。

这是 symfony profiler log

UPDATE system_user SET password = ?, default_language = ? WHERE id = ?
Parameters: [null, nb, 2]  

1 个答案:

答案 0 :(得分:0)

问题是用户实体中的方法将密码设置为null:

public function eraseCredentials() {
    $this->password = null;
  }

我在这里找到答案: symfony2 : user password set to empty after running this method