1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CRUDlexUser package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CRUDlex; |
13
|
|
|
|
14
|
|
|
use CRUDlex\UserSetup; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class offers some features to implement a password reset flow. |
18
|
|
|
*/ |
19
|
|
|
class PasswordReset { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Holds the user Data instance. |
23
|
|
|
*/ |
24
|
|
|
protected $userData; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Holds the password reset Data instance. |
28
|
|
|
*/ |
29
|
|
|
protected $passwordResetData; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Gets the password reset of a token but only if it is younger than 48h. |
33
|
|
|
* |
34
|
|
|
* @param string $token |
35
|
|
|
* the password reset token |
36
|
|
|
* |
37
|
|
|
* @return null|CRUDlex\Entity |
38
|
|
|
* the password reset request |
39
|
|
|
*/ |
40
|
|
|
protected function getValidPasswordReset($token) { |
41
|
|
|
$passwordResets = $this->passwordResetData->listEntries(array('token' => $token)); |
42
|
|
|
if (count($passwordResets) !== 1) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
$passwordReset = $passwordResets[0]; |
46
|
|
|
|
47
|
|
|
$createdAt = $passwordReset->get('created_at'); |
48
|
|
|
if (strtotime($createdAt . ' UTC') < time() - 2 * 24 * 60 * 60 || $passwordReset->get('reset')) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $passwordReset; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
* |
58
|
|
|
* @param CRUDlex\Data $userData |
59
|
|
|
* the user data instance |
60
|
|
|
* @param CRUDlex\Data $passwordResetData |
61
|
|
|
* the password reset data instance |
62
|
|
|
*/ |
63
|
|
|
public function __construct($userData, $passwordResetData) { |
64
|
|
|
$this->userData = $userData; |
65
|
|
|
$this->passwordResetData = $passwordResetData; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates a password reset request. |
70
|
|
|
* |
71
|
|
|
* @param string $identifyingField |
72
|
|
|
* the identifying field to grab an user, likely the email |
73
|
|
|
* @param string $identifyingValue |
74
|
|
|
* the identifying value to grab an user, likely the email |
75
|
|
|
* |
76
|
|
|
* @return null|string |
77
|
|
|
* the token of the password reset instance ready to be send to the user via |
78
|
|
|
* a secondary channel like email; might be null if the user could not be |
79
|
|
|
* identified uniquly via the given parameters: either zero or more than one |
80
|
|
|
* users were found |
81
|
|
|
*/ |
82
|
|
|
public function requestPasswordReset($identifyingField, $identifyingValue) { |
83
|
|
|
|
84
|
|
|
$users = $this->userData->listEntries(array($identifyingField => $identifyingValue)); |
85
|
|
|
if (count($users) !== 1) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$user = $users[0]; |
90
|
|
|
$userSetup = new UserSetup(); |
91
|
|
|
|
92
|
|
|
do { |
93
|
|
|
$token = $userSetup->getSalt(32); |
94
|
|
|
$tokenFound = $this->passwordResetData->countBy($this->passwordResetData->getDefinition()->getTable(), array('token' => $token), array('token' => '='), true) === 0; |
95
|
|
|
} while (!$tokenFound); |
96
|
|
|
|
97
|
|
|
$passwordReset = $this->passwordResetData->createEmpty(); |
98
|
|
|
$passwordReset->set('user', $user->get('id')); |
99
|
|
|
$passwordReset->set('token', $token); |
100
|
|
|
$this->passwordResetData->create($passwordReset); |
101
|
|
|
|
102
|
|
|
return $token; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Resets the password of an user belonging to the given password reset |
107
|
|
|
* token. |
108
|
|
|
* |
109
|
|
|
* @param string $token |
110
|
|
|
* the password reset token |
111
|
|
|
* @param string $newPassword |
112
|
|
|
* the new password |
113
|
|
|
* |
114
|
|
|
* @return boolean |
115
|
|
|
* true on success, false on failure with one of this reasons: |
116
|
|
|
* - no or more than one password reset request found for this token |
117
|
|
|
* - the password request for this token is older than 48h |
118
|
|
|
* - the password request for this token has already been used |
119
|
|
|
*/ |
120
|
|
|
public function resetPassword($token, $newPassword) { |
121
|
|
|
$passwordReset = $this->getValidPasswordReset($token); |
122
|
|
|
if ($passwordReset === null) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$user = $this->userData->get($passwordReset->get('user')); |
127
|
|
|
$user->set('password', $newPassword); |
128
|
|
|
$this->userData->update($user); |
129
|
|
|
|
130
|
|
|
$reset = gmdate('Y-m-d H:i:s'); |
131
|
|
|
$passwordReset->set('reset', $reset); |
132
|
|
|
$this->passwordResetData->update($passwordReset); |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|