|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Fort\Http\Requests\Adminarea; |
|
6
|
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
9
|
|
|
|
|
10
|
|
|
class UserFormRequest extends FormRequest |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Determine if the user is authorized to make this request. |
|
14
|
|
|
* |
|
15
|
|
|
* @return bool |
|
16
|
|
|
*/ |
|
17
|
|
|
public function authorize() |
|
18
|
|
|
{ |
|
19
|
|
|
return true; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Prepare the data for validation. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function prepareForValidation() |
|
28
|
|
|
{ |
|
29
|
|
|
$data = $this->all(); |
|
30
|
|
|
|
|
31
|
|
|
$user = $this->route('user') ?? app('rinvex.fort.user'); |
|
32
|
|
|
$country = $data['country_code'] ?? null; |
|
33
|
|
|
$twoFactor = $user->getTwoFactor(); |
|
34
|
|
|
|
|
35
|
|
|
$data['email_verified'] = $this->get('email_verified', false); |
|
36
|
|
|
$data['phone_verified'] = $this->get('phone_verified', false); |
|
37
|
|
|
|
|
38
|
|
|
if ($user->exists && empty($data['password'])) { |
|
39
|
|
|
unset($data['password'], $data['password_confirmation']); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Update email verification date |
|
43
|
|
|
if ($data['email_verified'] && $user->email_verified !== $data['email_verified']) { |
|
44
|
|
|
$data['email_verified_at'] = Carbon::now(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Update phone verification date |
|
48
|
|
|
if ($data['phone_verified'] && $user->phone_verified !== $data['phone_verified']) { |
|
49
|
|
|
$data['phone_verified_at'] = Carbon::now(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Set abilities |
|
53
|
|
|
if ($this->user()->can('grant-abilities') && $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'] = $this->user()->isSuperadmin() ? $data['roles'] |
|
63
|
|
|
: array_intersect($this->user()->roles->pluck('id')->toArray(), $data['roles']); |
|
64
|
|
|
} else { |
|
65
|
|
|
unset($data['roles']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $user->country_code)) { |
|
69
|
|
|
array_set($twoFactor, 'phone.enabled', false); |
|
70
|
|
|
$data['two_factor'] = $twoFactor; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->replace($data); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get the validation rules that apply to the request. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function rules() |
|
82
|
|
|
{ |
|
83
|
|
|
$user = $this->route('user') ?? app('rinvex.fort.user'); |
|
84
|
|
|
$user->updateRulesUniques(); |
|
85
|
|
|
$rules = $user->getRules(); |
|
86
|
|
|
|
|
87
|
|
|
// Attach attribute rules |
|
88
|
|
|
$user->getEntityAttributes()->each(function ($attribute, $attributeSlug) use (&$rules) { |
|
89
|
|
|
switch ($attribute->type) { |
|
90
|
|
|
case 'datetime': |
|
|
|
|
|
|
91
|
|
|
$type = 'date'; |
|
92
|
|
|
break; |
|
93
|
|
|
case 'text': |
|
|
|
|
|
|
94
|
|
|
case 'varchar': |
|
|
|
|
|
|
95
|
|
|
$type = 'string'; |
|
96
|
|
|
break; |
|
97
|
|
|
default: |
|
|
|
|
|
|
98
|
|
|
$type = $attribute->type; |
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$rule = ($attribute->is_required ? 'required|' : 'nullable|').$type; |
|
103
|
|
|
$rules[$attributeSlug.($attribute->is_collection ? '.*' : '')] = $rule; |
|
104
|
|
|
}); |
|
105
|
|
|
|
|
106
|
|
|
$rules['roles'] = 'nullable|array'; |
|
107
|
|
|
$rules['abilities'] = 'nullable|array'; |
|
108
|
|
|
$rules['password'] = $user->exists |
|
109
|
|
|
? 'confirmed|min:'.config('rinvex.fort.password_min_chars') |
|
110
|
|
|
: 'required|confirmed|min:'.config('rinvex.fort.password_min_chars'); |
|
111
|
|
|
|
|
112
|
|
|
return $rules; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.