소스 검색

Force lowercase on email to prevent capitalization issue with SQLite

Bubka 4 년 전
부모
커밋
4d6ae849d8
3개의 변경된 파일69개의 추가작업 그리고 5개의 파일을 삭제
  1. 2 2
      app/Http/Controllers/Auth/LoginController.php
  2. 64 0
      app/Http/Requests/CaseInsensitiveLogin.php
  3. 3 3
      app/User.php

+ 2 - 2
app/Http/Controllers/Auth/LoginController.php

@@ -7,6 +7,7 @@ use Illuminate\Http\Response;
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Lang;
+use App\Http\Requests\CaseInsensitiveLogin;
 use Illuminate\Validation\ValidationException;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
 use Carbon\Carbon;
@@ -35,9 +36,8 @@ class LoginController extends Controller
      *
      * @throws \Illuminate\Validation\ValidationException
      */
-    public function login(Request $request)
+    public function login(CaseInsensitiveLogin $request)
     {
-        $this->validateLogin($request);
 
         // If the class is using the ThrottlesLogins trait, we can automatically throttle
         // the login attempts for this application. We'll key this by the username and

+ 64 - 0
app/Http/Requests/CaseInsensitiveLogin.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Support\Facades\DB;
+use Illuminate\Foundation\Http\FormRequest;
+
+class CaseInsensitiveLogin extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            'email' => [
+                'required',
+                'email',
+                function ($attribute, $value, $fail) {
+
+                    if ('sqlite' === config('database.default')) {
+                        $user = DB::table('users')
+                         ->whereRaw('email = "' . $value . '" COLLATE NOCASE')
+                        ->first();
+                    }
+                    else {
+                        $user = DB::table('users')
+                         ->where('email', $value)
+                        ->first();
+                    }
+
+                    if (!$user) {
+                        $fail(__('validation.custom.email.exists'));
+                    }
+                },
+            ],
+            'password' => 'required|string',
+        ];
+    }
+
+    /**
+     * Prepare the data for validation.
+     *
+     * @return void
+     */
+    protected function prepareForValidation()
+    {
+        $this->merge([
+            'email' => strtolower($this->email),
+        ]);
+    }
+}

+ 3 - 3
app/User.php

@@ -51,11 +51,11 @@ class User extends Authenticatable
     }
 
     /**
-     * Get Email attribute
+     * set Email attribute
      * @param string $value
      */
-    public function getEmailAttribute($value)
+    public function setEmailAttribute($value)
     {
-        return strtolower($value);
+        $this->attributes['email'] = strtolower($value);
     }
 }