【问题标题】:reset password entered email in laravel 5.2 is case sensitive, make it insensitive在 laravel 5.2 中重置密码输入的电子邮件区分大小写,使其不敏感
【发布时间】:2017-10-10 05:21:50
【问题描述】:

我正在开发 laravel5.2 项目,我正在使用 laravel 的默认身份验证模块,它还为我们提供了重置密码功能。

如果用户注册,我将面临问题

abcd@gmail.com

如果用户输入电子邮件重置密码是

Abcd@gmail.com

在这种情况下,它会抛出错误帐户与此电子邮件不存在。

我们可以看到两封电子邮件是相同的,但只是因为第二封电子邮件中的第一个字母大写,它会引发错误。

如何使这个功能不区分大小写?

【问题讨论】:

  • 您可以在密码数据上使用 strtolower() 函数,然后再在数据库中搜索
  • @OdinThunder 是的,我知道,但我没有得到它搜索电子邮件存在的文件/函数。

标签: php laravel


【解决方案1】:

搜索电子邮件的文件路径是 Illuminate\Foundation\Auth\ResetsPasswords。但是您不想编辑此文件。该文件包含在 PasswordController 类中使用的 php 特征。 因此,您可以通过覆盖来更改 trait 方法的功能。

postEmail 是使用给定电子邮件查找用户并发送重置链接的被覆盖方法。使用 ilike 不区分大小写地通过电子邮件查找用户。然后用确切的用户电子邮件覆盖请求电子邮件变量。

以下是您的 PasswordController 类中的代码 (App\Http\Controllers\Auth\PasswordController)

public function postEmail(Request $request) {

    $this->validate($request, ['email' => 'required|email']);

    //Find the user by email case insensitively using ilike
    $user = User::where('email', 'ilike', $request->email)->first();

    // Overwrite request email variable by exact user email
    $request->email = $user->email;

    $response = Password::sendResetLink($request->only('email'), function (Message $message) {
                $message->subject($this->getEmailSubject());
            });

    switch ($response) {
        case Password::RESET_LINK_SENT:
            return redirect()->back()->with('status', trans($response));
        case Password::INVALID_USER:
            return redirect()->back()->withErrors(['email' => trans($response)]);
    }
}

【讨论】:

    【解决方案2】:

    在 ForgotPasswordController.php 中添加此函数以覆盖默认功能。

        use Illuminate\Http\Request;
        use Illuminate\Support\Facades\Password;
    
        public function sendResetLinkEmail(Request $request)
        {
            $this->validateEmail($request);
    
            $data['email'] = strtolower($request->email);
            $response = $this->broker()->sendResetLink($data);
    
            return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
        }
    

    【讨论】:

      【解决方案3】:

      这一切都归结为排序规则。

      我最近在一个使用 Laravel 5.2 的旧网站上也遇到了这个问题。事实证明,将数据库中 email 列的排序规则从 utf8mb4_bin 更改为 utf8mb4_unicode_ci 解决了。

      显然,MySQL 适配器在进行查询时确实使用了 LIKE,但只有在列排序规则允许时才返回不区分大小写的结果。我的不是。

      在这个 Stack Overflow 问题上有更多关于 MySQL 排序规则和区分大小写的讨论:MySQL case insensitive select 以及official MySQL documentation。

      【讨论】:

        【解决方案4】:

        如果您在数据库中存储了小写的电子邮件,只需在ForgotPasswordController.php 中重新定义credentials 函数:

        protected function credentials(\Illuminate\Http\Request $request)
        {
            $data = $request->only('email');
            $data['email'] = mb_strtolower($data['email']);
            return $data;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-25
          • 1970-01-01
          • 2011-01-22
          • 2017-01-07
          • 1970-01-01
          • 2016-11-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多