2016-01-13 53 views
0

如何在不丟失密碼的情況下更新symfony2用戶?更新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的探查日誌

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

那麼,通常你不會想要存儲'$ plainPassword'。你是否確實想要這樣做? – JimL

+0

那麼如何更新用戶實體而不丟失密碼?它必須是preUpdate上的一個技巧......我想 –

+0

你有一些聽衆清除密碼?如果是這樣,爲什麼? –

回答

相關問題