1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Http\Requests\Adminarea; |
6
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
8
|
|
|
|
9
|
|
|
class SentinelFormRequest extends FormRequest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Determine if the user is authorized to make this request. |
13
|
|
|
* |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function authorize(): bool |
17
|
|
|
{ |
18
|
|
|
return true; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Prepare the data for validation. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
protected function prepareForValidation(): void |
27
|
|
|
{ |
28
|
|
|
$data = $this->all(); |
29
|
|
|
|
30
|
|
|
$sentinel = $this->route('sentinel') ?? app('cortex.auth.sentinel'); |
31
|
|
|
$country = $data['country_code'] ?? null; |
32
|
|
|
$twoFactor = $sentinel->getTwoFactor(); |
33
|
|
|
|
34
|
|
|
$data['email_verified'] = $this->get('email_verified', false); |
35
|
|
|
$data['phone_verified'] = $this->get('phone_verified', false); |
36
|
|
|
|
37
|
|
|
if ($sentinel->exists && empty($data['password'])) { |
38
|
|
|
unset($data['password'], $data['password_confirmation']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Update email verification date |
42
|
|
|
if ($data['email_verified'] && $sentinel->email_verified !== $data['email_verified']) { |
43
|
|
|
$data['email_verified_at'] = now(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Update phone verification date |
47
|
|
|
if ($data['phone_verified'] && $sentinel->phone_verified !== $data['phone_verified']) { |
48
|
|
|
$data['phone_verified_at'] = now(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Set abilities |
52
|
|
|
if ($this->user($this->get('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) { |
53
|
|
|
$data['abilities'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('abilities', []) |
54
|
|
|
: $this->user($this->get('guard'))->abilities->pluck('id')->intersect($this->get('abilities', []))->toArray(); |
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
unset($data['abilities']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Set roles |
60
|
|
|
if ($this->user($this->get('guard'))->can('assign', \Cortex\Auth\Models\Role::class) && $data['roles']) { |
61
|
|
|
$data['roles'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('roles', []) |
62
|
|
|
: $this->user($this->get('guard'))->roles->pluck('id')->intersect($this->get('roles', []))->toArray(); |
63
|
|
|
} else { |
64
|
|
|
unset($data['roles']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $sentinel->country_code)) { |
68
|
|
|
array_set($twoFactor, 'phone.enabled', false); |
69
|
|
|
$data['two_factor'] = $twoFactor; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->replace($data); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the validation rules that apply to the request. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function rules(): array |
81
|
|
|
{ |
82
|
|
|
$sentinel = $this->route('sentinel') ?? app('cortex.auth.sentinel'); |
83
|
|
|
$sentinel->updateRulesUniques(); |
84
|
|
|
$rules = $sentinel->getRules(); |
85
|
|
|
|
86
|
|
|
$rules['roles'] = 'nullable|array'; |
87
|
|
|
$rules['abilities'] = 'nullable|array'; |
88
|
|
|
$rules['password'] = $sentinel->exists |
89
|
|
|
? 'confirmed|min:'.config('cortex.auth.password_min_chars') |
90
|
|
|
: 'required|confirmed|min:'.config('cortex.auth.password_min_chars'); |
91
|
|
|
|
92
|
|
|
return $rules; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.