Php Laravel三状态登录尝试

Php Laravel三状态登录尝试,php,laravel,laravel-5,laravel-5.3,Php,Laravel,Laravel 5,Laravel 5.3,默认情况下,Laravel登录尝试方法返回bool,但如果用户存在,但用户状态为被动,我想修改它并获得另一个结果。 我不想更改laravel供应商目录中的任何代码。我可以在LoginController中编写自己的登录方法,比如AuthenticatesUsers::login(),但问题是SessionGuard::trust()有自己的生命周期,这不是LoginController使用它的特点 以下是SessionGuard::trunt()方法的原始版本 public function a

默认情况下,Laravel登录尝试方法返回bool,但如果用户存在,但用户状态为被动,我想修改它并获得另一个结果。 我不想更改laravel供应商目录中的任何代码。我可以在LoginController中编写自己的登录方法,比如
AuthenticatesUsers::login()
,但问题是
SessionGuard::trust()
有自己的生命周期,这不是LoginController使用它的特点

以下是
SessionGuard::trunt()
方法的原始版本

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::trunt()
方法,类似这样

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
}

如何实现这一点?

您可以在控制器(通常是LoginController)的登录功能中实现这一点

您首先使用一封独特的电子邮件获取用户。然后检查帐户是否具有该变量。如果是,您可以尝试登录用户,如果不是,您可以抛出一个错误

class LoginController extends Controller
{

    use AuthenticatesUsers;

        public function login(Request $request) {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }
        $user = App\User::where('email', $request->get('email'))->first();

        if ($user && $user->status_id == 1 && $this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }
}

您可以在控制器(通常是LoginController)的登录功能中执行此操作

您首先使用一封独特的电子邮件获取用户。然后检查帐户是否具有该变量。如果是,您可以尝试登录用户,如果不是,您可以抛出一个错误

class LoginController extends Controller
{

    use AuthenticatesUsers;

        public function login(Request $request) {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }
        $user = App\User::where('email', $request->get('email'))->first();

        if ($user && $user->status_id == 1 && $this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }
}

我们需要更多的信息。您对laravel框架中的
任何代码是什么意思?在这两个函数中应该发生什么?您可以在控制器功能中手动登录用户,这样就不必覆盖它们。对不起,我的英语很差,我想我无法解释我的问题。我刚刚编辑了我的问题。我希望这次比旧版本好。我们需要更多信息。您对laravel框架中的
任何代码是什么意思?在这两个函数中应该发生什么?您可以在控制器功能中手动登录用户,这样就不必覆盖它们。对不起,我的英语很差,我想我无法解释我的问题。我刚刚编辑了我的问题。我希望这次比旧版本更好。这是正确的答案,但需要一些改进和修复语法错误。我试图编辑,但它说编辑队列已满。也许有人也需要它,所以你可以添加这些行
使用authenticateUsers{login as protected traitLogin;}
并使用
return$this->traitLogin($request)
而不是
Auth::trunt($request->only(['email','password'])最后谢谢。更新了我的答案。我现在使用了trait中的login函数,并修改了if语句。您可以更改逻辑,例如,如果您想在用户未激活时显示另一条消息,但现在应该可以了。我已经为我的项目编辑了旧版本的代码,它也可以了。此外,我还覆盖LoginController中的AuthenticatesUsers::sendFailedLoginResponse方法,以便在任何情况下发送任何消息。AuthenticatesUsers或SessionGuard没有更改。再次感谢。这是正确的答案,但需要一些改进和修复语法错误。我试图编辑,但它说编辑队列已满。也许有人也需要它,所以你可以添加这些行
使用authenticateUsers{login as protected traitLogin;}
并使用
return$this->traitLogin($request)
而不是
Auth::trunt($request->only(['email','password'])最后谢谢。更新了我的答案。我现在使用了trait中的login函数,并修改了if语句。您可以更改逻辑,例如,如果您想在用户未激活时显示另一条消息,但现在应该可以了。我已经为我的项目编辑了旧版本的代码,它也可以了。此外,我还覆盖LoginController中的AuthenticatesUsers::sendFailedLoginResponse方法,以便在任何情况下发送任何消息。AuthenticatesUsers或SessionGuard没有更改。再次感谢你。