Completed
Push — develop ( 10b573...34b970 )
by Abdelrahman
09:38
created

UserFormRequest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 88
rs 10
c 2
b 0
f 0
wmc 19
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
D prepareForValidation() 0 50 16
A rules() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Requests\Adminarea;
6
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class UserFormRequest 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
        $user = $this->route('user') ?? app('rinvex.fort.user');
31
        $country = $data['country_code'] ?? null;
32
        $twoFactor = $user->getTwoFactor();
33
34
        $data['email_verified'] = $this->get('email_verified', false);
35
        $data['phone_verified'] = $this->get('phone_verified', false);
36
37
        if ($user->exists && empty($data['password'])) {
38
            unset($data['password'], $data['password_confirmation']);
39
        }
40
41
        // Update email verification date
42
        if ($data['email_verified'] && $user->email_verified !== $data['email_verified']) {
43
            $data['email_verified_at'] = now();
44
        }
45
46
        // Update phone verification date
47
        if ($data['phone_verified'] && $user->phone_verified !== $data['phone_verified']) {
48
            $data['phone_verified_at'] = now();
49
        }
50
51
        // Set abilities
52
        if ($this->user()->can('grant-abilities') && $data['abilities']) {
53
            $data['abilities'] = array_map('intval', $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'] = array_map('intval', $data['roles']);
63
            $data['roles'] = $this->user()->isSuperadmin() ? $data['roles']
64
                : array_intersect($this->user()->roles->pluck('id')->toArray(), $data['roles']);
65
        } else {
66
            unset($data['roles']);
67
        }
68
69
        if ($twoFactor && (isset($data['phone_verified_at']) || $country !== $user->country_code)) {
70
            array_set($twoFactor, 'phone.enabled', false);
71
            $data['two_factor'] = $twoFactor;
72
        }
73
74
        $this->replace($data);
75
    }
76
77
    /**
78
     * Get the validation rules that apply to the request.
79
     *
80
     * @return array
81
     */
82
    public function rules(): array
83
    {
84
        $user = $this->route('user') ?? app('rinvex.fort.user');
85
        $user->updateRulesUniques();
86
        $rules = $user->getRules();
87
88
        $rules['roles'] = 'nullable|array';
89
        $rules['abilities'] = 'nullable|array';
90
        $rules['password'] = $user->exists
91
            ? 'confirmed|min:'.config('rinvex.fort.password_min_chars')
92
            : 'required|confirmed|min:'.config('rinvex.fort.password_min_chars');
93
94
        return $rules;
95
    }
96
}
97