/** * Change profile wrapper * * @param UserForm $model * @return mixed */ protected function changeProfile(UserForm $model) { /* @var $systemAlert Alert */ $systemAlert = Yii::$app->systemAlert; if ($this->userModule->updateUser($model)) { $systemAlert->setMessage(Alert::INFO, Yii::t('user', 'Profile successfully changed')); } else { $systemAlert->setMessage(Alert::DANGER, Yii::t('user', 'Change profile error')); } return $this->refresh(); }
/** * Update exits user model. * * @param integer $id * @return mixed * @throws NotFoundHttpException */ public function actionUpdate($id) { $model = $this->findModel($id); $model->setScenario('update'); if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; $model->load(Yii::$app->request->post()); return ActiveForm::validate($model); } /* @var $systemAlert Alert */ $systemAlert = Yii::$app->systemAlert; if ($model->load(Yii::$app->request->post()) && $model->validate()) { try { if ($this->userModule->updateUser($model)) { $systemAlert->setMessage(Alert::SUCCESS, Yii::t('user', 'User successfully updated')); return $this->redirect(['index']); } else { $systemAlert->setMessage(Alert::DANGER, Yii::t('user', 'User update error')); } } catch (Exception $ex) { $systemAlert->setMessage(Alert::DANGER, Yii::t('app', 'System error: {message}', ['message' => $ex->getMessage()])); } } return $this->render('update', ['model' => $model]); }
/** * Tests user update form * * @depends testCreateUser */ public function testUpdateUser() { /* @var $user UserForm */ $user = UserForm::findOne($this->getModule('Yii2')->grabFixture('users', 'activeUser1')->id); $this->assertInstanceOf(UserForm::className(), $user); $user->setScenario('update'); $oldPassword = $user->password; // remove role $user->roles = []; $this->assertFalse($user->validate()); $this->assertArrayHasKey('roles', $user->getErrors(), 'Check empty roles'); $user->roles[] = 'admin'; // generate new password $user->generateRandomPassword = true; $user->sendNotification = true; $result = $this->userModule->updateUser($user); $this->assertTrue($result); $this->assertNotEquals($oldPassword, $user->password); // user can authenticate $this->assertTrue($user->canSignIn()); return $user; }