默認的Laravel登錄嘗試方法返回布爾,但我想修改它,並獲得另一個結果,如果用戶存在,但用戶的狀態是被動的。 我不想更改laravel供應商目錄中的任何代碼。我可以輕鬆地在LoginController中編寫我自己的登錄方法,但問題是SessionGuard::attempt()
有自己的生命週期,它不是LoginController使用它的特徵。Laravel三州登錄嘗試
以下是SessionGuard::attempt()
方法的原始版本。
public function attempt(array $credentials = [], $remember = false, $login = false)
{
$this->fireAttemptEvent($credentials, $remember, $login);
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
// If an implementation of UserInterface was returned, we'll ask the provider
// to validate the user against the given credentials, and if they are in
// fact valid we'll log the users into the application and return true.
if ($this->hasValidCredentials($user, $credentials)) {
if ($login) {
$this->login($user, $remember);
}
return true;
}
// If the authentication attempt fails we will fire an event so that the user
// may be notified of any suspicious attempts to access their account from
// an unrecognized user. A developer may listen to this event as needed.
if ($login) {
$this->fireFailedEvent($user, $credentials);
}
return false;
}
注:
$credentials = [
'email' => $request->input('email'),
'password' => $request->input('password'),
'status_id' => 1
];
但我想刪除STATUS_ID領域,我想表明的消息給用戶,如果用戶的STATUS_ID不是1
基本上我只是想重寫SessionGuard::attempt()
方法,像這樣。
public function attempt(array $credentials = [], $remember = false, $login = false)
{
$this->fireAttemptEvent($credentials, $remember, $login);
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
// If an implementation of UserInterface was returned, we'll ask the provider
// to validate the user against the given credentials, and if they are in
// fact valid we'll log the users into the application and return true.
if ($this->hasValidCredentials($user, $credentials)) {
//User exists but user's status_id is not 1
if($user->status_id != 1)
return 2; // 2 for passive accounts
if ($login) {
$this->login($user, $remember);
}
return 1; //for active accounts
}
// If the authentication attempt fails we will fire an event so that the user
// may be notified of any suspicious attempts to access their account from
// an unrecognized user. A developer may listen to this event as needed.
if ($login) {
$this->fireFailedEvent($user, $credentials);
}
return 0; // for non exists acounts
}
我該如何存檔?
我們需要更多信息。你在'laravel框架'中的任何代碼是什麼意思?兩種功能應該發生什麼?您可以在控制器功能中手動登錄用戶,因此您不必覆蓋它們。 – mimo
對不起,我可憐的英語我假設我無法解釋我的問題。我剛剛編輯了我的問題。我希望這次比老版本更好。 –