简体   繁体   English

reset password entered email in laravel 5.2区分大小写,设为不区分大小写

[英]reset password entered email in laravel 5.2 is case sensitive, make it insensitive

I am working on laravel5.2 project, i am using laravel's default auth module, which also provides us the reset password functionality.我正在从事 laravel5.2 项目,我正在使用 laravel 的默认 auth 模块,它还为我们提供了重置密码功能。

I am facing issue in case if user is registered with如果用户已注册,我将面临问题

abcd@gmail.com

and if user enter email to reset password is如果用户输入 email 重置密码

Abcd@gmail.com

In case of this it throws error account with this email doesn't exists.在这种情况下,它会抛出此 email 不存在的错误帐户。

As we can see both emails are same but just because of capitalization for first letter in second email it is throwing error.正如我们所看到的,两封电子邮件是相同的,但仅仅因为第二个 email 中第一个字母的大写,它就会抛出错误。

How to make this functionality case insensitive?如何使此功能不区分大小写?

Add this function in ForgotPasswordController.php to override the default functionality. 在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);
    }

Path of the file where searching email is Illuminate\\Foundation\\Auth\\ResetsPasswords . 搜索电子邮件的文件路径是Illuminate \\ Foundation \\ Auth \\ ResetsPasswords But you don't want to edit this file. 但是您不想编辑此文件。 This file contain a php trait that use in PasswordController class. 此文件包含在PasswordController类中使用的php特性。 So you can change the functionality of the trait methods by overriding it. 因此,您可以通过覆盖trait方法来更改其功能。

postEmail is the methods to be overwritten that find the user with given email and send reset link. postEmail是要覆盖的方法,该方法将查找给定电子邮件的用户并发送重置链接。 Find the user by email case insensitively using ilike . 使用ilike不区分大小写地通过电子邮件查找用户。 Then overwrite request email variable by exact user email. 然后用确切的用户电子邮件覆盖请求电子邮件变量。

Following is to be the code in your PasswordController class (App\\Http\\Controllers\\Auth\\PasswordController) 以下是您的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)]);
    }
}

It all comes down to Collation. 归结为归类。

I also ran in to this problem recently on an older site using Laravel 5.2. 最近在使用Laravel 5.2的旧站点上,我也遇到了这个问题。 It turns out that changing the collation of the email column in the database from utf8mb4_bin to utf8mb4_unicode_ci solved it. 事实证明,将数据库中电子邮件列的排序规则从utf8mb4_bin更改为utf8mb4_unicode_ci可以解决此问题。

Apparently, the MySQL adapter does use LIKE when making the query, but that only returns the case-insensitive result if the column collation is allowing it to. 显然,MySQL适配器在进行查询时确实使用LIKE,但只有在列排序规则允许时,它才返回不区分大小写的结果。 Mine wasn't. 我的不是。

There is more discussion on the topic of MySQL collation and case sensitivity on this Stack Overflow question: MySQL case insensitive select as well as in the official MySQL documentation . 关于这个堆栈溢出问题: MySQL不区分大小写的选择以及官方的MySQL文档,有更多关于MySQL排序规则和区分大小写的讨论。

In case you have stored your emails lowercased in the database, just redefine the credentials function in ForgotPasswordController.php :如果您将小写的电子邮件存储在数据库中,只需在ForgotPasswordController.php中重新定义credentials function :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM