|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Requests\PasswordTokenRequest; |
|
6
|
|
|
use App\Models\PasswordReset; |
|
7
|
|
|
use App\Models\User; |
|
8
|
|
|
use App\Notifications\PasswordResetRequest; |
|
9
|
|
|
use App\Notifications\PasswordResetSuccess; |
|
10
|
|
|
use App\Services\TenantManager; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
class PasswordsController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
protected $tenantManager; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create a new controller instance. |
|
19
|
|
|
* |
|
20
|
|
|
* @param TenantManager $tenantManager |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(TenantManager $tenantManager) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->tenantManager = $tenantManager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function token(PasswordTokenRequest $request) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($this->tenantManager->loadTenantByUsername($request->email)) { |
|
|
|
|
|
|
32
|
|
|
$user = User::where('email', $request->email)->first(); |
|
33
|
|
|
|
|
34
|
|
|
if ($user) { |
|
35
|
|
|
$passwordReset = PasswordReset::updateOrCreate( |
|
36
|
|
|
['email' => $user->email], |
|
37
|
|
|
['email' => $user->email, 'token' => Str::random(60)] |
|
|
|
|
|
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
if ($passwordReset) { |
|
41
|
|
|
$user->notify(new PasswordResetRequest($passwordReset->token)); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
//always empty response regardless of actual result (for security reasons) |
|
47
|
|
|
return response()->json(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function reset(\App\Http\Requests\PasswordResetRequest $request) |
|
51
|
|
|
{ |
|
52
|
|
|
$passwordReset = PasswordReset::where('token', $request->get('token'))->first(); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if (!$passwordReset) { |
|
55
|
|
|
return response()->json(['message' => 'This password reset token is invalid.'], 404); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($this->tenantManager->loadTenantByUsername($passwordReset->email)) { |
|
59
|
|
|
$user = User::where('email', $passwordReset->email)->first(); |
|
60
|
|
|
|
|
61
|
|
|
if (!$user) { |
|
62
|
|
|
return response()->json(['message' => 'We can\'t find a user with that e-mail address.'], 404); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$user->password = bcrypt($request->get('password')); |
|
66
|
|
|
$user->save(); |
|
67
|
|
|
$passwordReset->delete(); |
|
68
|
|
|
|
|
69
|
|
|
$user->notify(new PasswordResetSuccess()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return response()->json(['message' => 'Password has been changed.']); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|