Blame view

backend/models/User.php 4.39 KB
d8d0c38c   Administrator   add active menu
1
2
3
4
5
6
7
8
  <?php
  
  namespace backend\models;
  
  use developeruz\db_rbac\interfaces\UserRbacInterface;
  use common\models\Share;
  use common\modules\comment\models\Comment;
  use common\modules\comment\models\Rating;
2ffff539   Administrator   проапдейтил роли
9
  use yii\base\NotSupportedException;
d8d0c38c   Administrator   add active menu
10
  use Yii;
2ffff539   Administrator   проапдейтил роли
11
12
13
  use yii\behaviors\TimestampBehavior;
  use yii\db\ActiveRecord;
  use yii\web\IdentityInterface;
d8d0c38c   Administrator   add active menu
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  /**
   * 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 Comment[] $comments
   * @property Rating[] $ratings
   * @property Share[] $shares
   */
2ffff539   Administrator   проапдейтил роли
31
  class User extends ActiveRecord  implements UserRbacInterface, IdentityInterface
d8d0c38c   Administrator   add active menu
32
  {
2ffff539   Administrator   проапдейтил роли
33
34
35
36
37
  
      const STATUS_DELETED = 0;
      const STATUS_ACTIVE = 10;
      public $password;
  
d8d0c38c   Administrator   add active menu
38
39
40
41
42
43
44
45
46
47
48
49
50
51
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'user';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
2ffff539   Administrator   проапдейтил роли
52
              [['username', 'password', 'email'], 'required'],
d8d0c38c   Administrator   add active menu
53
54
55
              [['status', 'created_at', 'updated_at'], 'integer'],
              [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
              [['auth_key'], 'string', 'max' => 32],
d8d0c38c   Administrator   add active menu
56
              [['password_reset_token'], 'unique'],
2ffff539   Administrator   проапдейтил роли
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
              ['email', 'unique', 'targetClass' => '\backend\models\User', 'message' =>  Yii::t('app','message',[
                  'field' => 'Email'
              ])],
          ];
      }
  
  
      /**
       * @inheritdoc
       */
      public function behaviors()
      {
          return [
              TimestampBehavior::className(),
              [
                  'class'              => 'common\behaviors\ShowImage',
              ],
d8d0c38c   Administrator   add active menu
74
75
76
          ];
      }
  
2ffff539   Administrator   проапдейтил роли
77
78
79
80
81
82
83
84
85
86
  
      public function beforeSave($insert)
      {
          $this->setPassword($this->password);
          $this->generateAuthKey();
          return parent::beforeSave($insert);
      }
  
  
  
d8d0c38c   Administrator   add active menu
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      /**
       * @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',
          ];
      }
  
2ffff539   Administrator   проапдейтил роли
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
  
      /**
       * 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]);
      }
  
d8d0c38c   Administrator   add active menu
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getComments()
      {
          return $this->hasMany(Comment::className(), ['user_id' => 'id']);
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getRatings()
      {
          return $this->hasMany(Rating::className(), ['user_id' => 'id']);
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getShares()
      {
          return $this->hasMany(Share::className(), ['user_id' => 'id']);
      }
  
d8d0c38c   Administrator   add active menu
197
198
199
200
201
202
  
      public function getUserName()
      {
          return $this->username;
      }
  
d8d0c38c   Administrator   add active menu
203
204
  
  }