Completed
Push — master ( ccfeeb...7d9725 )
by Alexey
02:50 queued 46s
created

BaseUser::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace modules\users\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
use yii\db\ActiveRecord;
8
use yii\web\IdentityInterface;
9
use yii\helpers\Html;
10
use yii\helpers\ArrayHelper;
11
use modules\users\traits\ModuleTrait;
12
use modules\users\Module;
13
14
/**
15
 * Class BaseUser
16
 * @package modules\users\models
17
 *
18
 * This is the model class for table "{{%users}}".
19
 *
20
 * @property int $id ID
21
 * @property string $username Username
22
 * @property string $auth_key Authorization Key
23
 * @property string $password_hash Hash Password
24
 * @property string $password_reset_token Password Token
25
 * @property string $email_confirm_token Email Confirm Token
26
 * @property string $email Email
27
 * @property int|string $status Status
28
 * @property int $last_visit Last Visit
29
 * @property int $created_at Created
30
 * @property int $updated_at Updated
31
 * @property string $first_name First Name
32
 * @property string $last_name Last Name
33
 * @property int $registration_type Type Registration
34
 * @property string statusLabelName Status name in label
35
 * @property array statusesArray Array statuses
36
 * @property string statusName Name status
37
 * @property int|string registrationType Type registered
38
 */
39
class BaseUser extends ActiveRecord implements IdentityInterface
40
{
41
    use ModuleTrait;
42
43
    /**
44
     * Length password symbols min
45
     */
46
    const LENGTH_STRING_PASSWORD_MIN = 6;
47
48
    /**
49
     * Length password symbols max
50
     */
51
    const LENGTH_STRING_PASSWORD_MAX = 16;
52
53
    /**
54
     * Users Statuses
55
     */
56
    const STATUS_BLOCKED = 0;
57
    const STATUS_ACTIVE = 1;
58
    const STATUS_WAIT = 2;
59
    const STATUS_DELETED = 3;
60
61
    /**
62
     * Type of registration
63
     */
64
    const TYPE_REGISTRATION_SYSTEM = 0;
65
66
    /**
67
     * @inheritdoc
68
     * @return string
69
     */
70
    public static function tableName()
71
    {
72
        return '{{%users}}';
73
    }
74
75
    /**
76
     * @inheritdoc
77
     * @return array
78
     */
79
    public function behaviors()
80
    {
81
        return [
82
            'timestamp' => [
83
                'class' => TimestampBehavior::class,
84
            ],
85
        ];
86
    }
87
88
    /**
89
     * @inheritdoc
90
     * @return array
91
     */
92
    public function rules()
93
    {
94
        return [
95
            ['username', 'required'],
96
            ['username', 'match', 'pattern' => '#^[\w_-]+$#i'],
97
            ['username', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This username is already taken.')],
98
            ['username', 'string', 'min' => 2, 'max' => 255],
99
100
            ['email', 'required'],
101
            ['email', 'email'],
102
            ['email', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This email is already taken.')],
103
            ['email', 'string', 'max' => 255],
104
105
            ['first_name', 'string', 'max' => 45],
106
            ['last_name', 'string', 'max' => 45],
107
108
            ['registration_type', 'safe'],
109
110
            ['status', 'integer'],
111
            ['status', 'default', 'value' => self::STATUS_WAIT],
112
            ['status', 'in', 'range' => array_keys(self::getStatusesArray())],
113
        ];
114
    }
115
116
    /**
117
     * @inheritdoc
118
     * @return array
119
     */
120
    public function attributeLabels()
121
    {
122
        return [
123
            'id' => 'ID',
124
            'created_at' => Module::t('module', 'Created'),
125
            'updated_at' => Module::t('module', 'Updated'),
126
            'last_visit' => Module::t('module', 'Last Visit'),
127
            'username' => Module::t('module', 'Username'),
128
            'email' => Module::t('module', 'Email'),
129
            'auth_key' => Module::t('module', 'Auth Key'),
130
            'status' => Module::t('module', 'Status'),
131
            'first_name' => Module::t('module', 'First Name'),
132
            'last_name' => Module::t('module', 'Last Name'),
133
            'registration_type' => Module::t('module', 'Registration Type'),
134
        ];
135
    }
136
137
    /**
138
     * @param int|string $id
139
     * @return IdentityInterface
140
     */
141
    public static function findIdentity($id)
142
    {
143
        /** @var  IdentityInterface $result */
144
        $result = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
145
        return $result;
146
    }
147
148
    /**
149
     * @param mixed $token
150
     * @param null|mixed $type
151
     * @return IdentityInterface
152
     */
153
    public static function findIdentityByAccessToken($token, $type = null)
154
    {
155
        /** @var  IdentityInterface $result */
156
        $result = static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]);
157
        return $result;
158
    }
159
160
    /**
161
     * @return string|integer
162
     */
163
    public function getId()
164
    {
165
        return $this->id;
166
    }
167
168
    /**
169
     * @return string current users auth key
170
     */
171
    public function getAuthKey()
172
    {
173
        return $this->auth_key;
174
    }
175
176
    /**
177
     * @param string|mixed $authKey
178
     * @return boolean if auth key is valid for current users
179
     */
180
    public function validateAuthKey($authKey)
181
    {
182
        return $this->getAuthKey() === $authKey;
183
    }
184
185
    /**
186
     * Finds users by username
187
     *
188
     * @param string $username
189
     * @return static|null
190
     */
191
    public static function findByUsername($username)
192
    {
193
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
194
    }
195
196
    /**
197
     * Generates "remember me" authentication key
198
     */
199
    public function generateAuthKey()
200
    {
201
        $this->auth_key = Yii::$app->security->generateRandomString();
202
    }
203
204
    /**
205
     * Actions before saving
206
     *
207
     * @param bool $insert
208
     * @return bool
209
     */
210
    public function beforeSave($insert)
211
    {
212
        if (parent::beforeSave($insert)) {
213
            if ($insert) {
214
                $this->generateAuthKey();
215
            }
216
            return true;
217
        }
218
        return false;
219
    }
220
221
    /**
222
     * @return array
223
     */
224
    public static function getStatusesArray()
225
    {
226
        return [
227
            self::STATUS_BLOCKED => Module::t('module', 'Blocked'),
228
            self::STATUS_ACTIVE => Module::t('module', 'Active'),
229
            self::STATUS_WAIT => Module::t('module', 'Wait'),
230
            self::STATUS_DELETED => Module::t('module', 'Deleted'),
231
        ];
232
    }
233
234
    /**
235
     * @return mixed
236
     */
237
    public function getStatusName()
238
    {
239
        return ArrayHelper::getValue(self::getStatusesArray(), $this->status);
240
    }
241
242
    /**
243
     * Return <span class="label label-success">Active</span>
244
     * @return string
245
     */
246
    public function getStatusLabelName()
247
    {
248
        $name = ArrayHelper::getValue(self::getLabelsArray(), $this->status);
249
        return Html::tag('span', $this->getStatusName(), ['class' => 'label label-' . $name]);
250
    }
251
252
    /**
253
     * @return array
254
     */
255
    public static function getLabelsArray()
256
    {
257
        return [
258
            self::STATUS_BLOCKED => 'default',
259
            self::STATUS_ACTIVE => 'success',
260
            self::STATUS_WAIT => 'warning',
261
            self::STATUS_DELETED => 'danger',
262
        ];
263
    }
264
}
265