|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Security\Password; |
|
4
|
|
|
|
|
5
|
|
|
use Mouf\Security\Password\Api\ForgotYourPasswordDao; |
|
6
|
|
|
use Mouf\Security\Password\Api\TokenNotFoundException; |
|
7
|
|
|
use Mouf\Security\UserService\UserService; |
|
8
|
|
|
use Mouf\Utils\Value\ValueInterface; |
|
9
|
|
|
use Psr\Http\Message\UriInterface; |
|
10
|
|
|
use Ramsey\Uuid\Uuid; |
|
11
|
|
|
use TheCodingMachine\Mail\Template\SwiftTwigMailTemplate; |
|
12
|
|
|
|
|
13
|
|
|
class ForgotYourPasswordService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ForgotYourPasswordDao |
|
17
|
|
|
*/ |
|
18
|
|
|
private $forgetYourPasswordDao; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Swift_Mailer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $swiftMailer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var SwiftTwigMailTemplate |
|
27
|
|
|
*/ |
|
28
|
|
|
private $mailTemplate; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var UserService |
|
32
|
|
|
*/ |
|
33
|
|
|
private $userService; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ForgotYourPasswordService constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ForgotYourPasswordDao $forgetYourPasswordDao |
|
39
|
|
|
* @param \Swift_Mailer $swiftMailer |
|
40
|
|
|
* @param ValueInterface|string $from |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(ForgotYourPasswordDao $forgetYourPasswordDao, \Swift_Mailer $swiftMailer, SwiftTwigMailTemplate $mailTemplate, UserService $userService) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->forgetYourPasswordDao = $forgetYourPasswordDao; |
|
45
|
|
|
$this->swiftMailer = $swiftMailer; |
|
46
|
|
|
$this->mailTemplate = $mailTemplate; |
|
47
|
|
|
$this->userService = $userService; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Generates and sends via mail a token for user whose mail is $email, stores the token in database and returns the token. |
|
52
|
|
|
* Throws an EmailNotFoundException if the email is not part of the database. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $email |
|
55
|
|
|
* @param UriInterface $resetPasswordUrl The URL to reset the password. |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Mouf\Security\Password\Api\EmailNotFoundException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function sendMail(string $email, UriInterface $resetPasswordUrl) |
|
60
|
|
|
{ |
|
61
|
|
|
// Let's generate a new token |
|
62
|
|
|
$token = Uuid::uuid4()->toString(); |
|
63
|
|
|
|
|
64
|
|
|
// Let's store this new token |
|
65
|
|
|
$this->forgetYourPasswordDao->setToken($email, $token); |
|
66
|
|
|
|
|
67
|
|
|
$user = $this->forgetYourPasswordDao->getUserByToken($token); |
|
68
|
|
|
|
|
69
|
|
|
$resetPasswordUrl = $resetPasswordUrl->withQuery('token='.urlencode($token)); |
|
70
|
|
|
|
|
71
|
|
|
$mail = $this->mailTemplate->renderMail([ |
|
72
|
|
|
'url' => (string) $resetPasswordUrl, |
|
73
|
|
|
'website' => $resetPasswordUrl->getHost(), |
|
74
|
|
|
'user' => $user->getLogin(), |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
$mail->setTo($email); |
|
78
|
|
|
|
|
79
|
|
|
$this->swiftMailer->send($mail); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns true if a token is valid, false otherwise. |
|
84
|
|
|
*/ |
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $token |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function checkToken(string $token) : bool |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
|
|
$this->forgetYourPasswordDao->getUserByToken($token); |
|
94
|
|
|
} catch (TokenNotFoundException $e) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Uses the $token to replace the $password. |
|
103
|
|
|
* The token will no longer be usable. |
|
104
|
|
|
* Just after this action, the user is logged (using the UserService). |
|
105
|
|
|
* |
|
106
|
|
|
* Throws an TokenNotFoundException if the token is not part of the database. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $token |
|
109
|
|
|
* @param string $password |
|
110
|
|
|
* |
|
111
|
|
|
* @throws TokenNotFoundException |
|
112
|
|
|
*/ |
|
113
|
|
|
public function useToken(string $token, string $password) |
|
114
|
|
|
{ |
|
115
|
|
|
$user = $this->forgetYourPasswordDao->getUserByToken($token); |
|
116
|
|
|
|
|
117
|
|
|
$this->forgetYourPasswordDao->setPasswordAndDiscardToken($token, $password); |
|
118
|
|
|
|
|
119
|
|
|
$this->userService->loginWithoutPassword($user->getLogin()); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.