-
Notifications
You must be signed in to change notification settings - Fork 138
/
PasswordRecoveryService.php
76 lines (61 loc) · 2.03 KB
/
PasswordRecoveryService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Factory\TokenFactory;
use Da\User\Model\User;
use Da\User\Query\UserQuery;
use Da\User\Traits\MailAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Exception;
use Yii;
use yii\web\NotFoundHttpException;
class PasswordRecoveryService implements ServiceInterface
{
use MailAwareTrait;
use ModuleAwareTrait;
protected $query;
protected $email;
protected $mailService;
protected $securityHelper;
public function __construct($email, MailService $mailService, UserQuery $query)
{
$this->email = $email;
$this->mailService = $mailService;
$this->query = $query;
}
public function run()
{
try {
Yii::$app->session->setFlash(
'info',
Yii::t('usuario', 'An email with instructions to create a new password has been sent to {email} if it is associated with an {appName} account. Your existing password has not been changed.', ['email' => $this->email, 'appName' => Yii::$app->name])
);
/** @var User $user */
$user = $this->query->whereEmail($this->email)->one();
if ($user === null) {
throw new NotFoundHttpException(Yii::t('usuario', 'User not found'));
}
$token = TokenFactory::makeRecoveryToken($user->id);
if (!$token) {
return false;
}
$this->mailService->setViewParam('user', $user);
$this->mailService->setViewParam('token', $token);
if (!$this->sendMail($user)) {
return false;
}
return true;
} catch (Exception $e) {
Yii::error($e->getMessage(), 'usuario');
return false;
}
}
}