Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Tests user create form
  */
 public function testCreateUser()
 {
     $user = new UserForm();
     $user->setScenario('create');
     // create user and check every error
     $this->assertFalse($user->validate(), 'Check error validation');
     $this->assertArrayHasKey('roles', $user->getErrors(), 'Check has roles error');
     $user->roles[] = 'admin';
     $this->assertFalse($user->validate());
     $this->assertArrayHasKey('name', $user->getErrors(), 'Check has name error');
     $user->name = 'Tester';
     $this->assertFalse($user->validate());
     $this->assertArrayHasKey('email', $user->getErrors(), 'Check has email error');
     $user->email = 'wrong email format';
     $this->assertArrayHasKey('email', $user->getErrors(), 'Check has wrong e-mail format');
     $user->email = '*****@*****.**';
     $user->sendNotification = true;
     $this->assertTrue($user->validate(), 'Check every field is validated');
     // create user model
     $result = $this->userModule->createUser($user);
     $this->assertNotEmpty($user->id);
     $this->assertTrue($result, 'User successfully created');
     $this->assertNotEmpty($user->password);
     // activate user
     $foundUser = $this->userModule->findUserByChecker('email_checker', $user->checker->email_checker);
     $this->assertInstanceOf(User::className(), $foundUser);
     $this->assertEquals($foundUser->id, $user->id);
     // change user password
     $changePasswordForm = new ChangePasswordForm();
     $this->assertFalse($changePasswordForm->validate(), 'Check error validation');
     $this->assertArrayHasKey('password', $changePasswordForm->getErrors(), 'Check has password error');
     $changePasswordForm->password = '******';
     $this->assertFalse($changePasswordForm->validate());
     $this->assertArrayHasKey('confirmPassword', $changePasswordForm->getErrors(), 'Check has confirmPassword error');
     $changePasswordForm->confirmPassword = '******';
     $this->assertFalse($changePasswordForm->validate());
     $this->assertArrayHasKey('confirmPassword', $changePasswordForm->getErrors(), 'Check has confirmPassword error');
     $changePasswordForm->confirmPassword = '******';
     $this->assertTrue($changePasswordForm->validate(), 'Check every field is validated');
     $result = $this->userModule->changeUserForgottenPassword($changePasswordForm, $foundUser);
     $this->assertTrue($result, 'Password successfully changed');
     $this->assertNull($foundUser->checker->email_checker);
     // user can authenticate
     $this->assertTrue($user->canSignIn());
     // create new user with exists data
     $newUser = new UserForm();
     $newUser->setAttributes($user->getAttributes());
     $this->assertFalse($newUser->validate());
     $this->assertArrayHasKey('email', $newUser->getErrors(), 'Check user already exists');
     return $foundUser;
 }