/** * 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('users', 'User has been created')); return $this->redirect(['index']); } return $this->render('create', ['user' => $user]); }
public function up() { $root = \Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => '*****@*****.**', 'username' => 'root', 'password' => 'rootroot']); $root->create(); $root->confirm(); $admin = \Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => '*****@*****.**', 'username' => 'admin', 'password' => 'adminadmin']); $admin->create(); $admin->confirm(); $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => '*****@*****.**', 'username' => 'user', 'password' => 'useruser']); $user->create(); $user->confirm(); }
/** * 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('users', 'User has been created') . "!\n", Console::FG_GREEN); } else { $this->stdout(\Yii::t('users', '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]); }
<?php use mii\modules\users\models\Token; use mii\modules\users\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('users')->getModel('users'); $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));
$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('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('You have successfully cancelled email changing process'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => null]); $I->amGoingTo('change username and password'); $page->update('*****@*****.**', 'nickname', 'qwerty', '123654'); $I->see('Account settings have been successfully saved'); $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 mii\modules\users\models\User; use mii\modules\users\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('users')->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('users')->getModel('users'); $page->recover($user->email); $I->see('You will receive an email with instructions on how to reset your password in a few minutes.'); $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('users')->getModel('user_with_expired_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/users/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->see('Recovery link is invalid or out-of-date. Please try requesting a new one.'); $I->amGoingTo('reset password'); $user = $I->getFixture('users')->getModel('user_with_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/users/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.');
/** @inheritdoc */ public function init() { $this->user = \Yii::createObject(['class' => User::className(), 'scenario' => 'register']); $this->module = \Yii::$app->getModule('users'); }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' endpoint. * @return mixed */ public function actionCreate() { $model = \Yii::createObject(User::className()); $model->scenario = 'create'; $model->load(\Yii::$app->request->bodyParams, ''); if ($model->save()) { \Yii::$app->response->setStatusCode(201); \Yii::$app->response->headers->set('Location', Url::toRoute(['view', 'id' => $model->id], true)); } elseif (!$model->hasErrors()) { throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); } return $model; }