2021-09-17 23:48:30 +02:00
|
|
|
<?php
|
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
namespace App\Api\v1\Requests;
|
2021-09-17 23:48:30 +02:00
|
|
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2021-11-13 13:36:34 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-08-06 11:21:08 +02:00
|
|
|
use Illuminate\Support\Fluent;
|
|
|
|
use Illuminate\Validation\Validator;
|
2021-09-17 23:48:30 +02:00
|
|
|
|
|
|
|
class TwoFAccountDynamicRequest extends FormRequest
|
|
|
|
{
|
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-17 23:48:30 +02:00
|
|
|
public function authorize()
|
|
|
|
{
|
2021-11-13 13:36:34 +01:00
|
|
|
return Auth::check();
|
2021-09-17 23:48:30 +02:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-09-17 23:48:30 +02:00
|
|
|
/**
|
2022-11-22 15:15:52 +01:00
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-17 23:48:30 +02:00
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
$rules = Arr::has($this->validationData(), 'uri')
|
|
|
|
? (new TwoFAccountUriRequest)->rules()
|
|
|
|
: (new TwoFAccountStoreRequest)->rules();
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
2022-10-10 12:55:59 +02:00
|
|
|
|
2024-08-06 11:21:08 +02:00
|
|
|
/**
|
|
|
|
* Get the "withValidator" validation callables for the request.
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator) : void
|
|
|
|
{
|
|
|
|
// The account may have to be assign to a specific group.
|
|
|
|
// If so, we check if the provided group exists.
|
|
|
|
$validator->sometimes('group_id', 'exists:groups,id', function (Fluent $input) {
|
|
|
|
return $input['group_id'] > 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:55:59 +02:00
|
|
|
/**
|
|
|
|
* Prepare the data for validation.
|
|
|
|
*
|
2022-12-09 10:52:17 +01:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2022-10-10 12:55:59 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function prepareForValidation()
|
|
|
|
{
|
|
|
|
$this->merge([
|
2022-11-22 15:15:52 +01:00
|
|
|
'otp_type' => strtolower($this->otp_type),
|
2022-10-10 12:55:59 +02:00
|
|
|
'algorithm' => strtolower($this->algorithm),
|
|
|
|
]);
|
2024-08-06 11:21:08 +02:00
|
|
|
|
|
|
|
if ($this->has('group_id') && $this->group_id === '') {
|
|
|
|
$this->merge([
|
|
|
|
'group_id' => null,
|
|
|
|
]);
|
|
|
|
}
|
2022-10-10 12:55:59 +02:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|