1 | <?php |
||
2 | |||
3 | namespace modules\users\models; |
||
4 | |||
5 | use yii\behaviors\TimestampBehavior; |
||
6 | use yii\db\ActiveRecord; |
||
7 | use yii\web\IdentityInterface; |
||
8 | use modules\users\traits\ModuleTrait; |
||
9 | use modules\users\Module; |
||
10 | |||
11 | /** |
||
12 | * Class IdentityUser |
||
13 | * @package modules\users\models |
||
14 | * |
||
15 | * This is the model class for table "{{%users}}". |
||
16 | * |
||
17 | * @property int $id ID |
||
18 | * @property string $username Username |
||
19 | * @property string $auth_key Authorization Key |
||
20 | * @property string $password_hash Hash Password |
||
21 | * @property string $password_reset_token Password Token |
||
22 | * @property string $email_confirm_token Email Confirm Token |
||
23 | * @property string $email Email |
||
24 | * @property int|string $status Status |
||
25 | * @property int $last_visit Last Visit |
||
26 | * @property int $created_at Created |
||
27 | * @property int $updated_at Updated |
||
28 | * @property string $first_name First Name |
||
29 | * @property string $last_name Last Name |
||
30 | * @property array statusesArray Array statuses |
||
31 | * @property string $authKey |
||
32 | */ |
||
33 | class IdentityUser extends ActiveRecord implements IdentityInterface |
||
34 | { |
||
35 | use ModuleTrait; |
||
36 | |||
37 | private $roles; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
38 | |||
39 | /** |
||
40 | * Length password symbols min |
||
41 | */ |
||
42 | const LENGTH_STRING_PASSWORD_MIN = 6; |
||
43 | |||
44 | /** |
||
45 | * Length password symbols max |
||
46 | */ |
||
47 | const LENGTH_STRING_PASSWORD_MAX = 16; |
||
48 | |||
49 | /** |
||
50 | * Users Statuses |
||
51 | */ |
||
52 | const STATUS_BLOCKED = 0; |
||
53 | const STATUS_ACTIVE = 1; |
||
54 | const STATUS_WAIT = 2; |
||
55 | const STATUS_DELETED = 3; |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | * @return string |
||
60 | */ |
||
61 | public static function tableName() |
||
62 | { |
||
63 | return '{{%user}}'; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | * @return array |
||
69 | */ |
||
70 | public function behaviors() |
||
71 | { |
||
72 | return [ |
||
73 | 'timestamp' => [ |
||
74 | 'class' => TimestampBehavior::class, |
||
75 | ], |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | * @return array |
||
82 | */ |
||
83 | public function rules() |
||
84 | { |
||
85 | return [ |
||
86 | ['username', 'required'], |
||
87 | ['username', 'match', 'pattern' => '#^[\w_-]+$#i'], |
||
88 | ['username', 'unique', 'targetClass' => self::class, |
||
89 | 'message' => Module::t( |
||
90 | 'module', |
||
91 | 'This username is already taken.' |
||
92 | ) |
||
93 | ], |
||
94 | ['username', 'string', 'min' => 2, 'max' => 255], |
||
95 | |||
96 | ['email', 'required'], |
||
97 | ['email', 'email'], |
||
98 | ['email', 'unique', 'targetClass' => self::class, |
||
99 | 'message' => Module::t( |
||
100 | 'module', |
||
101 | 'This email is already taken.' |
||
102 | ) |
||
103 | ], |
||
104 | ['email', 'string', 'max' => 255], |
||
105 | |||
106 | ['first_name', 'string', 'max' => 45], |
||
107 | ['last_name', 'string', 'max' => 45], |
||
108 | |||
109 | ['status', 'integer'], |
||
110 | ['status', 'default', 'value' => self::STATUS_WAIT], |
||
111 | ['status', 'in', 'range' => array_keys(self::getStatusesArray())], |
||
112 | ]; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | * @return array |
||
118 | */ |
||
119 | public function attributeLabels() |
||
120 | { |
||
121 | return [ |
||
122 | 'id' => 'ID', |
||
123 | 'created_at' => Module::t('module', 'Created'), |
||
124 | 'updated_at' => Module::t('module', 'Updated'), |
||
125 | 'last_visit' => Module::t('module', 'Last Visit'), |
||
126 | 'username' => Module::t('module', 'Username'), |
||
127 | 'email' => Module::t('module', 'Email'), |
||
128 | 'auth_key' => Module::t('module', 'Auth Key'), |
||
129 | 'status' => Module::t('module', 'Status'), |
||
130 | 'first_name' => Module::t('module', 'First Name'), |
||
131 | 'last_name' => Module::t('module', 'Last Name'), |
||
132 | 'email_confirm_token' => Module::t('module', 'Email Confirm Token'), |
||
133 | 'password_reset_token' => Module::t('module', 'Password Reset Token'), |
||
134 | 'password_hash' => Module::t('module', 'Password Hash'), |
||
135 | ]; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param int|string $id |
||
140 | * @return IdentityInterface |
||
141 | */ |
||
142 | public static function findIdentity($id) |
||
143 | { |
||
144 | /** @var IdentityInterface $result */ |
||
145 | $result = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); |
||
146 | return $result; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param mixed $token |
||
151 | * @param null|mixed $type |
||
152 | * @return IdentityInterface |
||
153 | */ |
||
154 | public static function findIdentityByAccessToken($token, $type = null) |
||
155 | { |
||
156 | /** @var IdentityInterface $result */ |
||
157 | $result = static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]); |
||
158 | return $result; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return string|integer |
||
163 | */ |
||
164 | public function getId() |
||
165 | { |
||
166 | return $this->id; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @return string current users auth key |
||
171 | */ |
||
172 | public function getAuthKey() |
||
173 | { |
||
174 | return $this->auth_key; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param string|mixed $authKey |
||
179 | * @return boolean if auth key is valid for current users |
||
180 | */ |
||
181 | public function validateAuthKey($authKey) |
||
182 | { |
||
183 | return $this->getAuthKey() === $authKey; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return array |
||
188 | */ |
||
189 | public static function getStatusesArray() |
||
190 | { |
||
191 | return [ |
||
192 | self::STATUS_BLOCKED => Module::t('module', 'Blocked'), |
||
193 | self::STATUS_ACTIVE => Module::t('module', 'Active'), |
||
194 | self::STATUS_WAIT => Module::t('module', 'Wait'), |
||
195 | self::STATUS_DELETED => Module::t('module', 'Deleted'), |
||
196 | ]; |
||
197 | } |
||
198 | } |
||
199 |