/** * Create new user and send notification * * @param UserForm $user * @return boolean true if success * @throws Exception */ public function createUser(UserForm $user) { if (!$user->isNewRecord || !$user->validate()) { // only new validated users record return false; } $transaction = $user->getDb()->beginTransaction(); try { // generate new random password $user->newPassword = Yii::$app->security->generateRandomString(32); $user->status = User::STATUS_ACTIVE; $user->save(); $this->updateUserRoles($user, $user->roles); if ($user->sendNotification) { // send user's notification $changePasswordLink = Url::toRoute(['/user/auth/change-password', 'hash' => $this->getUserChecker($user, 'email_checker')], true); Yii::$app->mailer->compose('userNewNotification', ['user' => $user, 'link' => $changePasswordLink])->setTo($user->email)->setSubject(Yii::t('user', 'Account created'))->send(); } $transaction->commit(); return true; } catch (Exception $ex) { $transaction->rollBack(); throw $ex; } return false; }
/** * Find user model by id and generate 404 if model is not found. * * @param integer $id User id * * @return UserForm * * @throws NotFoundHttpException */ protected function findModel($id) { $id = is_scalar($id) ? (int) $id : 0; $model = UserForm::find()->andWhere(['id' => $id])->one(); if (!$model instanceof UserForm) { throw new NotFoundHttpException(); } return $model; }
/** * Profile index * * @return mixed */ public function actionIndex() { // profile $profileForm = UserForm::findOne(Yii::$app->user->getId()); $profileForm->setScenario('profile'); $ret = $this->performAjaxValidation($profileForm); if (is_array($ret)) { // AJAX validation return $ret; } $dataLoaded = $profileForm->load(Yii::$app->request->post()); $profileForm->uploadedAvatar = UploadedFile::getInstance($profileForm, 'uploadedAvatar'); if ($dataLoaded && $profileForm->validate()) { // change user's profile return $this->changeProfile($profileForm); } // change password $changePasswordForm = new ChangePasswordForm(); $ret = $this->performAjaxValidation($changePasswordForm); if (is_array($ret)) { // AJAX validation return $ret; } if ($changePasswordForm->load(Yii::$app->request->post()) && $changePasswordForm->validate()) { // change user's password return $this->changePassword($changePasswordForm); } return $this->render('index', ['profileForm' => $profileForm, 'changePasswordForm' => $changePasswordForm]); }
/** * 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; }