|
1
|
|
|
<?php |
|
2
|
|
|
namespace site\models; |
|
3
|
|
|
|
|
4
|
|
|
use common\models\User; |
|
5
|
|
|
use yii\base\Model; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* change email form |
|
10
|
|
|
*/ |
|
11
|
|
|
class ChangeEmailForm extends Model { |
|
12
|
|
|
public $desired_email; |
|
13
|
|
|
private $user; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Creates a form model |
|
17
|
|
|
* |
|
18
|
|
|
* @param object $user |
|
19
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(\common\interfaces\UserInterface $user, $config = []) { |
|
22
|
|
|
$this->user = $user; |
|
23
|
|
|
parent::__construct($config); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function rules() { |
|
30
|
|
|
return [ |
|
31
|
|
|
['desired_email', 'email'], |
|
32
|
|
|
['desired_email', 'required'], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function attributeLabels() { |
|
37
|
|
|
return [ |
|
38
|
|
|
'desired_email' => 'New Email', |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Sets verification tokens and sends verification emails |
|
44
|
|
|
* |
|
45
|
|
|
* @return Boolean whether or not the operation was successful |
|
46
|
|
|
*/ |
|
47
|
|
|
public function changeEmail() { |
|
48
|
|
|
// check that the desired_email is not already the email of another user |
|
49
|
|
|
$user = $this->user->findByEmail($this->desired_email); |
|
|
|
|
|
|
50
|
|
|
// if it is, we do nothing and return true, don't leak the data |
|
51
|
|
|
if(!$user) { |
|
52
|
|
|
$this->user->desired_email = $this->desired_email; |
|
|
|
|
|
|
53
|
|
|
$this->user->generateChangeEmailToken(); |
|
|
|
|
|
|
54
|
|
|
$this->user->save(); |
|
55
|
|
|
|
|
56
|
|
|
$this->sendChangeEmailEmail(); |
|
57
|
|
|
} |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function sendChangeEmailEmail() { |
|
62
|
|
|
Yii::$app->mailer->compose('changeEmail', [ |
|
63
|
|
|
'current_email' => $this->user->email, |
|
|
|
|
|
|
64
|
|
|
'desired_email' => $this->desired_email, |
|
65
|
|
|
'token' => $this->user->change_email_token, |
|
|
|
|
|
|
66
|
|
|
])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name]) |
|
67
|
|
|
->setSubject(Yii::$app->name . ' -- your requested email change') |
|
68
|
|
|
->setTo($this->desired_email) |
|
69
|
|
|
->send(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|