Completed
Push — master ( dd6d91...4dac25 )
by Alexey
02:17
created

User::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace modules\admin\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use modules\admin\Module;
8
9
/**
10
 * Class User
11
 * @package modules\admin\models
12
 *
13
 * @property $password string
14
 */
15
class User extends \modules\users\models\User
16
{
17
    /**
18
     * @var string
19
     */
20
    public $password;
21
22
    const SCENARIO_ADMIN_CREATE = 'adminCreate';
23
24
    /**
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        return [
30
            [['username', 'email', 'status'], 'required'],
31
            [['password'], 'required', 'on' => [self::SCENARIO_ADMIN_CREATE]],
32
33
            ['username', 'match', 'pattern' => '#^[\w_-]+$#i'],
34
            ['username', 'unique', 'targetClass' => self::class, 'message' => Module::t('users', 'This username is already taken.'), 'on' => [self::SCENARIO_ADMIN_CREATE]],
35
            ['username', 'string', 'min' => 2, 'max' => 255],
36
37
            ['email', 'email'],
38
            ['email', 'unique', 'targetClass' => self::class, 'message' => Module::t('users', 'This email is already taken.'), 'on' => [self::SCENARIO_ADMIN_CREATE]],
39
            ['email', 'string', 'max' => 255],
40
41
            [['first_name', 'last_name'], 'string', 'max' => 45],
42
43
            ['status', 'default', 'value' => self::STATUS_WAIT],
44
            ['status', 'in', 'range' => array_keys(self::getStatusesArray())],
45
46
            ['password', 'string', 'min' => self::LENGTH_STRING_PASSWORD_MIN, 'max' => self::LENGTH_STRING_PASSWORD_MAX],
47
            ['registration_type', 'safe'],
48
        ];
49
    }
50
51
    /**
52
     * @inheritdoc
53
     * @return array
54
     */
55
    public function attributeLabels()
56
    {
57
        return ArrayHelper::merge(parent::attributeLabels(), [
58
            'password' => Module::t('users', 'Password'),
59
        ]);
60
    }
61
62
    /**
63
     * Actions before saving
64
     *
65
     * @param bool $insert
66
     * @return bool
67
     * @throws \yii\base\Exception
68
     */
69
    public function beforeSave($insert)
70
    {
71
        if (parent::beforeSave($insert)) {
72
            if (!empty($this->password)) {
73
                $this->setPassword($this->password);
74
                $this->registration_type = Yii::$app->user->id;
75
            }
76
            return true;
77
        }
78
        return false;
79
    }
80
}
81