1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Auth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Events\PasswordReset; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Facades\Hash; |
10
|
|
|
use Illuminate\Support\Facades\Password; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Illuminate\Validation\ValidationException; |
13
|
|
|
|
14
|
|
|
trait ResetsPasswords |
15
|
|
|
{ |
16
|
|
|
use RedirectsUsers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Display the password reset view for the given token. |
20
|
|
|
* |
21
|
|
|
* If no token is present, display the link request form. |
22
|
|
|
* |
23
|
|
|
* @param \Illuminate\Http\Request $request |
24
|
|
|
* @param string|null $token |
25
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
26
|
|
|
*/ |
27
|
|
|
public function showResetForm(Request $request, $token = null) |
28
|
|
|
{ |
29
|
|
|
return view('auth.passwords.reset')->with( |
|
|
|
|
30
|
|
|
['token' => $token, 'email' => $request->email] |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Reset the given user's password. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Http\Request $request |
38
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
39
|
|
|
*/ |
40
|
|
|
public function reset(Request $request) |
41
|
|
|
{ |
42
|
|
|
$request->validate($this->rules(), $this->validationErrorMessages()); |
43
|
|
|
|
44
|
|
|
// Here we will attempt to reset the user's password. If it is successful we |
45
|
|
|
// will update the password on an actual user model and persist it to the |
46
|
|
|
// database. Otherwise we will parse the error and return the response. |
47
|
|
|
$response = $this->broker()->reset( |
48
|
|
|
$this->credentials($request), function ($user, $password) { |
49
|
|
|
$this->resetPassword($user, $password); |
50
|
|
|
} |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
// If the password was successfully reset, we will redirect the user back to |
54
|
|
|
// the application's home authenticated view. If there is an error we can |
55
|
|
|
// redirect them back to where they came from with their error message. |
56
|
|
|
return $response == Password::PASSWORD_RESET |
57
|
|
|
? $this->sendResetResponse($request, $response) |
58
|
|
|
: $this->sendResetFailedResponse($request, $response); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the password reset validation rules. |
63
|
|
|
* |
64
|
|
|
* @return array |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
protected function rules() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'token' => 'required', |
70
|
|
|
'email' => 'required|email', |
71
|
|
|
'password' => 'required|confirmed|min:8', |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the password reset validation error messages. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function validationErrorMessages() |
81
|
|
|
{ |
82
|
|
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the password reset credentials from the request. |
87
|
|
|
* |
88
|
|
|
* @param \Illuminate\Http\Request $request |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function credentials(Request $request) |
92
|
|
|
{ |
93
|
|
|
return $request->only( |
94
|
|
|
'email', 'password', 'password_confirmation', 'token' |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Reset the given user's password. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Contracts\Auth\CanResetPassword $user |
102
|
|
|
* @param string $password |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
protected function resetPassword($user, $password) |
106
|
|
|
{ |
107
|
|
|
$this->setUserPassword($user, $password); |
108
|
|
|
|
109
|
|
|
$user->setRememberToken(Str::random(60)); |
110
|
|
|
|
111
|
|
|
$user->save(); |
112
|
|
|
|
113
|
|
|
event(new PasswordReset($user)); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->guard()->login($user); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the user's password. |
120
|
|
|
* |
121
|
|
|
* @param \Illuminate\Contracts\Auth\CanResetPassword $user |
122
|
|
|
* @param string $password |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
protected function setUserPassword($user, $password) |
126
|
|
|
{ |
127
|
|
|
$user->password = Hash::make($password); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the response for a successful password reset. |
132
|
|
|
* |
133
|
|
|
* @param \Illuminate\Http\Request $request |
134
|
|
|
* @param string $response |
135
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
136
|
|
|
*/ |
137
|
|
|
protected function sendResetResponse(Request $request, $response) |
138
|
|
|
{ |
139
|
|
|
if ($request->wantsJson()) { |
140
|
|
|
return new JsonResponse(['message' => trans($response)], 200); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return redirect($this->redirectPath()) |
144
|
|
|
->with('status', trans($response)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the response for a failed password reset. |
149
|
|
|
* |
150
|
|
|
* @param \Illuminate\Http\Request $request |
151
|
|
|
* @param string $response |
152
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
protected function sendResetFailedResponse(Request $request, $response) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
if ($request->wantsJson()) { |
157
|
|
|
throw ValidationException::withMessages([ |
158
|
|
|
'email' => [trans($response)], |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return redirect()->back() |
163
|
|
|
->withInput($request->only('email')) |
164
|
|
|
->withErrors(['email' => trans($response)]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the broker to be used during password reset. |
169
|
|
|
* |
170
|
|
|
* @return \Illuminate\Contracts\Auth\PasswordBroker |
171
|
|
|
*/ |
172
|
|
|
public function broker() |
173
|
|
|
{ |
174
|
|
|
return Password::broker(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the guard to be used during password reset. |
179
|
|
|
* |
180
|
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard |
181
|
|
|
*/ |
182
|
|
|
protected function guard() |
183
|
|
|
{ |
184
|
|
|
return Auth::guard(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: