1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Fort\Http\Requests\Frontarea; |
6
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
8
|
|
|
|
9
|
|
|
class AccountSettingsRequest extends FormRequest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Determine if the user is authorized to make this request. |
13
|
|
|
* |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function authorize() |
17
|
|
|
{ |
18
|
|
|
return true; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Prepare the data for validation. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
protected function prepareForValidation() |
27
|
|
|
{ |
28
|
|
|
$data = $this->all(); |
29
|
|
|
|
30
|
|
|
$country = $data['country_code'] ?? null; |
31
|
|
|
$email = $data['email'] ?? null; |
32
|
|
|
$phone = $data['phone'] ?? null; |
33
|
|
|
$user = $this->user(); |
34
|
|
|
$twoFactor = $user->getTwoFactor(); |
35
|
|
|
|
36
|
|
|
if (empty($data['password'])) { |
37
|
|
|
unset($data['password'], $data['password_confirmation']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($email !== $user->email) { |
41
|
|
|
$data['email_verified'] = false; |
42
|
|
|
$data['email_verified_at'] = null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($phone !== $user->phone) { |
46
|
|
|
$data['phone_verified'] = false; |
47
|
|
|
$data['phone_verified_at'] = null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($twoFactor && (isset($data['phone_verified']) || $country !== $user->country_code)) { |
51
|
|
|
array_set($twoFactor, 'phone.enabled', false); |
52
|
|
|
$data['two_factor'] = $twoFactor; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->replace($data); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the validation rules that apply to the request. |
60
|
|
|
* |
61
|
|
|
* @return array |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function rules() |
64
|
|
|
{ |
65
|
|
|
$user = $this->user(); |
66
|
|
|
$user->updateRulesUniques(); |
67
|
|
|
$rules = $user->getRules(); |
68
|
|
|
|
69
|
|
|
// Attach attribute rules |
70
|
|
|
$user->getEntityAttributes()->each(function ($attribute, $attributeSlug) use (&$rules) { |
71
|
|
|
switch ($attribute->type) { |
72
|
|
|
case 'datetime': |
|
|
|
|
73
|
|
|
$type = 'date'; |
74
|
|
|
break; |
75
|
|
|
case 'text': |
|
|
|
|
76
|
|
|
case 'varchar': |
|
|
|
|
77
|
|
|
$type = 'string'; |
78
|
|
|
break; |
79
|
|
|
default: |
|
|
|
|
80
|
|
|
$type = $attribute->type; |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$rule = ($attribute->is_required ? 'required|' : 'nullable|').$type; |
85
|
|
|
$rules[$attributeSlug.($attribute->is_collection ? '.*' : '')] = $rule; |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
$rules['password'] = 'sometimes|required|confirmed|min:'.config('rinvex.fort.password_min_chars'); |
89
|
|
|
|
90
|
|
|
return $rules; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.