1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Fort\Http\Requests\Adminarea; |
6
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
8
|
|
|
|
9
|
|
|
class UserFormRequest 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
|
|
|
$user = $this->route('user') ?? app('rinvex.fort.user'); |
31
|
|
|
$country = $data['country_code'] ?? null; |
32
|
|
|
$twoFactor = $user->getTwoFactor(); |
33
|
|
|
|
34
|
|
|
$data['email_verified'] = $this->get('email_verified', false); |
35
|
|
|
$data['phone_verified'] = $this->get('phone_verified', false); |
36
|
|
|
|
37
|
|
|
if ($user->exists && empty($data['password'])) { |
38
|
|
|
unset($data['password'], $data['password_confirmation']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Update email verification date |
42
|
|
|
if ($data['email_verified'] && $user->email_verified !== $data['email_verified']) { |
43
|
|
|
$data['email_verified_at'] = now(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Update phone verification date |
47
|
|
|
if ($data['phone_verified'] && $user->phone_verified !== $data['phone_verified']) { |
48
|
|
|
$data['phone_verified_at'] = now(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Set abilities |
52
|
|
|
if ($this->user()->can('grant-abilities') && $data['abilities']) { |
53
|
|
|
$data['abilities'] = array_map('intval', $data['abilities']); |
54
|
|
|
$data['abilities'] = $this->user()->isSuperadmin() ? $data['abilities'] |
55
|
|
|
: array_intersect($this->user()->allAbilities->pluck('id')->toArray(), $data['abilities']); |
56
|
|
|
} else { |
57
|
|
|
unset($data['abilities']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Set roles |
61
|
|
|
if ($this->user()->can('assign-roles') && $data['roles']) { |
62
|
|
|
$data['roles'] = array_map('intval', $data['roles']); |
63
|
|
|
$data['roles'] = $this->user()->isSuperadmin() ? $data['roles'] |
64
|
|
|
: array_intersect($this->user()->roles->pluck('id')->toArray(), $data['roles']); |
65
|
|
|
} else { |
66
|
|
|
unset($data['roles']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $user->country_code)) { |
70
|
|
|
array_set($twoFactor, 'phone.enabled', false); |
71
|
|
|
$data['two_factor'] = $twoFactor; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->replace($data); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the validation rules that apply to the request. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function rules(): array |
83
|
|
|
{ |
84
|
|
|
$user = $this->route('user') ?? app('rinvex.fort.user'); |
85
|
|
|
$user->updateRulesUniques(); |
86
|
|
|
$rules = $user->getRules(); |
87
|
|
|
|
88
|
|
|
$rules['roles'] = 'nullable|array'; |
89
|
|
|
$rules['abilities'] = 'nullable|array'; |
90
|
|
|
$rules['password'] = $user->exists |
91
|
|
|
? 'confirmed|min:'.config('rinvex.fort.password_min_chars') |
92
|
|
|
: 'required|confirmed|min:'.config('rinvex.fort.password_min_chars'); |
93
|
|
|
|
94
|
|
|
return $rules; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|