Пример #1
0
    /**
     * @return void
     */
    public function actionPassword()
    {
        $model = new PasswordForm();

        if (isset($_POST['PasswordForm'])) {
            $model->attributes = $_POST['PasswordForm'];

            if ($model->validate()) {
                // get user from db
                $user = User::model()->findByPk(Yii::app()->user->id);

                // set new password
                $user->password = Yii::app()->securityManager->padUserPassword($model->newPassword);

                // encrypt encryptionKey with new password
                $securityManager = Yii::app()->securityManager;
                $user->encryptionKey = $securityManager->encrypt(Yii::app()->user->encryptionKey, $user->password);

                // salt password
                $user->saltPassword(new CEvent());

                // save user record
                $user->save(false);

                // set success-flash & refresh page
                Yii::app()->user->setFlash('success', 'Your password was changed successfully.');
                $this->refresh();
            }
        }

        $this->render('password', array('model' => $model));
    }
Пример #2
0
 /**
  * @dataProvider invalidDataProvider
  */
 public function testInvalid($scenario, $attributes, $errors)
 {
     $form = new PasswordForm($scenario);
     $form->userIdentityClass = 'UserIdentity';
     $form->setIdentity(new UserIdentity('neo', 'Test1233'));
     $form->setAttributes($attributes);
     $this->assertFalse($form->validate());
     $this->assertEquals($errors, $form->getErrors());
 }
Пример #3
0
 public function actionPassword()
 {
     $model = new PasswordForm();
     if (isset($_POST['PasswordForm'])) {
         $model->attributes = $_POST['PasswordForm'];
         if ($model->validate()) {
             $password = UserIdentity::createPassword($model->password, $model->cost);
             return $this->render('password_generated', array('password' => $password));
         }
     }
     $model->cost = 8;
     $this->render('password', array('model' => $model));
 }
Пример #4
0
 /**
  * Displays the password reset page
  */
 public function actionPassword()
 {
     if (Pii::guest()) {
         $this->_redirectError('You must be logged in to change your password.');
     }
     $_model = new PasswordForm();
     // collect user input data
     if (isset($_POST, $_POST['PasswordForm'])) {
         $_model->attributes = $_POST['PasswordForm'];
         //	Validate user input and redirect to the previous page if valid
         if ($_model->validate()) {
             try {
                 $_userId = Session::getCurrentUserId();
                 $_result = Password::changePassword($_userId, $_model->old_password, $_model->new_password);
                 if (Option::getBool($_result, 'success')) {
                     Yii::app()->user->setFlash('password-form', 'Your password has been successfully updated.');
                 }
             } catch (\Exception $_ex) {
                 $_model->addError(null, $_ex->getMessage());
             }
         }
     }
     $this->render('password', array('model' => $_model, 'backUrl' => $this->_getRedirectUrl()));
 }
Пример #5
0
 public function actionProfile($update = false)
 {
     if (Yii::app()->user->isGuest) {
         $this->redirect(array('login'));
     }
     $model = new ProfileForm();
     $model->setAttributes($model->getIdentity()->getAttributes());
     $passwordForm = new PasswordForm();
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'profile-form') {
         $models = array($model);
         if (isset($_POST['PasswordForm']) && trim($_POST['PasswordForm']['newPassword']) !== '') {
             $models[] = $passwordForm;
         }
         echo CActiveForm::validate($models);
         Yii::app()->end();
     }
     $flashes = array('success' => array(), 'error' => array());
     if (isset($_POST['PasswordForm']) && trim($_POST['PasswordForm']['newPassword']) !== '') {
         $passwordForm->setAttributes($_POST['PasswordForm']);
         if ($passwordForm->validate()) {
             if ($passwordForm->resetPassword($model->getIdentity())) {
                 $flashes['success'][] = Yii::t('UsrModule.usr', 'Changes have been saved successfully.');
             } else {
                 $flashes['error'][] = Yii::t('UsrModule.usr', 'Failed to change password.');
             }
         }
     }
     if (isset($_POST['ProfileForm']) && empty($flashes['error'])) {
         $model->setAttributes($_POST['ProfileForm']);
         if ($model->validate()) {
             $oldEmail = $model->getIdentity()->getEmail();
             if ($model->save()) {
                 if ($this->module->requireVerifiedEmail && $oldEmail != $model->email) {
                     if ($this->sendEmail($model, 'verify')) {
                         $flashes['success'][] = Yii::t('UsrModule.usr', 'An email containing further instructions has been sent to provided email address.');
                     } else {
                         $flashes['error'][] = Yii::t('UsrModule.usr', 'Failed to send an email.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.');
                     }
                 }
                 $flashes['success'][] = Yii::t('UsrModule.usr', 'Changes have been saved successfully.');
                 if (!empty($flashes['success'])) {
                     Yii::app()->user->setFlash('success', implode('<br/>', $flashes['success']));
                 }
                 if (!empty($flashes['error'])) {
                     Yii::app()->user->setFlash('error', implode('<br/>', $flashes['error']));
                 }
                 $this->redirect(array('profile'));
             } else {
                 $flashes['error'][] = Yii::t('UsrModule.usr', 'Failed to update profile.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.');
             }
         }
     }
     if (!empty($flashes['success'])) {
         Yii::app()->user->setFlash('success', implode('<br/>', $flashes['success']));
     }
     if (!empty($flashes['error'])) {
         Yii::app()->user->setFlash('error', implode('<br/>', $flashes['error']));
     }
     if ($update) {
         $this->render('updateProfile', array('model' => $model, 'passwordForm' => $passwordForm));
     } else {
         $this->render('viewProfile', array('model' => $model));
     }
 }