Passed
Push — master ( c9d60c...0ec193 )
by Alexey
02:24
created

User::getRegistrationTypesArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace modules\users\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii\helpers\Html;
8
use modules\users\models\query\UserQuery;
9
use modules\users\Module;
10
11
/**
12
 * Class User
13
 * @package modules\users\models
14
 *
15
 * @property array statusesArray Array statuses
16
 * @property string userFullName Full user name
17
 * @property int|string registrationType Type registered
18
 * @property int $registration_type Type Registration
19
 * @property string statusLabelName Status name in label
20
 * @property string statusName Status name
21
 */
22
class User extends IdentityUser
23
{
24
    const LENGTH_STRING_PASSWORD_MIN = 6;
25
    const LENGTH_STRING_PASSWORD_MAX = 16;
26
27
    /**
28
     * @var string
29
     */
30
    public $currentPassword;
31
32
    /**
33
     * @var string
34
     */
35
    public $newPassword;
36
37
    /**
38
     * @var string
39
     */
40
    public $newPasswordRepeat;
41
42
    /**
43
     * @return array
44
     */
45
    public function rules()
46
    {
47
        return ArrayHelper::merge(parent::rules(), [
48
            ['status', 'integer'],
49
            ['status', 'default', 'value' => self::STATUS_WAIT],
50
            ['status', 'in', 'range' => array_keys(self::getStatusesArray())],
51
52
            [['newPassword', 'newPasswordRepeat'], 'required', 'on' => [self::SCENARIO_ADMIN_CREATE, self::SCENARIO_PASSWORD_UPDATE, self::SCENARIO_ADMIN_PASSWORD_UPDATE]],
53
            ['newPassword', 'string', 'min' => self::LENGTH_STRING_PASSWORD_MIN],
54
            ['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'],
55
            ['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false],
56
        ]);
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function scenarios()
63
    {
64
        $scenarios = parent::scenarios();
65
        $scenarios[self::SCENARIO_ADMIN_CREATE] = ['avatar', 'username', 'email', 'status', 'newPassword', 'newPasswordRepeat', 'first_name', 'last_name'];
66
        $scenarios[self::SCENARIO_ADMIN_UPDATE] = ['username', 'email', 'status', 'first_name', 'last_name', 'newPassword', 'newPasswordRepeat'];
67
        $scenarios[self::SCENARIO_ADMIN_PASSWORD_UPDATE] = ['newPassword', 'newPasswordRepeat'];
68
        $scenarios[self::SCENARIO_PASSWORD_UPDATE] = ['currentPassword', 'newPassword', 'newPasswordRepeat'];
69
        $scenarios[self::SCENARIO_PROFILE_UPDATE] = ['username', 'email', 'first_name', 'last_name'];
70
        $scenarios[self::SCENARIO_PROFILE_DELETE] = ['status'];
71
        $scenarios['default'] = ['username', 'email', 'first_name', 'last_name', 'password_hash', 'status', 'auth_key', 'email_confirm_token'];
72
        return $scenarios;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function attributeLabels()
79
    {
80
        return ArrayHelper::merge(parent::attributeLabels(), [
81
            'userRoleName' => Module::t('module', 'Role'),
82
            'currentPassword' => Module::t('module', 'Current Password'),
83
            'newPassword' => Module::t('module', 'New Password'),
84
            'newPasswordRepeat' => Module::t('module', 'Repeat Password'),
85
        ]);
86
    }
87
88
    /**
89
     * @param string $attribute
90
     */
91
    public function validateCurrentPassword($attribute)
92
    {
93
        if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) {
94
            $this->processValidatePassword($attribute);
95
        } else {
96
            $this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.'));
97
        }
98
    }
99
100
    /**
101
     * @param string $attribute
102
     */
103
    protected function processValidatePassword($attribute)
104
    {
105
        if ($attribute) {
106
            if (!$this->validatePassword($this->$attribute))
107
                $this->addError($attribute, Module::t('module', 'Incorrect current password.'));
108
        } else {
109
            $this->addError($attribute, Module::t('module', 'Enter your current password.'));
110
        }
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public static function getStatusesArray()
117
    {
118
        return [
119
            self::STATUS_BLOCKED => Module::t('module', 'Blocked'),
120
            self::STATUS_ACTIVE => Module::t('module', 'Active'),
121
            self::STATUS_WAIT => Module::t('module', 'Wait'),
122
            self::STATUS_DELETED => Module::t('module', 'Deleted'),
123
        ];
124
    }
125
126
    /**
127
     * Set Status
128
     * @return int|string
129
     */
130
    public function setStatus()
131
    {
132
        switch ($this->status) {
133
            case self::STATUS_ACTIVE:
134
                $this->status = self::STATUS_BLOCKED;
135
                break;
136
            case self::STATUS_DELETED:
137
                $this->status = self::STATUS_WAIT;
138
                break;
139
            default:
140
                $this->status = self::STATUS_ACTIVE;
141
        }
142
        return $this->status;
143
    }
144
145
    /**
146
     * @param integer|string $id
147
     * @return bool
148
     */
149
    public function isSuperAdmin($id = '')
150
    {
151
        $id = $id ? $id : $this->id;
152
        $authManager = Yii::$app->authManager;
153
        $roles = $authManager->getRolesByUser($id);
154
        foreach ($roles as $role) {
155
            if ($role->name == \modules\rbac\models\Role::ROLE_SUPER_ADMIN)
156
                return true;
157
        }
158
        return false;
159
    }
160
161
    /**
162
     * @return bool
163
     */
164
    public function isDeleted()
165
    {
166
        return $this->status === self::STATUS_DELETED;
167
    }
168
169
    /**
170
     * @return mixed
171
     */
172
    public function getStatusName()
173
    {
174
        return ArrayHelper::getValue(self::getStatusesArray(), $this->status);
175
    }
176
177
    /**
178
     * Return <span class="label label-success">Active</span>
179
     * @return string
180
     */
181
    public function getStatusLabelName()
182
    {
183
        $name = ArrayHelper::getValue(self::getLabelsArray(), $this->status);
184
        return Html::tag('span', $this->getStatusName(), ['class' => 'label label-' . $name]);
185
    }
186
187
    /**
188
     * @return array
189
     */
190
    public static function getLabelsArray()
191
    {
192
        return [
193
            self::STATUS_BLOCKED => 'default',
194
            self::STATUS_ACTIVE => 'success',
195
            self::STATUS_WAIT => 'warning',
196
            self::STATUS_DELETED => 'danger',
197
        ];
198
    }
199
200
    /**
201
     * @return object|\yii\db\ActiveQuery
202
     * @throws \yii\base\InvalidConfigException
203
     */
204
    public static function find()
205
    {
206
        return Yii::createObject(UserQuery::class, [get_called_class()]);
207
    }
208
209
    /**
210
     * Actions before saving
211
     *
212
     * @param bool $insert
213
     * @return bool
214
     */
215
    /**
216
     * Actions before saving
217
     *
218
     * @param bool $insert
219
     * @return bool
220
     * @throws \yii\base\Exception
221
     */
222
    public function beforeSave($insert)
223
    {
224
        if (parent::beforeSave($insert)) {
225
            if ($insert) {
226
                $this->generateAuthKey();
227
            }
228
            if (!empty($this->newPassword)) {
229
                $this->setPassword($this->newPassword);
230
            }
231
            return true;
232
        }
233
        return false;
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function beforeDelete()
240
    {
241
        if (!parent::beforeDelete()) {
242
            return false;
243
        }
244
245
        $authManager = Yii::$app->getAuthManager();
246
        if ($authManager->getRolesByUser($this->id)) {
247
            $authManager->revokeAll($this->id);
248
        }
249
        return true;
250
    }
251
}
252