Beispiel #1
0
 /**
  * Verify if the attribute is unique in database
  *
  * @param $attribute
  * @throws ServerErrorHttpException
  */
 public function validateUniqueValue($attribute)
 {
     $oldApp = Yii::$app;
     $webConfigFile = Yii::getAlias('@app/config/web.php');
     if (!file_exists($webConfigFile) || !is_array($webConfig = (require $webConfigFile))) {
         throw new ServerErrorHttpException('Cannot find `' . Yii::getAlias('@app/config/console.php') . '`. Please create and configure console config.');
     }
     Yii::$app = new Application($webConfig);
     $count = Account::find()->where([$attribute => $this->{$attribute}])->count();
     Yii::$app = $oldApp;
     if ($count > 0) {
         $this->addError($attribute, Yii::t('setup', 'This value has already been taken.'));
     }
 }
 public function register()
 {
     $pconfirm = Yii::$app->session->get('user.pconfirm');
     if ($this->validate() && $pconfirm == '') {
         $arrData = Yii::$app->request->post();
         if ($arrData['RegisterForm']['roleid'] == 2) {
             $oldApp = Yii::$app;
             $webConfigFile = Yii::getAlias('@app/config/web.php');
             if (!file_exists($webConfigFile) || !is_array($webConfig = (require $webConfigFile))) {
                 throw new ServerErrorHttpException('Cannot find `' . Yii::getAlias('@app/config/console.php') . '`. Please create and configure console config.');
             }
             Yii::$app = new Application($webConfig);
             $transaction = Account::getDb()->beginTransaction();
             try {
                 $account = new Account();
                 $account->role_id = $arrData['RegisterForm']['roleid'];
                 $account->status = 1;
                 $account->email = $arrData['RegisterForm']['email'];
                 $account->username = $arrData['RegisterForm']['username'];
                 $account->password = Yii::$app->security->generatePasswordHash('test123');
                 $account->auth_key = Yii::$app->security->generateRandomString();
                 $account->api_key = Yii::$app->security->generateRandomString();
                 $account->create_ip = Yii::$app->request->getUserIP();
                 $account->create_time = date('Y-m-d H:i:s');
                 $account->save();
                 $profile = new Profile();
                 $profile->user_id = $account->id;
                 $profile->timezone = !empty($this->timezone) ? $this->timezone : null;
                 $profile->language = $oldApp->language;
                 $profile->create_time = date('Y-m-d H:i:s');
                 $profile->save();
                 $transaction->commit();
             } catch (\Exception $e) {
                 // Rolls back the transaction
                 $transaction->rollBack();
                 return false;
             }
             Yii::$app = $oldApp;
         } else {
             Yii::$app->session->set('user.planid', $arrData['RegisterForm']['roleid']);
             Yii::$app->session->set('user.uname', $arrData['RegisterForm']['username']);
             Yii::$app->session->set('user.uemail', $arrData['RegisterForm']['email']);
         }
         return true;
     } else {
         //after payment done
         Yii::$app->session->set('user.pconfirm', '');
         $oldApp = Yii::$app;
         $webConfigFile = Yii::getAlias('@app/config/web.php');
         if (!file_exists($webConfigFile) || !is_array($webConfig = (require $webConfigFile))) {
             throw new ServerErrorHttpException('Cannot find `' . Yii::getAlias('@app/config/console.php') . '`. Please create and configure console config.');
         }
         Yii::$app = new Application($webConfig);
         $transaction = Account::getDb()->beginTransaction();
         try {
             $account = new Account();
             $account->role_id = Yii::$app->session->get('user.planid');
             $account->status = 1;
             $account->email = Yii::$app->session->get('user.uemail');
             $account->username = Yii::$app->session->get('user.uname');
             $account->password = Yii::$app->security->generatePasswordHash('test123');
             $account->auth_key = Yii::$app->security->generateRandomString();
             $account->api_key = Yii::$app->security->generateRandomString();
             $account->create_ip = Yii::$app->request->getUserIP();
             $account->create_time = date('Y-m-d H:i:s');
             $account->save();
             $profile = new Profile();
             $profile->user_id = $account->id;
             $profile->timezone = !empty($this->timezone) ? $this->timezone : null;
             $profile->language = $oldApp->language;
             $profile->create_time = date('Y-m-d H:i:s');
             $profile->save();
             $transaction->commit();
         } catch (\Exception $e) {
             // Rolls back the transaction
             $transaction->rollBack();
             return false;
         }
         Yii::$app = $oldApp;
         return true;
     }
     return false;
 }