dongliang9576 2016-09-04 12:09
浏览 84
已采纳

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

Hi there I am trying to redirect any user who tries to login who has a ban_status ! = 0

I am wondering what the best way for me to do this, I was thinking just to log them out and redirect them to a banned page but I only want banned users accessing this page, or adding a error on the form to say 'you are banned' etc. Here is my code currently:

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 ($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条回答 默认 最新

  • dongshun1884 2016-09-04 12:22
    关注

    The method wouldn't stop the users from accessing your other routes directly if they know your route names.

    You could stop this by adding a middleware, protecting all your other routes and if the middle ware fails to validate, redirecting the user to a banned page.

    See here: https://laravel.com/docs/5.3/middleware

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?