Ver Fonte

Refactor login FormRequest and email case sensitive validation rule

Bubka há 3 anos atrás
pai
commit
4ee3557cc1

+ 2 - 2
app/Api/v1/Controllers/GroupController.php

@@ -117,9 +117,9 @@ class GroupController extends Controller
      */
     public function accounts(Group $group)
     {
-        $groups = $this->groupService->getAccounts($group);
+        $twofaccounts = $this->groupService->getAccounts($group);
             
-        return new TwoFAccountCollection($groups);
+        return new TwoFAccountCollection($twofaccounts);
 
     }
 

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

@@ -7,7 +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 App\Http\Requests\LoginRequest;
 use Illuminate\Validation\ValidationException;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
 use Carbon\Carbon;
@@ -31,12 +31,12 @@ class LoginController extends Controller
     /**
      * Handle a login request to the application.
      *
-     * @param  \App\Http\Requests\CaseInsensitiveLogin  $request
+     * @param  \App\Http\Requests\LoginRequest  $request
      * @return \Illuminate\Http\JsonResponse
      *
      * @throws \Illuminate\Validation\ValidationException
      */
-    public function login(CaseInsensitiveLogin $request)
+    public function login(LoginRequest $request)
     {
 
         // If the class is using the ThrottlesLogins trait, we can automatically throttle

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

@@ -1,64 +0,0 @@
-<?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),
-        ]);
-    }
-}

+ 38 - 0
app/Http/Requests/LoginRequest.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Support\Facades\DB;
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Validation\Rule;
+
+
+class LoginRequest 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',
+                new \App\Rules\CaseInsensitiveEmailExists
+            ],
+            'password' => 'required|string',
+        ];
+    }
+}

+ 45 - 0
app/Rules/CaseInsensitiveEmailExists.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Rules;
+
+use Illuminate\Contracts\Validation\Rule;
+use Illuminate\Support\Facades\DB;
+
+class CaseInsensitiveEmailExists implements Rule
+{
+    /**
+     * Create a new rule instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Determine if the validation rule passes.
+     *
+     * @param  string  $attribute
+     * @param  mixed  $value
+     * @return bool
+     */
+    public function passes($attribute, $value)
+    {
+        $user = DB::table('users')
+            ->whereRaw('email = "' . strtolower($value) . '"' . ('sqlite' === config('database.default') ? ' COLLATE NOCASE' : ''))
+            ->first();
+
+        return !$user ? false : true;
+    }
+
+    /**
+     * Get the validation error message.
+     * @codeCoverageIgnore
+     * @return string
+     */
+    public function message()
+    {
+        return trans('validation.custom.email.exists');
+    }
+}