默认的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
对不起,我可怜的英语我假设我无法解释我的问题。我刚刚编辑了我的问题。我希望这次比老版本更好。 –