Exemple #1
0
 public function run()
 {
     $form = new RecoveryForm();
     if (Yii::app()->request->isPostRequest && isset($_POST['RecoveryForm'])) {
         $form->setAttributes($_POST['RecoveryForm']);
         if ($form->validate()) {
             $user = $form->getUser();
             // если пароль должен быть сгенерирован автоматически
             if (Yii::app()->getModule('user')->autoRecoveryPassword) {
                 $recovery = new RecoveryPassword();
                 $recovery->setAttributes(array('user_id' => $user->id, 'code' => $recovery->generateRecoveryCode($user->id)));
                 if ($recovery->save()) {
                     // отправить письмо с сылкой на сброс пароля
                     Yii::log(Yii::t('user', 'Заявка на автоматическое восстановление пароля.'), CLogger::LEVEL_INFO, UserModule::$logCategory);
                     Yii::app()->user->setFlash(YFlashMessages::NOTICE_MESSAGE, Yii::t('user', 'На указанный email отправлено письмо с инструкцией по восстановлению пароля!'));
                     $emailBody = $this->controller->renderPartial('passwordAutoRecoveryEmail', array('model' => $recovery), true);
                     Yii::app()->mail->send(Yii::app()->getModule('user')->notifyEmailFrom, $user->email, Yii::t('user', 'Восстановление пароля!'), $emailBody);
                     $this->controller->redirect(array('/user/account/login'));
                 } else {
                     Yii::log(Yii::t('user', 'Ощибка при создании заявки на автоматическое восстановление пароля'), CLogger::LEVEL_ERROR, UserModule::$logCategory);
                     Yii::app()->user->setFlash(YFlashMessages::ERROR_MESSAGE, Yii::t('user', 'При восстановлении пароля произошла ошибка! Повторите попытку позже!!!'));
                     $this->controller->redirect(array('/user/account/recovery'));
                 }
             } else {
                 $recovery = new RecoveryPassword();
                 $recovery->setAttributes(array('user_id' => $user->id, 'code' => $recovery->generateRecoveryCode($user->id)));
                 if ($recovery->save()) {
                     Yii::log(Yii::t('user', 'Заявка на восстановление пароля.'), CLogger::LEVEL_INFO, UserModule::$logCategory);
                     Yii::app()->user->setFlash(YFlashMessages::NOTICE_MESSAGE, Yii::t('user', 'На указанный email отправлено письмо с инструкцией по восстановлению пароля!'));
                     // отправить email уведомление
                     $emailBody = $this->controller->renderPartial('passwordRecoveryEmail', array('model' => $recovery), true);
                     Yii::app()->mail->send(Yii::app()->getModule('user')->notifyEmailFrom, $user->email, Yii::t('user', 'Восстановление пароля!'), $emailBody);
                     $this->controller->redirect(array('/user/account/recovery'));
                 } else {
                     Yii::app()->user->setFlash(YFlashMessages::ERROR_MESSAGE, Yii::t('user', 'При восстановлении пароля произошла ошибка!'));
                     Yii::log($e->getMessage(), CLogger::LEVEL_ERROR, UserModule::$logCategory);
                     $this->controller->redirect(array('/user/account/recovery'));
                 }
             }
         }
     }
     $this->controller->render('recovery', array('model' => $form));
 }
 public function actionRecovery()
 {
     $form = new UserRecoveryForm();
     /*if(!Yii::app()->user->id){ #tiene que estár logeado el jefe
     			$this->redirect(Yii::app()->controller->module->returnUrl);
     		}else{*/
     $email = isset($_GET['email']) ? $_GET['email'] : '';
     $activkey = isset($_GET['activkey']) ? $_GET['activkey'] : '';
     if ($email && $activkey) {
         $form2 = new UserChangePassword();
         $find = User::model()->notsafe()->findByAttributes(array('email' => $email));
         if (isset($find) && $find->activkey == $activkey) {
             if (isset($_POST['UserChangePassword'])) {
                 $form2->attributes = $_POST['UserChangePassword'];
                 if ($form2->validate()) {
                     $find->password = Yii::app()->controller->module->encrypting($form2->password);
                     $find->activkey = Yii::app()->controller->module->encrypting(microtime() . $form2->password);
                     if ($find->status == 0) {
                         $find->status = 1;
                     }
                     $find->save();
                     Yii::app()->user->setFlash('success', UserModule::t("New password is saved."));
                     $registro = RecoveryPassword::model()->findByAttributes(array('email' => $email));
                     $registro->delete();
                     #$this->redirect(Yii::app()->controller->module->recoveryUrl);
                     $this->redirect(array('/RecoveryPassword/index'));
                 }
             }
             $this->render('changepassword', array('model' => $form2));
         } else {
             Yii::app()->user->setFlash('error', UserModule::t("Incorrect recovery link."));
             $this->redirect(Yii::app()->controller->module->recoveryUrl);
         }
     } else {
         if (isset($_POST['UserRecoveryForm'])) {
             $form->attributes = $_POST['UserRecoveryForm'];
             if ($form->validate()) {
                 $user = User::model()->notsafe()->findbyPk($form->user_id);
                 $activation_url = 'http://' . $_SERVER['HTTP_HOST'] . $this->createUrl(implode(Yii::app()->controller->module->recoveryUrl), array("activkey" => $user->activkey, "email" => $user->email));
                 $activation_url = 'http://' . $_SERVER['HTTP_HOST'] . $this->createUrl('/user/recovery/recovery', array("activkey" => $user->activkey, "email" => $user->email));
                 ##
                 $recoveryPass = new RecoveryPassword();
                 $recoveryPass->idUser = $user->id;
                 $recoveryPass->email = $user->email;
                 $recoveryPass->link = $activation_url;
                 $recoveryPass->save();
                 $this->redirect(array('Recovery/hecho'));
                 /*$subject = UserModule::t("You have requested a password recovery of {site_name}",
                  					array(
                  						'{site_name}'=>Yii::app()->name,
                  					));
                  			$message = UserModule::t("You have requested our password recovery services. To receive a new password, go to {activation_url}. Sincerely, Team of {site_name}",
                  					array(
                  						'{site_name}'=>Yii::app()->name,
                  						'{activation_url}'=>$activation_url,
                  					));
                 	
                  			UserModule::sendMail($user->email,$subject,$message);*/
                 ##
                 #Yii::app()->user->setFlash('info',UserModule::t("Please check your email. Further instructions are sent to your email address."));
                 #$this->refresh();
             }
         }
         $this->render('recovery', array('model' => $form));
     }
     #}
 }