|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\PasswordResetInterface; |
|
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection; |
|
7
|
|
|
use Swift_Mailer; |
|
8
|
|
|
use Swift_Message; |
|
9
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class PasswordResetRequestHandler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var UserRepositoryCollection |
|
16
|
|
|
*/ |
|
17
|
|
|
private $userRepositoryCollection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Swift_Mailer |
|
21
|
|
|
*/ |
|
22
|
|
|
private $mailer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var TranslatorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $translator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var RouterInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $router; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(UserRepositoryCollection $userRepositoryCollection, Swift_Mailer $mailer, TranslatorInterface $translator, RouterInterface $router) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->userRepositoryCollection = $userRepositoryCollection; |
|
37
|
|
|
$this->mailer = $mailer; |
|
38
|
|
|
$this->translator = $translator; |
|
39
|
|
|
$this->router = $router; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Creates a password reset token and sends an email to the user. |
|
44
|
|
|
* |
|
45
|
|
|
* @param PasswordResetRequest $command |
|
46
|
|
|
* |
|
47
|
|
|
* @return int |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle(PasswordResetRequest $command) |
|
50
|
|
|
{ |
|
51
|
|
|
$user = $command->getUser(); |
|
52
|
|
|
$user->generatePasswordResetToken(); |
|
53
|
|
|
$repository = $this->userRepositoryCollection->findRepositoryByClassName(get_class($user)); |
|
54
|
|
|
$repository->save($user); |
|
55
|
|
|
|
|
56
|
|
|
return $this->sendPasswordResetToken($user); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Sends the password reset token to the user. |
|
61
|
|
|
* |
|
62
|
|
|
* @param PasswordResetInterface $user |
|
63
|
|
|
* |
|
64
|
|
|
* @return int |
|
65
|
|
|
*/ |
|
66
|
|
|
private function sendPasswordResetToken(PasswordResetInterface $user) |
|
67
|
|
|
{ |
|
68
|
|
|
$messageBody = $this->getPasswordResetMessage($user); |
|
69
|
|
|
|
|
70
|
|
|
$message = Swift_Message::newInstance() |
|
71
|
|
|
->setSubject('Password reset requested') |
|
72
|
|
|
->setFrom('[email protected]') |
|
73
|
|
|
->setTo($user->getEmail()) |
|
74
|
|
|
->setBody($messageBody, 'text/plain'); |
|
75
|
|
|
|
|
76
|
|
|
return $this->mailer->send($message); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Creates the password reset message. |
|
81
|
|
|
* |
|
82
|
|
|
* @param PasswordResetInterface $user |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getPasswordResetMessage(PasswordResetInterface $user) |
|
87
|
|
|
{ |
|
88
|
|
|
$url = $this->router->generate('multi_user_reset_password'); |
|
89
|
|
|
$token = '?token='.$user->getPasswordResetToken(); |
|
90
|
|
|
|
|
91
|
|
|
return $this->translator->trans('sumocoders.multiuserbundle.mail.request_password', ['%link%' => $url.$token]); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: