Refactor login FormRequest and email case sensitive validation rule

This commit is contained in:
Bubka 2021-11-30 17:36:45 +01:00
parent 0a22fb4cf1
commit 4ee3557cc1
5 changed files with 88 additions and 69 deletions

View File

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

View File

@ -7,7 +7,7 @@
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

View File

@ -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),
]);
}
}

View File

@ -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',
];
}
}

View File

@ -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');
}
}