public function testUserEmailChange() { $newEmail = '*****@*****.**'; $model = $this->user; $profileForm = new ProfileForm(); $this->assertTrue($model !== NULL); $profileForm->load($model->id, true); $profileForm->email = $newEmail; // Verify that the profile form saves $this->assertTrue($profileForm->save()); // Verify that the base user model didn't change $model = $this->getUserModel(); $this->assertTrue($model->email == '*****@*****.**'); $newEmailModel = UserMetadata::model()->findByAttributes(array('user_id' => $this->user->id, 'key' => 'newEmailAddress')); // Verify that the new email is stored in the database $this->assertTrue($newEmailModel !== NULL); $this->assertTrue($newEmailModel->value == $newEmail); $key = UserMetadata::model()->findByAttributes(array('user_id' => $this->user->id, 'key' => 'newEmailAddressChangeKey')); $this->assertTrue($key !== NULL); $emailChangeForm = new EmailChangeForm(); $emailChangeForm->setUser($this->getUserModel()); $emailChangeForm->verificationKey = $key->value; $emailChangeForm->password = '******'; // Verify that the verification key works $this->assertTrue($emailChangeForm->validateVerificationKey()); // Veirfy that the email address changes $this->assertTrue($emailChangeForm->validate()); $this->assertTrue($emailChangeForm->save()); // Verify that the email has changed for the model now $model = Users::model()->findByAttributes(array('email' => '*****@*****.**')); $this->assertTrue($model->email == $newEmail); }
/** * Allows the user to securely change their email address * @param string $key the user's secure key */ public function actionEmailChange($key = null) { $this->layout = '//layouts/main'; $this->setPageTitle(Yii::t('ciims.controllers.Site', '{{app_name}} | {{label}}', array('{{app_name}}' => Cii::getConfig('name', Yii::app()->name), '{{label}}' => Yii::t('ciims.controllers.Site', 'Change Your Email Address')))); $model = new EmailChangeForm(); $model->setUser(Users::model()->findByPk(Yii::app()->user->id)); $model->verificationKey = $key; if (!$model->validateVerificationKey()) { throw new CHttpException(403, Yii::t('ciims.controllers.Site', 'The verification key provided is invalid.')); } if (Cii::get($_POST, 'EmailChangeForm', false)) { $model->attributes = $_POST['EmailChangeForm']; if ($model->save()) { Yii::app()->user->setFlash('success', Yii::t('ciims.controllers.Site', 'Your new email address has been verified.')); $loginForm = new LoginForm(); $loginForm->attributes = array('username' => Users::model()->findByPk(Yii::app()->user->id)->email, 'password' => $model->password); if ($loginForm->login()) { return $this->redirect(Yii::app()->homeUrl); } throw new CHttpException(400, Yii::t('ciims.controllers.Site', 'Unable to re-authenticated user.')); } } $this->render('emailchange', array('model' => $model)); }