代码之家  ›  专栏  ›  技术社区  ›  slickness

Laravel密码重置错误消息

  •  0
  • slickness  · 技术社区  · 8 年前

    我希望被阻止的用户不能执行密码重置链接,接收错误消息并转发到页面。如果用户被阻止,则表用户中存储2,激活。我该怎么做?

    我从Laravel找到了密码:

    /**
         * Send a reset link to the given user.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
         */
        public function sendResetLinkEmail(Request $request)
        {
            $this->validateEmail($request);
    
            // We will send the password reset link to this user. Once we have attempted
            // to send the link, we will examine the response then see the message we
            // need to show to the user. Finally, we'll send out a proper response.
            $response = $this->broker()->sendResetLink(
                $request->only('email')
            );
    
            return $response == Password::RESET_LINK_SENT
                        ? $this->sendResetLinkResponse($response)
                        : $this->sendResetLinkFailedResponse($request, $response);
        }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   rkj    8 年前

    无需覆盖 sendResetLinkEmail 函数,您只需覆盖 validateEmail 这样地

    protected function validateEmail(Request $request)
    {
        $this->validate($request,   
    
            ['email' => ['required','email',
                          Rule::exists('users')->where(function ($query) {
                            $query->where('active', 1);
                          })
                        ] 
            ]
    
        );
    }
    

    如果要重定向到自定义URL,请覆盖 发送resetlinkemail 像这样的手动验证功能

    public function sendResetLinkEmail(Request $request)
    {
    
         $validator = Validator::make($request->all(), [
                'email' => ['required', 'email',
                             Rule::exists('users')->where(function ($query) {
                                 $query->where('active', 1);
                             })
                           ]
                 ]);
    
         if ($validator->fails()) {
            return redirect('some_other_url')
                   ->with('fail', 'You can not request reset password, account is block');
         }
    
        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );
    
        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }
    
    推荐文章