Completed
Push — master ( 20a6e3...887da8 )
by Abdelrahman
04:09 queued 02:12
created

AdminFormRequest::rules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
        $data['email_verified'] = $this->get('email_verified', false);
38
        $data['phone_verified'] = $this->get('phone_verified', false);
39
40
        if ($admin->exists && empty($data['password'])) {
41
            unset($data['password'], $data['password_confirmation']);
42
        }
43
44
        // Update email verification date
45
        if ($data['email_verified'] && $admin->email_verified !== $data['email_verified']) {
46
            $data['email_verified_at'] = now();
47
        }
48
49
        // Update phone verification date
50
        if ($data['phone_verified'] && $admin->phone_verified !== $data['phone_verified']) {
51
            $data['phone_verified_at'] = now();
52
        }
53
54
        // Set abilities
55
        if (! empty($data['abilities'])) {
56
            if ($this->user($this->route('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
57
                $abilities = array_map('intval', $this->get('abilities', []));
58
                $data['abilities'] = $this->user($this->route('guard'))->can('superadmin') ? $abilities
59
                    : $this->user($this->route('guard'))->getAbilities()->pluck('id')->intersect($abilities)->toArray();
60
            } else {
61
                unset($data['abilities']);
62
            }
63
        }
64
65
        // Set roles
66
        if (! empty($data['roles'])) {
67
            if ($data['roles'] && $this->user($this->route('guard'))->can('assign', \Cortex\Auth\Models\Role::class)) {
68
                $roles = array_map('intval', $this->get('roles', []));
69
                $data['roles'] = $this->user($this->route('guard'))->can('superadmin') ? $roles
70
                    : $this->user($this->route('guard'))->roles->pluck('id')->intersect($roles)->toArray();
71
            } else {
72
                unset($data['roles']);
73
            }
74
        }
75
76
        if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $admin->country_code)) {
77
            array_set($twoFactor, 'phone.enabled', false);
78
            $data['two_factor'] = $twoFactor;
79
        }
80
81
        $this->replace($data);
82
    }
83
84
    /**
85
     * Configure the validator instance.
86
     *
87
     * @param \Illuminate\Validation\Validator $validator
88
     *
89
     * @return void
90
     */
91
    public function withValidator($validator): void
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        // Sanitize input data before submission
94
        $this->replace($this->escape($this->all()));
95
    }
96
97
    /**
98
     * Get the validation rules that apply to the request.
99
     *
100
     * @return array
101
     */
102
    public function rules(): array
103
    {
104
        $admin = $this->route('admin') ?? app('cortex.auth.admin');
105
        $admin->updateRulesUniques();
106
        $rules = $admin->getRules();
107
108
        $rules['roles'] = 'nullable|array';
109
        $rules['abilities'] = 'nullable|array';
110
        $rules['password'] = $admin->exists
111
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
112
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
113
114
        return $rules;
115
    }
116
}
117