/** * 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(['class' => User::className(), 'scenario' => User::SCENARIO_REGISTER]); $this->loadAttributes($user); if (!$user->register()) { return false; } Yii::$app->session->setFlash('info', Yii::t('users', 'Your account has been created and a message with further instructions has been sent to your email')); return true; }
/** * 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 = Account::find()->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]); $event = $this->getConnectEvent($account, $user); $this->trigger(self::EVENT_BEFORE_CONNECT, $event); if ($user->load(Yii::$app->request->post()) && $user->create()) { $account->connect($user); $this->trigger(self::EVENT_AFTER_CONNECT, $event); Yii::$app->user->login($user, $this->module->rememberFor); return $this->goBack(); } return $this->render('connect', ['model' => $user, 'account' => $account]); }
/** * 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 = User::findUserByEmail($account->email); if (null !== $user) { return $user; } $user = Yii::createObject(['class' => User::className(), 'scenario' => User::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; }
/** * 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' => User::SCENARIO_CREATE]); $user->scenario = User::SCENARIO_CREATE; $event = $this->getUserEvent($user); $this->performAjaxValidation($user); $this->trigger(self::EVENT_BEFORE_CREATE, $event); if ($user->load(Yii::$app->request->post()) && $user->create()) { Yii::$app->getSession()->setFlash('success', Yii::t('users', 'User has been created')); $this->trigger(self::EVENT_AFTER_CREATE, $event); return $this->redirect(['update', 'id' => $user->id]); } return $this->render('create', ['user' => $user]); }