【问题标题】:Laravel 5.3 Auth block userLaravel 5.3 Auth 阻止用户
【发布时间】:2017-03-30 22:12:05
【问题描述】:

我有一个问题,我目前正在使用 Laravel 5.3 开发一个小网站,我正在使用他们提供的 Basic Auth 让用户注册和登录。

现在我想要以下内容:每个人都可以注册和登录,但如果我(以管理员身份)点击一个按钮,我可以“阻止”一个特定用户(例如,如果他做了不允许的事情),我不会' t 完全删除数据库中的行,但以某种方式确保如果用户尝试登录,他会收到一条消息,例如“您无法再登录,您的帐户被阻止,请联系管理员获取更多信息”或类似内容.问题是:最好的方法是什么?我没有找到内置的东西,如果我错了,请纠正我...... 当然,我可以更改 users 表并添加一个名为“blocked”的列,通常设置为 false,然后使用按钮,将其设置为 true,然后在登录时以某种方式检查该值并(如果为 true)显示这个消息,不允许登录。这是最好的方法吗?如果是,我必须在哪里检查此值以及如何显示该消息?如果没有,有什么更好的方法?

【问题讨论】:

    标签: laravel authentication laravel-5.3 laravel-authorization


    【解决方案1】:

    有一个包不仅可以阻止用户,还可以让您在决定是否阻止用户之前对其进行监控。

    Laravel 监控: https://github.com/neelkanthk/laravel-surveillance

    【讨论】:

      【解决方案2】:

      第一步:

      add new field to the User table called ‘status’ (1:enabled, 0:disabed)
      

      第二步:

      to block the web login , in app/Http/Controllers/Auth/LoginController.php add the follwoing function:
      
      /**
       * Get the needed authorization credentials from the request.
       *
       * @param \Illuminate\Http\Request $request
       * @return array
       */
       protected function credentials(\Illuminate\Http\Request $request)
       {
       $credentials = $request->only($this->username(), ‘password’);
      
      return array_add($credentials, ‘status’, ‘1’);
       }
      

      第三步:

      to block the user when using passport authentication ( token ) , in the User.php model add the following function :
      
      public function findForPassport($identifier) {
           return User::orWhere(‘email’, $identifier)->where(‘status’, 1)->first();
           }
      

      参考这个链接(教程)会帮助你:https://medium.com/@mshanak/solved-tutorial-laravel-5-3-disable-enable-block-user-login-web-passport-oauth-4bfb74b0c810

      【讨论】:

        【解决方案3】:

        已解决:此链接(教程)将对您有所帮助:https://medium.com/@mshanak/solved-tutorial-laravel-5-3-disable-enable-block-user-login-web-passport-oauth-4bfb74b0c810

        第一步:

        add new field to the User table called ‘status’ (1:enabled, 0:disabed)
        

        第二步:

        to block the web login , in app/Http/Controllers/Auth/LoginController.php add the follwoing function:
        
        /**
         * Get the needed authorization credentials from the request.
         *
         * @param \Illuminate\Http\Request $request
         * @return array
         */
         protected function credentials(\Illuminate\Http\Request $request)
         {
         $credentials = $request->only($this->username(), ‘password’);
        
        return array_add($credentials, ‘status’, ‘1’);
         }
        

        第三步:

        to block the user when using passport authentication ( token ) , in the User.php model add the following function :
        
        public function findForPassport($identifier) {
             return User::orWhere(‘email’, $identifier)->where(‘status’, 1)->first();
             }
        

        完成:)

        【讨论】:

        • 始终欢迎提供指向潜在解决方案的链接,但请add context around the link,以便您的其他用户知道它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。考虑到仅仅是指向外部站点的链接是Why and how are some answers deleted? 的一个可能原因。
        【解决方案4】:

        我会按照你的建议去做 - 使用 blockedactive 列来指示用户是否应该能够登录。当我过去做过类似的事情时,检查这个值登录,我将开箱即用的登录功能移到我的 LoginController 中并添加了一点。我的登录方法现在如下所示:

        /**
         * Handle a login request to the application.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function login(Request $request)
        {
            $this->validateLogin($request);
        
            $user = User::where('email', $request->email)->firstOrFail();
            if ( $user && !$user->active ) {
                return $this->sendLockedAccountResponse($request);
            }
        
            if ($this->hasTooManyLoginAttempts($request)) {
                $this->fireLockoutEvent($request);
        
                return $this->sendLockoutResponse($request);
            }
        
            if ($this->attemptLogin($request)) {
                return $this->sendLoginResponse($request);
            }
        
            $this->incrementLoginAttempts($request);
        
            return $this->sendFailedLoginResponse($request);
        }
        

        我还添加了这些函数来处理不活跃的用户:

        /**
         * Get the locked account response instance.
         *
         * @param \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        protected function sendLockedAccountResponse(Request $request)
        {
            return redirect()->back()
                ->withInput($request->only($this->loginUsername(), 'remember'))
                ->withErrors([
                    $this->loginUsername() => $this->getLockedAccountMessage(),
                ]);
        }
        
        /**
         * Get the locked account message.
         *
         * @return string
         */
        protected function getLockedAccountMessage()
        {
            return Lang::has('auth.locked')
                    ? Lang::get('auth.locked')
                    : 'Your account is inactive. Please contact the Support Desk for help.';
        }
        

        【讨论】:

        • 感谢您的回答。您发布的内容看起来很酷,但是如何将开箱即用的登录功能移到我自己的控制器中,以便它使用我 rpvoide 代替?
        • AuthController 从Illuminate\Foundation\Auth\AuthenticatesUsers 继承登录方法。如果你将该类的登录方法复制到你的 AuthController 中,Laravel 将使用 AuthController 版本。这样你就可以修改它以满足你的需要。 (这是针对 Laravel 5.2,但我相信这些类名在 5.3 中是相同的)
        • 但是 AuthController 到底是什么?只是我创建的一个新控制器?什么时候叫?抱歉,有点糊涂
        • 对 - 对不起,5.3 更改了控制器的名称。如果您使用的是 laravel auth 脚手架,您应该将 login 方法添加到 LoginController (App\Http\Controllers\Auth\LoginController)。
        • 所以我要做的是从AuthenticateUsers复制函数login,在LoginController中覆盖它,添加您提供的if条件以及添加两个方法(sendLockedAccountResponse和@987654330 @,对吗?在这样做之前,我确保有一个按钮将数据库中的 locked 列设置为 true,这是您的意思吗?
        【解决方案5】:

        您可以使用soft deleting 功能。

        除了实际从数据库中删除记录外,Eloquent 还可以“软删除”模型。当模型被软删除时,它们实际上并没有从您的数据库中删除。相反,在模型上设置了一个 deleted_at 属性并插入到数据库中。如果模型具有非空 deleted_at 值,则模型已被软删除。

        【讨论】:

        • 实际上与我所说的相同,但只是使用deleted_at 属性而不是名为blocked 的列。我也可能想完全删除数据库中的某些内容,所以这不是我想要的想我猜
        • @nameless 软删除正是您想要的。它已经拥有所有必要的方法,例如forceDelete()withTrashed()trashed()restore() 等。它还会自动隐藏“被阻止”的用户,使其远离通常的get()paginate() 等查询。
        • 但启用软删除后,如果我愿意,我还能完全删除用户吗?然后我怎么能(如果用户被软删除)在登录时检查它显示消息?
        • 要完全删除用户使用forceDelete()
        • To permanently remove a soft deleted model from the database, use the forceDelete method:我必须先软删除然后再使用forceDelete()?also still don't no how I can, when a user logs in, check if this account is soft deleted and if yes print this message (actually print it instead of the db`消息中找不到这条记录)
        猜你喜欢
        • 2017-03-14
        • 1970-01-01
        • 2017-04-28
        • 2017-01-04
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多