は、私はこれを解決するために、私見、より適切な方法を考えることができます。
ユーザエンティティは、追加のプロパティを持つことになります$ loginAttemptsこれは、ログインが失敗するたびにincrementLoginAttempts()メソッドによって増分されます。 ORMによって0に初期化され、isLocked()メソッドは5回試行したかどうかを示します。 SecuritySubscriberイベントにEventSubscriberを作成
その後
<?php
// AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* @ORM\Entity
* @ORM\Table(name="`user`")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
public function __construct()
{
parent::__construct();
}
/**
* @ORM\Column(type="integer",options={"default"=0})
*/
private $loginAttempts;
...
public function getLoginAttempts()
{
return $this->loginAttemps;
}
public function incrementLoginAttempts()
{
if($this->loginAttempts<5){
$this->loginAttempts++;
}
return $this;
}
public function isLocked()
{
return ($this->loginAttempts == 5)
}
public function resetLoginAttempts()
{
$this->loginAttempts =0;
return $this;
}
、およびincrementLoginAttemptsを発射()ログインに失敗するたびに、同じ時間チェックイン時にユーザーがすでにロックされているか、まだ
<?php
// src/AppBundle/EventSubscriber/SecuritySubscriber.php
namespace AppBundle\EventSubscriber;
use AppBundle\Entity\User;
class SecuritySubscriber implements EventSubscriberInterface
{
private $entityManager;
private $tokenStorage;
private $authenticationUtils;
public function __construct(EntityManager $entityManager, TokenStorageInterface $tokenStorage, AuthenticationUtils $authenticationUtils)
{
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
$this->authenticationUtils = $authenticationUtils;
}
public static function getSubscribedEvents()
{
return array(
AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
);
}
public function onAuthenticationFailure(AuthenticationFailureEvent $event)
{
$existingUser = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $username]);
if ($existingUser) {
$existingUser->incrementLoginAttempts();
$this->entityManager->persist($existingUser);
$this->entityManager->flush();
if($existingUser->isLocked()){
// Do your logic here
// Do not forget to un $existingUser->resetLoginAttempts() when necessary
}
}
}
}
はサービスとして
# app/config/services.yml
services:
app.security.authentication_event_listener:
class: AppBundle\EventSubscriber\SecuritySubscriber
arguments:
- "@doctrine.orm.entity_manager"
- "@security.token_storage"
- "@security.authentication_utils"
P.Sを加入者を登録することを忘れないでくださいされていない場合:コードがテストされていません。
この機能は、SymfonyのセキュリティコアとFOSUserBundleの内部で使用されていることに気付きましたか?私は確信していませんが、あなたが言ったようにそれほど簡単ではないと思います。別の答えを教えてください。 –
あなたは確かに基本クラスで何もすることはできません。あなたの 'User'クラスでこのメソッドをオーバーライドしたと思います。あなたは持っていないのですか? – Azuloo