1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\api\forms; |
4
|
|
|
|
5
|
|
|
use Base32\Base32; |
6
|
|
|
use Yii; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @class ChangeEmail |
10
|
|
|
* The form for validating the activation form |
11
|
|
|
*/ |
12
|
|
|
abstract class ChangeEmail extends \yii\base\model |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The new email to be changed |
16
|
|
|
* @var string $email |
17
|
|
|
*/ |
18
|
|
|
public $email; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The user's current password |
22
|
|
|
* @var string $password |
23
|
|
|
*/ |
24
|
|
|
public $password; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The user whose information we want to change |
28
|
|
|
* @var User $user |
29
|
|
|
*/ |
30
|
|
|
private $user; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the user object |
34
|
|
|
* @param User $user |
35
|
|
|
*/ |
36
|
|
|
public function setUser($user) |
37
|
|
|
{ |
38
|
|
|
$this->user = $user; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Validation rules |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function rules() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
[['email'], 'required'], |
49
|
|
|
[['email'], 'email'], |
50
|
|
|
[['password'], 'validatePassword'] |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Validates the user's current password |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function validatePassword($attributes, $params) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (!$this->hasErrors()) { |
61
|
|
|
if (!$this->user->validatePassword($this->password_current)) { |
|
|
|
|
62
|
|
|
$this->addError('password_current', Yii::t('yrc', 'The provided password is not valid')); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Changes the user's email address |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
|
|
public function change() |
72
|
|
|
{ |
73
|
|
|
if ($this->validate()) { |
74
|
|
|
$oldEmail = $this->user->email; |
75
|
|
|
$this->user->email = $this->email; |
76
|
|
|
|
77
|
|
|
// Validation check |
78
|
|
|
if ($this->user->validate()) { |
79
|
|
|
// Save chec |
80
|
|
|
if ($this->user->save()) { |
81
|
|
|
// Notify the user via their new email that their login information has been changed |
82
|
|
|
Yii::$app->yrc->sendEmail('email_change', Yii::t('app', 'Your login information has changed'), $user->email, [ |
|
|
|
|
83
|
|
|
'oldEmail' => $oldEmail, |
84
|
|
|
'newEmail' => $this->email |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
// Send a notice to the old email notifying them their login information has been changed |
88
|
|
|
Yii::$app->yrc->sendEmail('email_change', Yii::t('app', 'Your login information has changed'), $oldEmail, [ |
89
|
|
|
'oldEmail' => $oldEmail, |
90
|
|
|
'newEmail' => $this->email |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->addError('email', $this->user->getError('email')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.