Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

AdminFormRequest::rules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class AdminFormRequest 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
        $admin = $this->route('admin') ?? app('cortex.auth.admin');
31
        $country = $data['country_code'] ?? null;
32
        $twoFactor = $admin->getTwoFactor();
33
34
        $data['email_verified'] = $this->get('email_verified', false);
35
        $data['phone_verified'] = $this->get('phone_verified', false);
36
37
        if ($admin->exists && empty($data['password'])) {
38
            unset($data['password'], $data['password_confirmation']);
39
        }
40
41
        // Update email verification date
42
        if ($data['email_verified'] && $admin->email_verified !== $data['email_verified']) {
43
            $data['email_verified_at'] = now();
44
        }
45
46
        // Update phone verification date
47
        if ($data['phone_verified'] && $admin->phone_verified !== $data['phone_verified']) {
48
            $data['phone_verified_at'] = now();
49
        }
50
51
        // Set abilities
52
        if ($this->user($this->get('guard'))->can('grant', \Cortex\Auth\Models\Ability::class)) {
53
            $data['abilities'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('abilities', [])
54
                : $this->user($this->get('guard'))->abilities->pluck('id')->intersect($this->get('abilities', []))->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
55
        } else {
56
            unset($data['abilities']);
57
        }
58
59
        // Set roles
60
        if ($this->user($this->get('guard'))->can('assign', \Cortex\Auth\Models\Role::class) && $data['roles']) {
61
            $data['roles'] = $this->user($this->get('guard'))->can('superadmin') ? $this->get('roles', [])
62
                : $this->user($this->get('guard'))->roles->pluck('id')->intersect($this->get('roles', []))->toArray();
63
        } else {
64
            unset($data['roles']);
65
        }
66
67
        if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $admin->country_code)) {
68
            array_set($twoFactor, 'phone.enabled', false);
69
            $data['two_factor'] = $twoFactor;
70
        }
71
72
        $this->replace($data);
73
    }
74
75
    /**
76
     * Get the validation rules that apply to the request.
77
     *
78
     * @return array
79
     */
80
    public function rules(): array
81
    {
82
        $admin = $this->route('admin') ?? app('cortex.auth.admin');
83
        $admin->updateRulesUniques();
84
        $rules = $admin->getRules();
85
86
        $rules['roles'] = 'nullable|array';
87
        $rules['abilities'] = 'nullable|array';
88
        $rules['password'] = $admin->exists
89
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
90
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
91
92
        return $rules;
93
    }
94
}
95