/** * Registers a new user account. * @return bool */ public function register() { if (!$this->validate()) { return false; } $this->user->setAttributes(['email' => $this->email, 'username' => $this->username, 'password' => $this->password]); return $this->user->register(); }
/** @inheritdoc */ public function rules() { return [['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'exist', 'targetClass' => $this->module->modelMap['User'], 'message' => \Yii::t('user', 'There is no user with this email address')], ['email', function ($attribute) { $this->user = $this->finder->findUserByEmail($this->email); if ($this->user !== null && $this->module->enableConfirmation && !$this->user->getIsConfirmed()) { $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address')); } }], ['password', 'required'], ['password', 'string', 'min' => 6]]; }
/** @inheritdoc */ public function rules() { return [[['login', 'password'], 'required'], ['login', 'trim'], ['password', function ($attribute) { if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) { $this->addError($attribute, \Yii::t('user', 'Invalid login or password')); } }], ['login', function ($attribute) { if ($this->user !== null) { $confirmationRequired = $this->module->enableConfirmation && !$this->module->enableUnconfirmedLogin; if ($confirmationRequired && !$this->user->getIsConfirmed()) { $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address')); } if ($this->user->getIsBlocked()) { $this->addError($attribute, \Yii::t('user', 'Your account has been blocked')); } } }], ['rememberMe', 'boolean']]; }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'index' page. * @return mixed */ public function actionCreate() { /** @var User $user */ $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'create']); $this->performAjaxValidation($user); if ($user->load(\Yii::$app->request->post()) && $user->create()) { \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been created')); return $this->redirect(['index']); } return $this->render('create', ['user' => $user]); }
public function testRegister() { $this->specify('user should be registered', function () { $user = new User(['scenario' => 'register']); $user->username = '******'; $user->email = '*****@*****.**'; $user->password = '******'; verify($user->register())->true(); verify($user->username)->equals('tester'); verify($user->email)->equals('*****@*****.**'); verify(Yii::$app->getSecurity()->validatePassword('tester', $user->password_hash))->true(); }); $this->specify('profile should be created after registration', function () { $user = new User(['scenario' => 'register']); $user->username = '******'; $user->email = '*****@*****.**'; $user->password = '******'; verify($user->register())->true(); verify($user->profile->gravatar_email)->equals('*****@*****.**'); }); }
/** * This command creates new user account. If password is not set, this command will generate new 8-char password. * After saving user to database, this command uses mailer component to send credentials (username and password) to * user via email. * * @param string $email Email address * @param string $username Username * @param null|string $password Password (if null it will be generated automatically) */ public function actionIndex($email, $username, $password = null) { $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => $email, 'username' => $username, 'password' => $password]); if ($user->create()) { $this->stdout(\Yii::t('user', 'User has been created') . "!\n", Console::FG_GREEN); } else { $this->stdout(\Yii::t('user', 'Please fix following errors:') . "\n", Console::FG_RED); foreach ($user->errors as $errors) { foreach ($errors as $error) { $this->stdout(" - " . $error . "\n", Console::FG_RED); } } } }
/** * Displays page where user can create new account that will be connected to social account. * @param integer $account_id * @return string * @throws NotFoundHttpException */ public function actionConnect($account_id) { $account = $this->finder->findAccountById($account_id); if ($account === null || $account->getIsConnected()) { throw new NotFoundHttpException(); } /** @var User $user */ $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'connect']); if ($user->load(\Yii::$app->request->post()) && $user->create()) { $account->user_id = $user->id; $account->save(false); \Yii::$app->user->login($user, $this->module->rememberFor); return $this->goBack(); } return $this->render('connect', ['model' => $user, 'account' => $account]); }
public function testRegister() { $this->model = new RegistrationForm(); $this->model->setAttributes(['email' => '*****@*****.**', 'username' => 'foobar', 'password' => 'foobar']); /** @var User $user */ verify($this->model->register())->true(); $user = User::findOne(['email' => '*****@*****.**']); verify('$user is instance of User', $user instanceof User)->true(); verify('email is valid', $user->email)->equals($this->model->email); verify('username is valid', $user->username)->equals($this->model->username); verify('password is valid', Password::validate($this->model->password, $user->password_hash))->true(); $token = Token::findOne(['user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); verify($token)->notNull(); $mock = $this->getMock(RegistrationForm::className(), ['validate']); $mock->expects($this->once())->method('validate')->will($this->returnValue(false)); verify($mock->register())->false(); }
$user = $I->grabRecord(User::className(), ['id' => $user->id]); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_CONFIRM_NEW_EMAIL]); $I->seeInEmail(Html::encode($token->getUrl())); $I->seeInEmailRecipients($user->unconfirmed_email); Yii::$app->user->logout(); $I->amGoingTo('log in using new email address before clicking the confirmation link'); $page = LoginPage::openBy($I); $page->login('*****@*****.**', 'qwerty'); $I->see('Invalid login or password'); $I->amGoingTo('log in using new email address after clicking the confirmation link'); $user->attemptEmailChange($token->code); $page->login('*****@*****.**', 'qwerty'); $I->see('Logout'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => null]); $I->amGoingTo('reset email changing process'); $page = SettingsPage::openBy($I); $page->update('*****@*****.**', $user->username, 'qwerty'); $I->see('A confirmation message has been sent to your new email address'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => '*****@*****.**']); $page->update('*****@*****.**', $user->username, 'qwerty'); $I->see('Your account details have been updated'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => null]); $I->amGoingTo('change username and password'); $page->update('*****@*****.**', 'nickname', 'qwerty', '123654'); $I->see('Your account details have been updated'); $I->seeRecord(User::className(), ['username' => 'nickname', 'email' => '*****@*****.**']); Yii::$app->user->logout(); $I->amGoingTo('login with new credentials'); $page = LoginPage::openBy($I); $page->login('nickname', '123654'); $I->see('Logout');
use yii\helpers\Html; use yii\helpers\Url; use chd7well\user\models\User; use chd7well\user\models\Token; $I = new FunctionalTester($scenario); $I->wantTo('ensure that password recovery works'); $page = RecoveryPage::openBy($I); $I->amGoingTo('try to request recovery token for unconfirmed account'); $user = $I->getFixture('user')->getModel('unconfirmed'); $page->recover($user->email); $I->see('You need to confirm your email address'); $I->amGoingTo('try to request recovery token'); $user = $I->getFixture('user')->getModel('user'); $page->recover($user->email); $I->see('An email has been sent with instructions for resetting your password'); $user = $I->grabRecord(User::className(), ['email' => $user->email]); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->seeInEmail(Html::encode($token->getUrl())); $I->seeInEmailRecipients($user->email); $I->amGoingTo('reset password with invalid token'); $user = $I->getFixture('user')->getModel('user_with_expired_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->see('Recovery link is invalid or expired. Please try requesting a new one.'); $I->amGoingTo('reset password'); $user = $I->getFixture('user')->getModel('user_with_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->fillField('#recovery-form-password', 'newpass'); $I->click('Finish'); $I->see('Your password has been changed successfully.');
<?php use chd7well\user\models\Token; use chd7well\user\models\User; use tests\codeception\_pages\RegistrationPage; use yii\helpers\Html; $I = new FunctionalTester($scenario); $I->wantTo('ensure that registration works'); $page = RegistrationPage::openBy($I); $I->amGoingTo('try to register with empty credentials'); $page->register('', '', ''); $I->see('Username cannot be blank'); $I->see('Email cannot be blank'); $I->see('Password cannot be blank'); $I->amGoingTo('try to register with already used email and username'); $user = $I->getFixture('user')->getModel('user'); $page->register($user->username, $user->email, 'qwerty'); $I->see(Html::encode('This username has already been taken')); $I->see(Html::encode('This email address has already been taken')); $I->amGoingTo('try to register'); $page->register('tester', '*****@*****.**', 'tester'); $I->see('A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'); $user = $I->grabRecord(User::className(), ['email' => '*****@*****.**']); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); $I->seeInEmail(Html::encode($token->url));