Blame view

backend/models/User.php 5.83 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php
36d1807a   Yarik   Big commit.
2
3
4
5
      
      namespace backend\models;
      
      use developeruz\db_rbac\interfaces\UserRbacInterface;
36d1807a   Yarik   Big commit.
6
7
8
9
10
11
12
13
      use common\modules\comment\models\CommentModel;
      use common\modules\comment\models\RatingModel;
      use yii\base\NotSupportedException;
      use Yii;
      use yii\behaviors\TimestampBehavior;
      use yii\db\ActiveRecord;
      use yii\web\IdentityInterface;
      
d8c1a2e0   Yarik   Big commit artbox
14
      /**
36d1807a   Yarik   Big commit.
15
16
17
18
19
20
21
22
23
24
25
26
       * This is the model class for table "user".
       * @property integer        $id
       * @property string         $username
       * @property string         $auth_key
       * @property string         $password_hash
       * @property string         $password_reset_token
       * @property string         $email
       * @property integer        $status
       * @property integer        $created_at
       * @property integer        $updated_at
       * @property CommentModel[] $comments
       * @property RatingModel[]  $ratings
d8c1a2e0   Yarik   Big commit artbox
27
       */
36d1807a   Yarik   Big commit.
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
      class User extends ActiveRecord implements UserRbacInterface, IdentityInterface
      {
          
          const STATUS_DELETED = 0;
          const STATUS_ACTIVE = 10;
          
          public $password;
          
          /**
           * @inheritdoc
           */
          public static function tableName()
          {
              return 'user';
          }
          
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'username',
                          'password',
                          'email',
                      ],
                      'required',
                  ],
                  [
                      [
                          'status',
                          'created_at',
                          'updated_at',
                      ],
                      'integer',
                  ],
                  [
                      [
                          'username',
                          'password_hash',
                          'password_reset_token',
                          'email',
                      ],
                      'string',
                      'max' => 255,
                  ],
                  [
                      [ 'auth_key' ],
                      'string',
                      'max' => 32,
                  ],
                  [
                      [ 'password_reset_token' ],
                      'unique',
                  ],
                  [
                      'email',
                      'unique',
                      'targetClass' => '\backend\models\User',
                      'message'     => Yii::t('app', 'message', [
                          'field' => 'Email',
                      ]),
                  ],
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
                  TimestampBehavior::className(),
36d1807a   Yarik   Big commit.
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
              ];
          }
          
          public function beforeSave($insert)
          {
              $this->setPassword($this->password);
              $this->generateAuthKey();
              return parent::beforeSave($insert);
          }
          
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'id'                   => 'ID',
                  'username'             => 'Username',
                  'auth_key'             => 'Auth Key',
                  'password_hash'        => 'Password Hash',
                  'password_reset_token' => 'Password Reset Token',
                  'email'                => 'Email',
                  'status'               => 'Status',
                  'created_at'           => 'Created At',
                  'updated_at'           => 'Updated At',
              ];
          }
          
          /**
           * Generates "remember me" authentication key
           */
          public function generateAuthKey()
          {
              $this->auth_key = Yii::$app->security->generateRandomString();
          }
          
          /**
           * Generates password hash from password and sets it to the model
           *
           * @param string $password
           */
          public function setPassword($password)
          {
              $this->password_hash = Yii::$app->security->generatePasswordHash($password);
          }
          
          public function getRole()
          {
              return !empty( $this->id ) ? \Yii::$app->authManager->getRolesByUser($this->id) : "";
          }
          
          /**
           * @inheritdoc
           */
          public function getId()
          {
              return $this->getPrimaryKey();
          }
          
          /**
           * @inheritdoc
           */
          public function getAuthKey()
          {
              return $this->auth_key;
          }
          
          /**
           * @inheritdoc
           */
          public function validateAuthKey($authKey)
          {
              return $this->getAuthKey() === $authKey;
          }
          
          /**
           * @inheritdoc
           */
          public static function findIdentityByAccessToken($token, $type = NULL)
          {
              throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
          }
          
          /**
           * @inheritdoc
           */
          public static function findIdentity($id)
          {
              return static::findOne([
                  'id'     => $id,
                  'status' => self::STATUS_ACTIVE,
              ]);
          }
          
          /**
           * @return \yii\db\ActiveQuery
           */
          public function getComments()
          {
              return $this->hasMany(CommentModel::className(), [ 'user_id' => 'id' ]);
          }
          
          /**
           * @return \yii\db\ActiveQuery
           */
          public function getRatings()
          {
              return $this->hasMany(RatingModel::className(), [ 'user_id' => 'id' ]);
          }
          
36d1807a   Yarik   Big commit.
213
214
215
216
217
          public function getUserName()
          {
              return $this->username;
          }
          
d8c1a2e0   Yarik   Big commit artbox
218
      }