1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Http\Requests\Adminarea; |
6
|
|
|
|
7
|
|
|
use Rinvex\Support\Traits\Escaper; |
8
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
9
|
|
|
|
10
|
|
|
class AdminFormRequest extends FormRequest |
11
|
|
|
{ |
12
|
|
|
use Escaper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Determine if the user is authorized to make this request. |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public function authorize(): bool |
20
|
|
|
{ |
21
|
|
|
return true; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Prepare the data for validation. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
protected function prepareForValidation(): void |
30
|
|
|
{ |
31
|
|
|
$data = $this->all(); |
32
|
|
|
|
33
|
|
|
$admin = $this->route('admin') ?? app('cortex.auth.admin'); |
34
|
|
|
$country = $data['country_code'] ?? null; |
35
|
|
|
$twoFactor = $admin->getTwoFactor(); |
36
|
|
|
|
37
|
|
|
if ($admin->exists && empty($data['password'])) { |
38
|
|
|
unset($data['password'], $data['password_confirmation']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Set abilities |
42
|
|
|
if (! empty($data['abilities'])) { |
43
|
|
|
if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) { |
44
|
|
|
$abilities = array_map('intval', $this->get('abilities', [])); |
45
|
|
|
$data['abilities'] = $this->user($this->route('guard'))->can('superadmin') ? $abilities |
46
|
|
|
: $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray(); |
47
|
|
|
} else { |
48
|
|
|
unset($data['abilities']); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Set roles |
53
|
|
|
if (! empty($data['roles'])) { |
54
|
|
|
if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) { |
55
|
|
|
$roles = array_map('intval', $this->get('roles', [])); |
56
|
|
|
$data['roles'] = $this->user($this->route('guard'))->can('superadmin') ? $roles |
57
|
|
|
: $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray(); |
58
|
|
|
} else { |
59
|
|
|
unset($data['roles']); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $admin->country_code)) { |
64
|
|
|
array_set($twoFactor, 'phone.enabled', false); |
65
|
|
|
$data['two_factor'] = $twoFactor; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->replace($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Configure the validator instance. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Validation\Validator $validator |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function withValidator($validator): void |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
// Sanitize input data before submission |
81
|
|
|
$this->replace($this->escape($this->all())); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the validation rules that apply to the request. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function rules(): array |
90
|
|
|
{ |
91
|
|
|
$admin = $this->route('admin') ?? app('cortex.auth.admin'); |
92
|
|
|
$admin->updateRulesUniques(); |
93
|
|
|
$rules = $admin->getRules(); |
94
|
|
|
|
95
|
|
|
$rules['roles'] = 'nullable|array'; |
96
|
|
|
$rules['abilities'] = 'nullable|array'; |
97
|
|
|
$rules['password'] = $admin->exists |
98
|
|
|
? 'confirmed|min:'.config('cortex.auth.password_min_chars') |
99
|
|
|
: 'required|confirmed|min:'.config('cortex.auth.password_min_chars'); |
100
|
|
|
|
101
|
|
|
return $rules; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.