public function actionChangeEmail() { if (Yii::app()->user->isGuest) { Yii::app()->request->redirect(basePath('')); } //do validation $model = new ChangeEmailForm(); $model->setAttributes(array('username' => $_POST['username'], 'username_confirm' => $_POST['username_confirm'], 'password' => $_POST['password'])); $model->validate(); $errors = $model->getErrors(); if (count($errors) != 0) { Yii::app()->user->setFlash('changeemailerror', true); foreach ($errors as $key => $value) { Yii::app()->user->setFlash($key, $value); } Yii::app()->request->redirect(basePath('app/myprofile')); return; } $user = User::model()->findByPk(Yii::app()->user->getState('id')); if ($user->password != $_POST['password']) { Yii::app()->user->setFlash('password', array('0' => 'Invalid password')); Yii::app()->request->redirect(basePath('app/myprofile')); return; } //update user's email $user->email = $model->username; $user->update(); //update session state $userControl = new userControl(); $userControl->update(); //redirect user to my profile page Yii::app()->request->redirect(basePath('app/myprofile')); }
/** * Action ChangeEmail dùng để đổi email của user */ public function actionChangeEmail() { // Bước 1: Người dùng yêu cầu change email if (empty($_GET['code'])) { // Submit form if (isset($_POST['changepassword'])) { $user = User::model()->notsafe()->findByPk(Yii::app()->user->id); $user->activkey = $user->createCodeActivation(); $user->save(); // Create Code change email $codeChangeEmail = $user->activkey; if ($codeChangeEmail != '') { /** * @todo Change message email */ $strChangeEmailUrl = $this->createAbsoluteUrl('/user/profile/changeemail', array("code" => $codeChangeEmail)); $strMsgHTML = "<a href='{$strChangeEmailUrl}'>{$strChangeEmailUrl}</a>"; Yii::import('application.extensions.phpmailer.HMailHelper'); HMailHelper::Send('Change Email', $strMsgHTML, array(array($user->email, $user->username))); // Notice Sent mail validate for change password $this->setRedirectOptions(array("timeout" => 5, "title" => UserModule::t('Change Email - Step 1'), "message" => UserModule::t('We have sent you a confirmation email to the email change. Please check and verify it!'))); $this->redirect('/user/profile'); } } // Render form requirement change email $this->render('changeemailstep1'); } else { // Bước 2: Đổi email // Validate code change email $strChangeEmailCode = $_GET['code']; $user = User::model()->notsafe()->findByPk(Yii::app()->user->id); // Check Code if ($strChangeEmailCode == $user->activkey) { $model = new ChangeEmailForm(); // Submit form if (isset($_POST['ChangeEmailForm'])) { $model->attributes = $_POST['ChangeEmailForm']; $user = User::model()->notsafe()->findByPk(Yii::app()->user->id); //$model->user = $user; // Validate info if ($model->validate()) { // Change email $user->email = $model->email; // Delete old Code change email $user->activkey = $user->createCodeActivation(); // Save new email to DB if ($user->save()) { /** * @todo Change message email */ $strMsgHTML = "Change Email Success"; Yii::import('application.extensions.phpmailer.HMailHelper'); HMailHelper::Send('Change Email', $strMsgHTML, array(array($user->email, $user->username))); // Notice Change Email success $this->setRedirectOptions(array("title" => UserModule::t('Change Email Success'), "message" => UserModule::t('The change email was successful!'))); $this->redirect('/user/profile'); } } } // Render form change email $this->render('changeemailstep2', array('model' => $model)); Yii::app()->end(); } // Notice activation failure $this->setRedirectOptions(array("title" => UserModule::t('Change Email Failure'), "message" => UserModule::t('Incorrect change email URL!'))); $this->redirect("/"); } }