コード例 #1
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new User();
     $model->scenario = User::SCENARIO_CREATE;
     $model->status = User::STATUS_ACTIVE;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
 /**
  * @param $client \yii\authclient\BaseClient
  * @throws \yii\db\Exception
  */
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     /* @var $auth UserAuthClient */
     $auth = UserAuthClient::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // авторизация
             $user = $auth->user;
             Yii::$app->user->login($user);
         } else {
             // регистрация
             if (isset($attributes['email']) && User::find()->where(['email' => $attributes['email']])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "Пользователь с такой электронной почтой как в {client} уже существует, но с ним не связан. Для начала войдите на сайт использую электронную почту, для того, что бы связать её.", ['client' => $client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['login'], 'email' => $attributes['email'], 'password' => $password]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new UserAuthClient(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user);
                     } else {
                         print_r($auth->getErrors());
                     }
                 } else {
                     print_r($user->getErrors());
                 }
             }
         }
     } else {
         // Пользователь уже зарегистрирован
         if (!$auth) {
             // добавляем внешний сервис аутентификации
             $auth = new UserAuthClient(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }
コード例 #3
0
 public function actionSignup($modal = null)
 {
     $model = new SignupForm();
     //$model->scenario = $model::SCENARIO_WITH_CAPTCHA;
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $user = new User();
         $user->username = $model->username;
         $user->email = $model->email;
         $user->password = $model->password;
         if ($user->save() && Yii::$app->getUser()->login($user)) {
             Yii::$app->session->setFlash(Alert::TYPE_SUCCESS, Yii::t('gromver.platform', 'Registration complete.'));
             if ($modal) {
                 ModalIFrame::refreshParent();
             }
             return $this->goBack();
         }
     }
     if ($modal) {
         Yii::$app->applyModalLayout();
     } elseif ($this->module->authLayout) {
         Yii::$app->layout = $this->module->authLayout;
     }
     return $this->render('signup', ['model' => $model]);
 }