Blame view

thread/modules/user/models/form/RegisterForm.php 2.9 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  <?php
  
  namespace thread\modules\user\models\form;
  
  use Yii;
  use yii\log\Logger;
  //
  use thread\modules\user\models\{
      Group, Profile, User
  };
  use yii\helpers\ArrayHelper;
  
  /**
   * Class RegisterForm
   *
   * @package thread\modules\user\models
   * @author FilamentV <vortex.filament@gmail.com>
   * @copyright (c), Thread
   */
  class RegisterForm extends CommonForm
  {
  
      /**
       * @return array
       */
      public function rules()
      {
          $rules = [[['password', 'password_confirmation'], 'required']];
  
          if ($this->_username_attribute === 'email') {
              $rules[] = [['email'], 'required'];
          } elseif ($this->_username_attribute === 'username') {
              $rules[] = [['username'], 'required'];
          }
  
          return ArrayHelper::merge($rules, parent::rules());
      }
  
      /**
       * @return array
       */
      public function scenarios()
      {
          return [
              'register' => ['username', 'email', 'password', 'password_confirmation'],
          ];
      }
  
      /**
       * Add new user to base
       */
      public function addUser()
      {
          $model = new User([
              'scenario' => 'userCreate',
              'username' => $this->username,
              'email' => $this->email,
              'published' => User::STATUS_KEY_ON,
              'group_id' => Group::USER,
          ]);
          $model->setPassword($this->password)->generateAuthKey();
          if ($model->validate()) {
              /** @var $transaction */
              $transaction = self::getDb()->beginTransaction();
              try {
                  $save = $model->save();
                  if ($save) {
                      $transaction->commit();
                      return $this->addProfile($model->id);
                  } else {
                      $transaction->rollBack();
                      return false;
                  }
              } catch (\Exception $e) {
                  Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR);
                  $transaction->rollBack();
              }
          } else {
              $this->addErrors($model->getErrors());
              return false;
          }
      }
  
      /**
       * Create new empty profile for a new user
       *
       * @param $userId
       * @return bool
       * @throws \Exception
       */
      private function addProfile($userId)
      {
          $model = new Profile([
              'scenario' => 'basicCreate',
              'user_id' => $userId,
          ]);
          if ($model->validate()) {
              /** @var PDO $transaction */
              $transaction = self::getDb()->beginTransaction();
              try {
                  $save = $model->save();
                  ($save) ? $transaction->commit() : $transaction->rollBack();
                  return $save;
              } catch (\Exception $e) {
                  $transaction->rollBack();
                  throw new \Exception($e);
              }
          } else {
              $this->addErrors($model->getErrors());
              return false;
          }
      }
  }