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 |
|
|
|
|
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())) { |
|
|
|
|
119
|
|
|
return $response; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $request->wantsJson() |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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)) { |
|
|
|
|
179
|
|
|
return $response; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $request->wantsJson() |
|
|
|
|
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) |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|