Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

UserRequest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 53.85%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 59
ccs 14
cts 26
cp 0.5385
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
B rules() 0 42 10
1
<?php
2
3
namespace A17\Twill\Http\Requests\Admin;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Facades\Crypt;
7
use PragmaRX\Google2FA\Google2FA;
8
9
class UserRequest extends Request
10
{
11
    /**
12
     * Determines if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16 4
    public function authorize()
17
    {
18 4
        return true;
19
    }
20
21
    /**
22
     * Gets the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26 4
    public function rules()
27
    {
28 4
        switch ($this->method()) {
29 4
            case 'POST':
30
                {
31
                    return [
32 4
                        'name' => 'required',
33 4
                        'email' => 'required|email|unique:' . config('twill.users_table', 'twill_users') . ',email',
34 4
                        'role' => 'required|not_in:SUPERADMIN',
35
                    ];
36
                }
37 1
            case 'PUT':
38
                {
39
                    return [
40 1
                        'name' => 'required',
41 1
                        'role' => 'not_in:SUPERADMIN',
42 1
                        'email' => 'required|email|unique:' . config('twill.users_table', 'twill_users') . ',email,' . $this->route('user'),
43 1
                        'verify-code' => function ($attribute, $value, $fail) {
44
                            $user = Auth::guard('twill_users')->user();
45
                            $with2faSettings = config('twill.enabled.users-2fa') && $user->id == $this->route('user');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
47
                            if ($with2faSettings) {
48
                                $userIsEnabling = $this->get('google_2fa_enabled') && !$user->google_2fa_enabled;
0 ignored issues
show
Bug introduced by
Accessing google_2fa_enabled on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
                                $userIsDisabling = !$this->get('google_2fa_enabled') && $user->google_2fa_enabled;
50
51
                                $shouldValidateOTP = $userIsEnabling || $userIsDisabling;
52
53
                                if ($shouldValidateOTP) {
54
                                    $valid = (new Google2FA)->verifyKey($user->google_2fa_secret, $value ?? '');
0 ignored issues
show
Bug introduced by
Accessing google_2fa_secret on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
56
                                    if (!$valid) {
57
                                        $fail('Your one time password is invalid.');
58
                                    }
59
                                }
60
                            }
61 1
                        },
62
                    ];
63
                }
64
            default:break;
65
        }
66
67
        return [];
68
69
    }
70
71
}
72