Beispiel #1
0
 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_WAIT;
         $user->generateAuthKey();
         $user->generateEmailConfirmToken();
         if ($user->save()) {
             $auth = Yii::$app->authManager;
             $userRoleDefault = $auth->getRole('user');
             $auth->assign($userRoleDefault, $user->getId());
             $userProfile = new Profile();
             $userProfile->user_id = $user->getId();
             $userProfile->user_agent = Yii::$app->request->getUserAgent();
             $userProfile->user_ip = Yii::$app->request->getUserIP();
             $userProfile->name = $user->username;
             $userProfile->avatar_id = 1;
             //default.png (id = 1)
             $userProfile->save(false);
             Yii::$app->mailer->compose(['text' => '@app/modules/user/mails/emailConfirm'], ['user' => $user])->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])->setTo($this->email)->setSubject(Module::t('app', 'EMAIL_SIGNUP_SUBJECT') . Yii::$app->name)->send();
         }
         return $user;
     }
     return null;
 }
Beispiel #2
0
 /**
  * Signs user up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function signup()
 {
     if ($this->validate()) {
         $user = new User();
         $profile = new Profile();
         $user->username = $this->username;
         $user->email = $this->email;
         $user->setPassword($this->password);
         $user->generateAuthKey();
         $user->role = 'user';
         $user->status = 1;
         $profile->firstname = $this->firstname;
         $profile->lastname = $this->lastname;
         $profile->avatar = $this->avatar;
         // $profile->gender = 0;
         $transaction = Yii::$app->db->beginTransaction();
         if ($user->save()) {
             $profile->user_id = $user->getId();
             if ($profile->save()) {
                 $transaction->commit();
                 return $user;
             }
         } else {
             $transaction->rollback();
             print_r($user->getErrors());
             return false;
         }
     }
     return null;
 }
Beispiel #3
0
 /**
  * Creates a new Profile model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Profile();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Beispiel #4
0
 public function actionCreate()
 {
     $user = new User(['scenario' => 'admin-create']);
     $profile = new Profile();
     $statusArray = User::getStatusArray();
     $roleArray = User::getRoleArray();
     if ($user->load(Yii::$app->request->post())) {
         if ($user->validate()) {
             $transaction = Yii::$app->db->beginTransaction();
             if ($user->save(false)) {
                 $profile->user_id = $user->getId();
                 if ($profile->save(false)) {
                     $transaction->commit();
                     return $this->redirect(['index']);
                 } else {
                     $transaction->rollback();
                     print_r($user->getErrors());
                     return false;
                 }
             } else {
                 Yii::$app->session->setFlash('danger', 'Ошибка');
                 return $this->refresh();
             }
         }
     }
     // $transaction = Yii::$app->db->beginTransaction();
     //     if ($user->save())
     //     {
     //         $profile->user_id = $user->getId();
     //         if($profile->save())
     //         {
     //             $transaction->commit();
     //             return $user;
     //         }
     //     }
     //     else
     //     {
     //         $transaction->rollback();
     //         print_r($user->getErrors());
     //         return false;
     //     }
     return $this->render('create', ['model' => $user, 'statusArray' => $statusArray, 'roleArray' => $roleArray]);
 }
Beispiel #5
0
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     /* @var $auth Auth */
     $auth = Auth::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 (isset($attributes['email']) && User::find()->where(['email' => $attributes['email']])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['screen_name'], 'password' => $password, 'role' => 'user', 'email' => $attributes['email']]);
                 if (!empty($attributes['photo'])) {
                     $file = file_get_contents($attributes['photo']);
                     $avatarPath = \app\modules\user\Module::$avatarPath;
                     file_put_contents($avatarPath . '/1.jpg', $file);
                 }
                 $profile = new Profile(['firstname' => $attributes['first_name'], 'lastname' => $attributes['last_name'], 'country' => $attributes['country']]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     // Сохраняем профиль
                     $profile->user_id = $user->id;
                     if (!$profile->save()) {
                         print_r($profile->getErrors());
                         die;
                     }
                     $auth = new Auth(['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 {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }