/**
  * 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(['update', 'id' => $user->id]);
     }
     return $this->render('create', ['user' => $user]);
 }
 /**
  * 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 string $code
  *
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionConnect($code)
 {
     $account = $this->finder->findAccount()->byCode($code)->one();
     if ($account === null || $account->getIsConnected()) {
         throw new NotFoundHttpException();
     }
     /** @var User $user */
     $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]);
     if ($user->load(Yii::$app->request->post()) && $user->create()) {
         $account->connect($user);
         Yii::$app->user->login($user, $this->module->rememberFor);
         return $this->goBack();
     }
     return $this->render('connect', ['model' => $user, 'account' => $account]);
 }
 /**
  * Registers a new user account. If registration was successful it will set flash message.
  *
  * @return bool
  */
 public function register()
 {
     if (!$this->validate()) {
         return false;
     }
     /** @var User $user */
     $user = Yii::createObject(User::className());
     $user->setScenario('register');
     $this->loadAttributes($user);
     if (!$user->register()) {
         return false;
     }
     Yii::$app->session->setFlash('info', Yii::t('user', 'Your account has been created and a message with further instructions has been sent to your email'));
     return true;
 }
示例#5
0
 /**
  * Tries to find user or create a new one.
  *
  * @param Account $account
  *
  * @return User|bool False when can't create user.
  */
 protected static function fetchUser(Account $account)
 {
     $user = static::getFinder()->findUserByEmail($account->email);
     if (null !== $user) {
         return $user;
     }
     $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]);
     if (!$user->validate(['email'])) {
         $account->email = null;
     }
     if (!$user->validate(['username'])) {
         $account->username = null;
     }
     return $user->create() ? $user : false;
 }