4

ユーザーのログイン試行を検出して、ユーザーのアカウントをロックすることを決定する Auth.Attempt イベント ハンドラー クラスがあります。ただし、ユーザーをフラッシュ メッセージでログイン ページにリダイレクトしようとしたところ、リダイレクトが機能しないことがわかりました。まだ次のステップに進んでいます。イベントのプロセスを中断し、カスタムの警告メッセージを表示したいと考えています。誰でも私を助けることができますか?どうもありがとう。

私のイベントハンドラ:

namespace MyApp\Handlers\Security;

use DB;
use Session;
use Redirect;

class LoginHandler 
{
    /**
     * Maximum attempts
     * If user tries to login but failed more than this number, User account will be locked
     * 
     * @var integer
     */
    private $max_attemtps;

    /**
     * Maximum attempts per IP
     * If an IP / Device tries to login but failed more than this number, the IP will be blocked
     * 
     * @var integer
     */
    private $ip_max_attempts;

    public function __construct()
    {
        $this->max_attempts = 10;
        $this->ip_max_attempts = 5;
    }

    public function onLoginAttempt($data)
    {
        //detection process.......
        // if login attempts more than max attempts
        return Redirect::to('/')->with('message', 'Your account has been locked.');
    }
}

今私がこれをやっている方法は以下のようなものです:

Session::flash('message', 'Your account has been locked.');
header('Location: '.URL::to('/'));

それは機能しますが、それが完璧な方法であるかどうかはわかりません。

4

2 に答える 2

4

この非常に興味深い議論にはあまり触れていません:

フロー制御に例外を使用する必要があるかどうか

独自の例外ハンドラを設定して、そこからログイン ページにリダイレクトすることができます。

class FancyException extends Exception {}

App::error(function(FancyException $e, $code, $fromConsole)
{
    $msg = $e->getMessage();        
    Log::error($msg);

    if ( $fromConsole )
    {
        return 'Error '.$code.': '.$msg."\n";
    }

    if (Config::get('app.debug') == false) {
        return Redirect::route('your.login.route');
    }
    else
    {
        //some debug stuff here
    }


});

そしてあなたの機能で:

public function onLoginAttempt($data)
{
    //detection process.......
    // if login attempts more than max attempts
    throw new FancyException("some msg here");
}
于 2013-08-07T12:19:13.577 に答える