ldc-groupe/app/Http/Requests/Back/RetrieveDataFromTokenRequest.php

47 lines
1.3 KiB
PHP

<?php
namespace App\Http\Requests\Back;
use App\Traits\Requests\Api\HasLaravelFailedValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Crypt;
class RetrieveDataFromTokenRequest extends FormRequest
{
use HasLaravelFailedValidation;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return auth('backend')->check() and $this->ajax();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'token' => [
'required',
'string',
function ($attribute, $value, $fail) {
try {
$payload = json_decode(Crypt::decryptString($value), true);
if (!is_array($payload) || !isset($payload['modelName'], $payload['modelId'])) {
$fail("Le {$attribute} est invalide.");
}
} catch (\Illuminate\Contracts\Encryption\DecryptException $e) {
$fail("Le {$attribute} est invalide.");
}
},
],
];
}
}