2016-10-17 12 views
0

I'amをいっぱいにし、私はsecurity.ymlファイルたUserInterfaceを実装ユーザークラスを指すように設定している必要はありませんログインしています。 私のORMスキーマはYAMLにあります。symfonyは - ユーザーはすべてのフィールドがsymfonyの2.8とDoctrineを使用して

問題: - データベースでは、ユーザーも "email"フィールドを指定していますが、LOGGED USERのSymfonyはそのフィールドを埋めるわけではなく、 "password"フィールドも空です。私は、エンティティ・マネージャが正しくエンティティを$this->get('doctrine.orm.entity_manager')->clear(User::class); フェッチしているん

screenshot

。 しかし、エンティティがセッションから来ているときは、正しくありません。

問題は、私はその後、リポジトリ上で検索を()を使用して、データベースからの新鮮なエンティティを取得しようとすると、私は$em->clear(User::class)

質問使用しないときは、セッションからの不正な要素がフェッチされていることもある: を - Symfony/Doctrineに、すべてのフィールドがいっぱいになるようにエンティティを構築するように指示する方法は、永続的になるでしょうか?

<?php 

namespace XXX\AppBundle\Model\Entity; 

use XXX\AppBundle\Model\Entity\Server\Logging; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Security\Core\User\UserInterface; 

/** 
* User 
*/ 
class User implements UserInterface 
{ 
    /** 
    * @var integer $id 
    */ 
    protected $id; 

    /** 
    * @var string $username 
    */ 
    protected $username; 

    /** 
    * @var string $email 
    */ 
    protected $email; 

    /** 
    * @var string $password 
    */ 
    protected $password; 

    /** 
    * @var array $roles 
    */ 
    protected $roles = array(); 

    /** 
    * @var \Doctrine\Common\Collections\Collection $logEvents 
    */ 
    protected $logEvents; 

    /** 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getUsername() 
    { 
     return $this->username; 
    } 

    /** 
    * @param string $username 
    * @return $this 
    */ 
    public function setUsername($username) 
    { 
     $this->username = $username; 
     return $this; 
    } 

    /** 
    * @return string 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * @param string $email 
    * @return $this 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 
     return $this; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * @param string $password 
    * @return $this 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 
     return $this; 
    } 

    /** 
    * Returns the roles or permissions granted to the user for security. 
    */ 
    public function getRoles() 
    { 
     $roles = $this->roles; 

     // guarantees that a user always has at least one role for security 
     if (empty($roles)) { 
      $roles[] = 'ROLE_USER'; 
     } 

     return array_unique($roles); 
    } 

    public function setRoles(array $roles) 
    { 
     $this->roles = $roles; 
    } 

    /** 
    * Returns the salt that was originally used to encode the password. 
    */ 
    public function getSalt() 
    { 
     // See "Do you need to use a Salt?" at http://symfony.com/doc/current/cookbook/security/entity_provider.html 
     // we're using bcrypt in security.yml to encode the password, so 
     // the salt value is built-in and you don't have to generate one 

     return; 
    } 

    /** 
    * Removes sensitive data from the user. 
    */ 
    public function eraseCredentials() 
    { 
     $this->password = null; 
     $this->email = null; 
    } 

    /** 
    * Appends an entry to administration log 
    * 
    * @param \XXX\AppBundle\Model\Entity\Server\Logging $logEvent 
    * @return $this 
    */ 
    public function appendLog(Server\Logging $logEvent) 
    { 
     if (!$this->logEvents->contains($logEvent)) 
     { 
      $this->logEvents->add($logEvent); 
     } 

     return $this; 
    } 

    /** 
    * Remove a log entry from the history 
    * 
    * @param \XXX\AppBundle\Model\Entity\Server\Logging $logEvent 
    * @return $this 
    */ 
    public function clearLogEntry(Server\Logging $logEvent) 
    { 
     $this->logEvents->removeElement($logEvent); 

     return $this; 
    } 
} 

そして、ORMの設定:

XXX\AppBundle\Model\Entity\User: 
    type: entity 
    table: users 
    repositoryClass: XXX\AppBundle\Model\Repository\UserRepository 
    id: 
     id: 
      type: integer 
      scale: 0 
      length: null 
      unique: false 
      nullable: false 
      precision: 0 
      id: true 
      generator: 
       strategy: IDENTITY 
    fields: 
     username: 
      type: string 
      scale: 0 
      length: null 
      unique: true 
      nullable: false 
      precision: 0 
     email: 
      type: string 
      scale: 0 
      length: null 
      unique: true 
      nullable: false 
      precision: 0 
     password: 
      type: string 
      scale: 0 
      length: null 
      unique: false 
      nullable: false 
      precision: 0 
     roles: 
      type: json_array 
      scale: 0 
      length: null 
      unique: false 
      nullable: false 
      precision: 0 
    oneToMany: 
     logEvents: 
      targetEntity: XXX\AppBundle\Model\Entity\Server\Logging 
      cascade: 
       - remove 
       - persist 
      fetch: LAZY 
      mappedBy: author 
      inversedBy: null 
      orphanRemoval: false 
      orderBy: null 
    lifecycleCallbacks: { } 

お時間をいただき、ありがとうございます。 は:-)

+0

セッションからユーザーをどのように取得していますか、ユーザークラスのコードは何ですか? – Gerry

+0

はい、セッションからユーザーを取得しています。 ユーザークラスで私はプライベートプロパティ+ゲッターを保護しました。 ORM ymlファイルは他のエンティティのように通常見えます。 –

+0

コードを投稿してください。 – Gerry

答えて

1

素晴らしい一日は、あなたはそれがメールアドレスとパスワードの設定を解除する方法でのUserInterface :: eraseCredentials()メソッドを実装している:

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

symfonyはオブジェクトを直列化する前にこのメソッドを呼び出しますそれをセッションに保存します。

UserInterface :: eraseCredentials()は、ユーザーオブジェクトから機密データを削除するためのもので、セッションファイルなどでは終わらないようにしていますが、実際にはメールを削除する必要はありません。より良い例は、このオブジェクトのどこかにユーザーのパスワードのプレーンテキスト版を保存している場合です。これは、セッションファイルで決して終了したくないものです。

+0

ありがとうございます。私はユーザー名を削除するのではなく、単にパスワードと電子メール:) –

+0

電子メールまたはパスワードを消去すると、パスワードの変更や役割のリストの変更方法は? –

+0

そうですが、セッションから取得するときに電子メールが表示されないのはそのためです。 – Gerry

関連する問題