【问题标题】:Laravel dont allow password reset for certain user rolesLaravel 不允许某些用户角色的密码重置
【发布时间】:2018-09-24 21:58:30
【问题描述】:

我在 Laravel 中设置了密码重置。我已经通读了文档https://laravel.com/docs/5.5/passwords,但我看不到任何地方是否存在仅允许基于用户表中数据库中的字段为某些用户重置密码的功能。

在我的用户表中,如果 user_type 设置为 2,我已添加字段 user_type 我不想允许为该用户发送密码重置链接

【问题讨论】:

    标签: laravel


    【解决方案1】:

    Http/Controllers/Auth/ForgotPasswordController.php 中使用了一个特征SendsPasswordResetEmails

    您可以覆盖 ForgotPasswordController 中的函数 sendResetLinkEmail 并在那里添加您的条件。

    public function sendResetLinkEmail(Request $request)
    {
        $user = User::where('email', $request->get('email'))->get();
        if (!$user || $user->user_type == 2) {
           return redirect()->back()->with('error' => '...');
        }
        //rest of function
        $this->validateEmail($request);
    
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );
    
        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }
    

    【讨论】:

      【解决方案2】:

      Dimitri's Answer 很棒而且很正确。谢谢。

      但有一种方法可以避免代码重复:不要复制原始代码,只需在完成自定义代码后调用 trait 的方法。

      在您的 ForgotPasswordController 中,将 use SendsPasswordResetsEmails; 替换为

      use SendsPasswordResetEmails {
          // make the trait's method available as traitSendResetLinkEmail
          sendResetLinkEmail as public traitSendResetLinkEmail;
      }
      

      然后写sendResetLinkEmail如下:

      $this->validateEmail($request);    
      $user = User::where('email', $request->get('email'))->get();
      if (!$user || $user->user_type == 2) {
         return redirect()->back()->with('error' => '...');
      }
      // call the original method
      return $this->traitSendResetLinkEmail($request);
      

      【讨论】:

        猜你喜欢
        • 2011-02-16
        • 2019-02-16
        • 1970-01-01
        • 2013-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-19
        • 2013-04-12
        • 2015-05-17
        相关资源
        最近更新 更多