/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->save(); $user->afterSignup(); return $user; } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->status = Yii::$app->keyStorage->get('frontend.email-confirm') ? User::STATUS_INACTIVE : User::STATUS_ACTIVE; $user->setPassword($this->password); $user->generateAuthKey(); $user->save(); $user->afterSignup(); return $user; } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $shouldBeActivated = $this->shouldBeActivated(); $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->is_activated = !$shouldBeActivated; $user->setPassword($this->password); $user->save(); $user->afterSignup(); if ($shouldBeActivated) { $token = UserToken::create($user->id, UserToken::TYPE_ACTIVATION, Time::SECONDS_IN_A_DAY); Yii::$app->commandBus->handle(new SendEmailCommand(['subject' => Yii::t('frontend', 'Activation email'), 'view' => 'activation', 'params' => ['url' => Url::to(['/user/sign-in/activation', 'token' => $token->token], true)]])); } return $user; } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->save(); $user->afterSignup(); $auth = Yii::$app->authManager; $auth->revokeAll($user->getId()); if ($this->roles && is_array($this->roles)) { foreach ($this->roles as $role) { $auth->assign($auth->getRole($role), $user->getId()); } } return $user; } return null; }
/** * @param $client \yii\authclient\BaseClient * @return bool * @throws Exception */ public function successOAuthCallback($client) { // use BaseClient::normalizeUserAttributeMap to provide consistency for user attribute`s names $attributes = $client->getUserAttributes(); $user = User::find()->where(['oauth_client' => $client->getName(), 'oauth_client_user_id' => ArrayHelper::getValue($attributes, 'id')])->one(); if (!$user) { $user = new User(); $user->scenario = 'oauth_create'; $user->username = ArrayHelper::getValue($attributes, 'login'); $user->email = ArrayHelper::getValue($attributes, 'email'); $user->oauth_client = $client->getName(); $user->oauth_client_user_id = ArrayHelper::getValue($attributes, 'id'); $password = Yii::$app->security->generateRandomString(8); $user->setPassword($password); if ($user->save()) { $user->afterSignup(); $sentSuccess = Yii::$app->commandBus->handle(new SendEmailCommand(['view' => 'oauth_welcome', 'params' => ['user' => $user, 'password' => $password], 'subject' => Yii::t('frontend', '{app-name} | Your login information', ['app-name' => Yii::$app->name]), 'to' => $user->email])); if ($sentSuccess) { Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-success'], 'body' => Yii::t('frontend', 'Welcome to {app-name}. Email with your login information was sent to your email.', ['app-name' => Yii::$app->name])]); } } else { // We already have a user with this email. Do what you want in such case if (User::find()->where(['email' => $user->email])->count()) { Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-danger'], 'body' => Yii::t('frontend', 'We already have a user with email {email}', ['email' => $user->email])]); } else { Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-danger'], 'body' => Yii::t('frontend', 'Error while oauth process.')]); } } } if (Yii::$app->user->login($user, 3600 * 24 * 30)) { return true; } else { throw new Exception('OAuth error'); } }