public function actionPassword($code = null)
 {
     $message = null;
     $model = null;
     $user = $code ? User::findOne(['code' => $code]) : Yii::$app->user->identity;
     if ($user) {
         $model = new Password(['scenario' => $code ? 'reset' : 'default', 'user' => $user]);
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             if ($code) {
                 $user->code = null;
                 if (!$user->auth) {
                     $user->generateAuthKey();
                 }
                 $user->setPassword($model->new_password);
                 if ($user->save()) {
                     return $this->redirect(['user/login']);
                 } else {
                     $message = 'User validation error';
                 }
             } else {
                 if ($user->validatePassword($model->password)) {
                     $user->setPassword($model->new_password);
                     if ($user->save()) {
                         return $this->redirect(['user/view', 'name' => $user->name]);
                     } else {
                         $message = 'User validation error';
                     }
                 } else {
                     $model->addError('password', Yii::t('app', 'Invalid password'));
                 }
             }
         }
     } else {
         $message = Yii::t('app', 'Invalid code');
     }
     return $this->render('password', ['model' => $model, 'message' => $message]);
 }
Example #2
0
 public function actionPassword($id)
 {
     if (!\Yii::$app->user->isGuest) {
         $modeluser = $this->findModeluser($id);
         if (Yii::$app->user->identity->id == $id) {
             $modelpassword = new Password();
             $modelpassword->username = $modeluser->username;
             if ($modelpassword->load(Yii::$app->request->post())) {
                 if ($modelpassword->validate()) {
                     $modeluser->setPassword($modelpassword->new_password);
                     if ($modeluser->save()) {
                         Yii::$app->session->setFlash('info', 'Новый парль сохранен.');
                         return $this->refresh();
                     } else {
                         Yii::$app->session->setFlash('error', 'Новый парль не сохранен.');
                         return $this->refresh();
                     }
                 }
             }
             return $this->render('password', ['model' => $modelpassword]);
         } else {
             Yii::$app->session->setFlash('error', 'У вас не прав доступа к этому разделу сайта.');
             return $this->redirect(['index']);
         }
     } else {
         return $this->redirect(['login']);
     }
 }
Example #3
0
 public function actionReset($id, $hash)
 {
     $ep = User::getEmailAndPassById($id);
     $email = $ep['email'];
     $pass = $ep['password_hash'];
     $hash1 = $email;
     for ($i = 0; $i < 13; ++$i) {
         $hash1 = sha1(md5($hash1) . $email . $pass . sha1($email));
     }
     if ($hash == $hash1) {
         $model = new Password();
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             $model1 = User::findOne($id);
             $model1->setPassword($model->password);
             $model1->save();
             Yii::$app->user->login($model1);
             // @ todo пререкласти
             \Yii::$app->session->setFlash('notify', Yii::t('yii', 'Пароль успешно изменен'));
             return $this->redirect("/id" . $id);
         }
         return $this->render('reset', ['model' => $model]);
     } else {
         return $this->redirect('/');
     }
 }
Example #4
0
 public function actionPassword($code = null, $name = null)
 {
     /** @var User $user */
     $message = null;
     $model = null;
     if (isset($_POST['name'])) {
         $name = $_POST['name'];
     }
     if ($name) {
         if (Yii::$app->user->identity->isAdmin() || $name == Yii::$app->user->identity->name) {
             $user = User::findOne(['name' => $name]);
         } else {
             throw new ForbiddenHttpException();
         }
     } else {
         $user = $code ? User::findOne(['code' => $code]) : Yii::$app->user->identity;
     }
     if ($user) {
         $model = new Password(['scenario' => $code || $name ? 'reset' : 'default', 'user' => $user]);
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             if ('reset' == $model->scenario) {
                 $user->code = null;
                 if (!$user->auth) {
                     $user->generateAuthKey();
                 }
                 $user->setPassword($model->new_password);
                 if ($user->save()) {
                     Yii::$app->session->addFlash('success', Yii::t('app', 'Password saved'));
                     return Yii::$app->user->isGuest ? $this->redirect(['user/login']) : $this->redirect(['user/view', 'name' => $user->name]);
                 } else {
                     $message = json_encode($user->errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
                 }
             } else {
                 if ($user->validatePassword($model->password)) {
                     $user->setPassword($model->new_password);
                     if ($user->save()) {
                         Yii::$app->session->addFlash('success', Yii::t('app', 'Password saved'));
                         return $this->redirect(['user/view', 'name' => $user->name]);
                     } else {
                         $message = json_encode($user->errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
                     }
                 } else {
                     $model->addError('password', Yii::t('app', 'Invalid password'));
                 }
             }
         }
     } else {
         $message = Yii::t('app', 'Invalid code');
     }
     return $this->render('password', ['model' => $model, 'message' => $message]);
 }