1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class EmailChangeForm extends CFormModel |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* The user's existing password |
7
|
|
|
* @var string $password |
8
|
|
|
*/ |
9
|
|
|
public $password; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The veification key |
13
|
|
|
* @var string $verificationKey |
14
|
|
|
*/ |
15
|
|
|
public $verificationKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The user model |
19
|
|
|
* @var Users $_user |
20
|
|
|
*/ |
21
|
|
|
private $_user; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The new email address change key |
25
|
|
|
* @var UserMetadata $_newEmailAddressChangeKey |
26
|
|
|
*/ |
27
|
|
|
private $_newEmailAddressChangeKey; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The new email address record on file |
31
|
|
|
* @var UserMetadata $_newEmailAddress |
32
|
|
|
*/ |
33
|
|
|
private $_newEmailAddress; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validation rules |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function rules() |
40
|
|
|
{ |
41
|
|
|
return array( |
42
|
|
|
array('password, verificationKey', 'required'), |
43
|
|
|
array('password', 'length', 'min' => 8), |
44
|
|
|
array('password', 'validateUserPassword'), |
45
|
|
|
array('verificationKey', 'validateVerificationKey') |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Attribute labels |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function attributeLabels() |
54
|
|
|
{ |
55
|
|
|
return array( |
56
|
|
|
'password' => Yii::t('ciims.models.EmailChangeForm', 'Your Password'), |
57
|
|
|
'verificationKey' => Yii::t('ciims.models.EmailChangeForm', 'The Verification Key') |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets the user model |
63
|
|
|
* @param Users $user |
64
|
|
|
*/ |
65
|
|
|
public function setUser($user) |
66
|
|
|
{ |
67
|
|
|
$this->_user = $user; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Validates the tokens supplied and that the request hasn't expired |
72
|
|
|
* @param array $attributes |
73
|
|
|
* @param array $params |
74
|
|
|
* return array |
75
|
|
|
*/ |
76
|
|
|
public function validateVerificationKey($attributes = array(), $params = array()) |
77
|
|
|
{ |
78
|
|
|
$this->_newEmailAddressChangeKey = UserMetadata::model()->findByAttributes(array( |
79
|
|
|
'user_id' => $this->_user->id, |
80
|
|
|
'key' => 'newEmailAddressChangeKey', |
81
|
|
|
'value' => $this->verificationKey |
82
|
|
|
)); |
83
|
|
|
|
84
|
|
|
if ($this->_newEmailAddressChangeKey == NULL) |
85
|
|
|
{ |
86
|
|
|
$this->addError('verificationKey', Yii::t('ciims.models.EmailChangeForm', 'The activation key you provided is invalid')); |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->_newEmailAddress = UserMetadata::model()->findByAttributes(array( |
91
|
|
|
'user_id' => $this->_user->id, |
92
|
|
|
'key' => 'newEmailAddress' |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
if ($this->_newEmailAddress == NULL) |
96
|
|
|
{ |
97
|
|
|
$this->addError('verificationKey', Yii::t('ciims.models.EmailChangeForm', 'The activation key you provided is invalid')); |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Ensures that the password entered matches the one provided during registration |
106
|
|
|
* @param array $attributes |
107
|
|
|
* @param array $params |
108
|
|
|
* return array |
109
|
|
|
*/ |
110
|
|
|
public function validateUserPassword($attributes, $params) |
111
|
|
|
{ |
112
|
|
|
$result = password_verify($this->password, $this->_user->password); |
113
|
|
|
|
114
|
|
|
if ($result == false) |
115
|
|
|
{ |
116
|
|
|
$this->addError('password', Yii::t('ciims.models.ActivationForm', 'The password you entered does not match the password you registered with.')); |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Updates the user's email address and rehashes their password since the password is bound to the email |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
|
public function save() |
128
|
|
|
{ |
129
|
|
|
if (!$this->validate()) |
130
|
|
|
return false; |
131
|
|
|
|
132
|
|
|
// This is super buggy for some reason |
133
|
|
|
$this->_user->email = $this->_newEmailAddress->value; |
134
|
|
|
|
135
|
|
|
// Save the model |
136
|
|
|
if ($this->_user->save()) |
137
|
|
|
{ |
138
|
|
|
// Delete the metadata |
139
|
|
|
$this->_newEmailAddressChangeKey->delete(); |
140
|
|
|
$this->_newEmailAddress->delete(); |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Retrieves the user's email address from the model |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getEmail() |
152
|
|
|
{ |
153
|
|
|
return $this->_user->email; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.