Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — add-email-confirmation ( 0910fc...d2a0f5 )
by Pedro
28:04 queued 13:08
created

AuthenticatesUsers   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 49
c 4
b 2
f 0
dl 0
loc 216
rs 10
wmc 23

13 Methods

Rating   Name   Duplication   Size   Complexity  
A showLoginForm() 0 6 1
A sendFailedLoginResponse() 0 4 1
A credentials() 0 3 1
A username() 0 3 1
A validateLogin() 0 5 1
A loggedOut() 0 2 1
A authenticated() 0 2 1
A attemptLogin() 0 4 1
A login() 0 28 5
A sendLoginResponse() 0 13 3
A guard() 0 3 1
A logout() 0 15 3
A logoutIfEmailNotVerified() 0 19 3
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Auth;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Cookie;
9
use Illuminate\Validation\ValidationException;
10
11
trait AuthenticatesUsers
12
{
13
    use RedirectsUsers, ThrottlesLogins;
14
15
    /**
16
     * Show the application's login form.
17
     *
18
     * @return \Illuminate\Contracts\View\View
19
     */
20
    public function showLoginForm()
21
    {
22
        $this->data['title'] = trans('backpack::base.login'); // set the page title
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        $this->data['username'] = $this->username();
24
25
        return view(backpack_view('auth.login'), $this->data);
26
    }
27
28
    /**
29
     * Handle a login request to the application.
30
     *
31
     * @param  \Illuminate\Http\Request  $request
32
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
33
     *
34
     * @throws \Illuminate\Validation\ValidationException
35
     */
36
    public function login(Request $request)
37
    {
38
        $this->validateLogin($request);
39
40
        // If the class is using the ThrottlesLogins trait, we can automatically throttle
41
        // the login attempts for this application. We'll key this by the username and
42
        // the IP address of the client making these requests into this application.
43
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
44
            $this->hasTooManyLoginAttempts($request)) {
45
            $this->fireLockoutEvent($request);
46
47
            return $this->sendLockoutResponse($request);
48
        }
49
50
        if ($this->attemptLogin($request)) {
51
            if (config('backpack.base.setup_email_verification_routes', false)) {
52
                return $this->logoutIfEmailNotVerified($request);
53
            }
54
55
            return $this->sendLoginResponse($request);
56
        }
57
58
        // If the login attempt was unsuccessful we will increment the number of attempts
59
        // to login and redirect the user back to the login form. Of course, when this
60
        // user surpasses their maximum number of attempts they will get locked out.
61
        $this->incrementLoginAttempts($request);
62
63
        return $this->sendFailedLoginResponse($request);
64
    }
65
66
    /**
67
     * Validate the user login request.
68
     *
69
     * @param  \Illuminate\Http\Request  $request
70
     * @return void
71
     *
72
     * @throws \Illuminate\Validation\ValidationException
73
     */
74
    protected function validateLogin(Request $request)
75
    {
76
        $request->validate([
77
            $this->username() => 'required|string',
78
            'password' => 'required|string',
79
        ]);
80
    }
81
82
    /**
83
     * Attempt to log the user into the application.
84
     *
85
     * @param  \Illuminate\Http\Request  $request
86
     * @return bool
87
     */
88
    protected function attemptLogin(Request $request)
89
    {
90
        return $this->guard()->attempt(
91
            $this->credentials($request), $request->filled('remember')
92
        );
93
    }
94
95
    /**
96
     * Get the needed authorization credentials from the request.
97
     *
98
     * @param  \Illuminate\Http\Request  $request
99
     * @return array
100
     */
101
    protected function credentials(Request $request)
102
    {
103
        return $request->only($this->username(), 'password');
104
    }
105
106
    /**
107
     * Send the response after the user was authenticated.
108
     *
109
     * @param  \Illuminate\Http\Request  $request
110
     * @return \Illuminate\Http\Response
111
     */
112
    protected function sendLoginResponse(Request $request)
113
    {
114
        $request->session()->regenerate();
115
116
        $this->clearLoginAttempts($request);
117
118
        if ($response = $this->authenticated($request, $this->guard()->user())) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $this->authenticated($re...$this->guard()->user()) targeting Backpack\CRUD\app\Librar...sUsers::authenticated() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
119
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type void which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
120
        }
121
122
        return $request->wantsJson()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->wantsJs...($this->redirectPath()) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
123
                    ? new Response('', 204)
124
                    : redirect()->intended($this->redirectPath());
125
    }
126
127
    /**
128
     * The user has been authenticated.
129
     *
130
     * @param  \Illuminate\Http\Request  $request
131
     * @param  mixed  $user
132
     * @return mixed
133
     */
134
    protected function authenticated(Request $request, $user)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

134
    protected function authenticated(/** @scrutinizer ignore-unused */ Request $request, $user)

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

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

134
    protected function authenticated(Request $request, /** @scrutinizer ignore-unused */ $user)

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

Loading history...
135
    {
136
        //
137
    }
138
139
    /**
140
     * Get the failed login response instance.
141
     *
142
     * @param  \Illuminate\Http\Request  $request
143
     * @return \Symfony\Component\HttpFoundation\Response
144
     *
145
     * @throws \Illuminate\Validation\ValidationException
146
     */
147
    protected function sendFailedLoginResponse(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
    protected function sendFailedLoginResponse(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
148
    {
149
        throw ValidationException::withMessages([
150
            $this->username() => [trans('auth.failed')],
151
        ]);
152
    }
153
154
    /**
155
     * Get the login username to be used by the controller.
156
     *
157
     * @return string
158
     */
159
    public function username()
160
    {
161
        return 'email';
162
    }
163
164
    /**
165
     * Log the user out of the application.
166
     *
167
     * @param  \Illuminate\Http\Request  $request
168
     * @return \Illuminate\Http\Response
169
     */
170
    public function logout(Request $request)
171
    {
172
        $this->guard()->logout();
173
174
        $request->session()->invalidate();
175
176
        $request->session()->regenerateToken();
177
178
        if ($response = $this->loggedOut($request)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $this->loggedOut($request) targeting Backpack\CRUD\app\Librar...catesUsers::loggedOut() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
179
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type void which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
180
        }
181
182
        return $request->wantsJson()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->wantsJs...', 204) : redirect('/') also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
183
            ? new Response('', 204)
184
            : redirect('/');
185
    }
186
187
    /**
188
     * The user has logged out of the application.
189
     *
190
     * @param  \Illuminate\Http\Request  $request
191
     * @return mixed
192
     */
193
    protected function loggedOut(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    protected function loggedOut(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
194
    {
195
        //
196
    }
197
198
    /**
199
     * Get the guard to be used during authentication.
200
     *
201
     * @return \Illuminate\Contracts\Auth\StatefulGuard
202
     */
203
    protected function guard()
204
    {
205
        return Auth::guard();
206
    }
207
208
    private function logoutIfEmailNotVerified(Request $request): Response|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
209
    {
210
        $user = $this->guard()->user();
211
212
        // if the user is already verified, do nothing
213
        if ($user->email_verified_at) {
0 ignored issues
show
Bug introduced by
Accessing email_verified_at on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
214
            return $this->sendLoginResponse($request);
215
        }
216
        // user is not yet verified, log him out
217
        $this->guard()->logout();
218
219
        // add a cookie for 30m to remember the email address that needs to be verified
220
        Cookie::queue('backpack_email_verification', $user->{config('backpack.base.email_column')}, 30);
221
222
        if ($request->wantsJson()) {
223
            return new Response('Email verification required', 403);
224
        }
225
226
        return redirect(route('verification.notice'));
227
    }
228
}
229