1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Service; |
13
|
|
|
|
14
|
|
|
use Da\User\Contracts\MailChangeStrategyInterface; |
15
|
|
|
use Da\User\Contracts\ServiceInterface; |
16
|
|
|
use Da\User\Model\Token; |
17
|
|
|
use Da\User\Model\User; |
18
|
|
|
use Da\User\Query\TokenQuery; |
19
|
|
|
use Da\User\Query\UserQuery; |
20
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
21
|
|
|
use Yii; |
22
|
|
|
|
23
|
|
|
class EmailChangeService implements ServiceInterface |
24
|
|
|
{ |
25
|
|
|
use ModuleAwareTrait; |
26
|
|
|
|
27
|
|
|
protected $code; |
28
|
|
|
protected $model; |
29
|
|
|
protected $tokenQuery; |
30
|
|
|
protected $userQuery; |
31
|
|
|
|
32
|
1 |
|
public function __construct($code, User $model, TokenQuery $tokenQuery, UserQuery $userQuery) |
33
|
|
|
{ |
34
|
1 |
|
$this->code = $code; |
35
|
1 |
|
$this->model = $model; |
36
|
1 |
|
$this->tokenQuery = $tokenQuery; |
37
|
1 |
|
$this->userQuery = $userQuery; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
public function run() |
41
|
|
|
{ |
42
|
|
|
/** @var Token $token */ |
43
|
1 |
|
$token = $this->tokenQuery |
44
|
1 |
|
->whereUserId($this->model->id) |
45
|
1 |
|
->whereCode($this->code) |
46
|
1 |
|
->whereIsTypes([Token::TYPE_CONFIRM_NEW_EMAIL, Token::TYPE_CONFIRM_OLD_EMAIL]) |
47
|
1 |
|
->one(); |
48
|
|
|
|
49
|
1 |
|
if ($token === null || $token->getIsExpired()) { |
50
|
|
|
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your confirmation token is invalid or expired')); |
51
|
|
|
|
52
|
|
|
return false; |
53
|
|
|
} |
54
|
1 |
|
$token->delete(); |
55
|
1 |
|
if (empty($this->model->unconfirmed_email)) { |
56
|
|
|
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'An error occurred processing your request')); |
57
|
1 |
|
} elseif ($this->userQuery->whereEmail($this->model->unconfirmed_email)->exists() === false) { |
58
|
1 |
|
if ($this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_SECURE) { |
59
|
|
|
if ($token->type === Token::TYPE_CONFIRM_NEW_EMAIL) { |
60
|
|
|
$this->model->flags |= User::NEW_EMAIL_CONFIRMED; |
61
|
|
|
Yii::$app->session->setFlash( |
62
|
|
|
'success', |
63
|
|
|
Yii::t( |
64
|
|
|
'usuario', |
65
|
|
|
'Awesome, almost there. Now you need to click the confirmation link sent to your old email address.' |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
} elseif ($token->type === Token::TYPE_CONFIRM_OLD_EMAIL) { |
69
|
|
|
$this->model->flags |= User::OLD_EMAIL_CONFIRMED; |
70
|
|
|
Yii::$app->session->setFlash( |
71
|
|
|
'success', |
72
|
|
|
Yii::t( |
73
|
|
|
'usuario', |
74
|
|
|
'Awesome, almost there. Now you need to click the confirmation link sent to your new email address.' |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
1 |
|
if ((($this->model->flags & User::NEW_EMAIL_CONFIRMED) && ($this->model->flags & User::OLD_EMAIL_CONFIRMED)) |
80
|
1 |
|
|| $this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_DEFAULT |
81
|
|
|
) { |
82
|
1 |
|
$this->model->email = $this->model->unconfirmed_email; |
83
|
1 |
|
$this->model->unconfirmed_email = null; |
84
|
1 |
|
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Your email address has been changed')); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->model->save(false); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|