Esempio n. 1
0
 /**
  * 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->status = User::STATUS_ACTIVE;
         $user->generateAuthKey();
         $user->generateEmailConfirmToken();
         if ($user->save()) {
             Yii::$app->mailer->compose('@worstinme/user/mail/confirmEmail', ['user' => $user])->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name])->setTo($this->email)->setSubject('Подтверждение регистрации ' . Yii::$app->name)->send();
         }
         return $user;
     }
     return null;
 }
Esempio n. 2
0
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     $service = $client->getId();
     if (empty($attributes['email']) && $service == 'vkontakte') {
         $attributes['email'] = $attributes['id'] . '@vk.com';
     } elseif (empty($attributes['email']) && $service == 'twitter') {
         $attributes['email'] = $attributes['id'] . '@twitter.com';
     } elseif (empty($attributes['email'])) {
         $attributes['email'] = $attributes['id'] . "@{$service}.com";
     }
     /* @var $auth Auth */
     $auth = UserService::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // login
             $user = $auth->user;
             Yii::$app->user->login($user);
         } else {
             // signup
             if (User::find()->where(['email' => $attributes['email']])->exists()) {
                 Yii::$app->session->setFlash('error', Yii::t('user', "SERVICE_USER_EMAIL_EXISTS"));
             } else {
                 $user = new User(['username' => $attributes['email'], 'email' => $attributes['email'], 'status' => User::STATUS_SOCIAL]);
                 $user->setPassword(Yii::$app->security->generateRandomString(6));
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new UserService(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user);
                     } else {
                         Yii::$app->session->setFlash('error', Yii::t('user', "SERVICE_REG_FAIL") . ' ' . \yii\helpers\Json::encode($auth->getErrors()));
                     }
                 } else {
                     Yii::$app->session->setFlash('error', Yii::t('user', "SERVICE_REG_FAIL") . ' ' . \yii\helpers\Json::encode($user->getErrors()));
                 }
             }
         }
     } else {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new UserService(['user_id' => Yii::$app->user->identity->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
     if (!Yii::$app->user->isGuest) {
         $this->action->successUrl = \yii\helpers\Url::previous();
     }
 }