Completed
Push — master ( 5aafbc...cdd7d7 )
by Alexey
05:07 queued 54s
created

BaseUser::getStatusesArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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