Laravel 5.3在登录时重定向被禁用的用户

时间:2016-09-04 12:09:06

标签: php laravel

您好,我正在尝试重定向尝试登录谁拥有ban_status的用户! = 0

我想知道对我来说最好的方法是什么,我只想将它们注销并将它们重定向到禁止的页面,但我只想让禁止的用户访问此页面,或者在表单上添加错误说“你被禁止了”等。这是我目前的代码:

公共功能登录(请求$请求)     {         $这 - > validateLogin($请求);

    // 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 ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

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

    $credentials = $this->credentials($request);

    if ($this->guard()->attempt($credentials, $request->has('remember'))) {
        if ($this->guard()->user()->ban_status === 0) { // Check if the user is banned or not before allowing login
            return $this->sendLoginResponse($request);
        }
        else {

            $this->guard()->logout();

            $request->session()->flush();

            $request->session()->regenerate();

            return redirect('/banned'); // If the user banned_status is set to 1, then they will be redirected to a banned page.
        }
    }

    // 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.
    if (! $lockedOut) {
        $this->incrementLoginAttempts($request);
    }

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

1 个答案:

答案 0 :(得分:3)

如果用户知道您的路线名称,该方法不会阻止用户直接访问您的其他路线。

您可以通过添加中间件来阻止此操作,保护所有其他路由,如果中间件无法验证,则将用户重定向到禁止的页面。

见这里:https://laravel.com/docs/5.3/middleware