私はSymfonyと連絡を取り合っています。これは私の最初のアプリであり、私はすべての環境を理解しようとしています。これは非常に簡単な質問ですが、ウェブでの長いサーフィンの後、私はそれをどうやってやるのか分かりませんでした。Symfony 3のログイン後にユーザーデータを更新
成功またはエラーを処理するためにリスナーと非常にうまく動作するログインフォームがありますが、すべて正常ですが、問題はユーザーの最終アクセス日を更新しようとしたときに発生します。 IユーザーがログインがupdateLastLogin
メソッドを呼び出すとき
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\Column(name="salt", type="string", length=100)
*/
private $salt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastLogin = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt = null;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
public function getUsername()
{
return $this->username;
}
public function setSalt($salt)
{
$this->salt = $salt;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->isActive,
$this->createdAt,
$this->lastLogin,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->isActive,
$this->createdAt,
$this->lastLogin,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Updates last user login date
*
*/
public function updateLastLogin()
{
$this->lastAccess = new \DateTime("now");
}
public function getLastLogin()
{
return $this->lastLogin->format('Y-m-d H:i:s');
}
public function getCreatedAt()
{
return $this->createdAt->format('Y-m-d H:i:s');
}
}
:
<?php
namespace AppBundle\EventListener;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
class LoginListener {
public function __construct(){
}
public function onSuccessfulLogin(AuthenticationEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if($user instanceof AdvancedUserInterface){
$user->updateLastLogin();
}
}
public function onLoginError(AuthenticationEvent $event)
{
// Login error
}
}
updateLastLogin
コードに到達したのが、それはdoesnの
は、私が(ドキュメントから取られた)ユーザエンティティを持っていますデータベースで更新されます。誰かが私に何が間違っているのか教えてもらえますか?
は事前
に@mdmaの答えをありがとう、私はCall to a member function getManager() on string
エラーを得た彼の溶液を用いて、適切なソリューションに私をリードしています。それを解決するために私は、リスナーのコンストラクタでEntityManager
を注入して、データベース内のデータを永続化するために不足しているの呼び出しを追加しました:
<?php
namespace AppBundle\EventListener;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
class LoginListener {
protected $entityManager;
public function __construct(EntityManager $entityManager){
$this->entityManager = $entityManager;
}
public function onSuccessfulLogin(AuthenticationEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if($user instanceof AdvancedUserInterface){
$user->updateLastLogin();
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
public function onLoginError(AuthenticationEvent $event)
{
// Login error
}
}
そして、私は、それを引数として追加services.yml
ファイル内:
app.login_service:
class: AppBundle\EventListener\LoginListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: security.authentication.success, method: onSuccessfulLogin }
今は期待どおりに動作します。
ログイン日を更新するときに、ユーザークラスに誤字があります。存在しない変数を使用していました。 $this->lastAccess = new \DateTime("now");
の代わりに:
$this->lastLogin = new \DateTime("now");
グレート質問と素晴らしい解説!なぜ誰かがこれを否定的に投票するだろうか? – mburakergenc