Completed
Push — master ( 59199f...042504 )
by Alexey
02:46
created

User   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 23
c 4
b 0
f 1
dl 0
loc 138
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B validateCurrentPassword() 0 11 6
A scenarios() 0 11 1
A rules() 0 7 1
A beforeSave() 0 10 3
B setStatus() 0 10 5
A attributeLabels() 0 7 1
B getUserFullName() 0 15 6
1
<?php
2
namespace modules\users\models;
3
4
use Yii;
5
use yii\helpers\ArrayHelper;
6
use yii\helpers\Html;
7
use modules\users\Module;
8
9
/**
10
 * Class User
11
 * @package modules\users\models
12
 */
13
class User extends BaseUser
14
{
15
    const LENGTH_STRING_PASSWORD_MIN = 6;
16
    const LENGTH_STRING_PASSWORD_MAX = 16;
17
18
    const SCENARIO_ADMIN_CREATE = 'adminCreate';
19
    const SCENARIO_ADMIN_UPDATE = 'adminUpdate';
20
    const SCENARIO_ADMIN_PASSWORD_UPDATE = 'adminPasswordUpdate';
21
    const SCENARIO_PROFILE_UPDATE = 'profileUpdate';
22
    const SCENARIO_PASSWORD_UPDATE = 'passwordUpdate';
23
    const SCENARIO_PROFILE_DELETE = 'profileDelete';
24
25
    /**
26
     * @var string
27
     */
28
    public $currentPassword;
29
30
    /**
31
     * @var string
32
     */
33
    public $newPassword;
34
35
    /**
36
     * @var string
37
     */
38
    public $newPasswordRepeat;
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function rules()
44
    {
45
        return ArrayHelper::merge(parent::rules(), [
46
            [['newPassword', 'newPasswordRepeat'], 'required', 'on' => [self::SCENARIO_ADMIN_CREATE, self::SCENARIO_PASSWORD_UPDATE, self::SCENARIO_ADMIN_PASSWORD_UPDATE]],
47
            ['newPassword', 'string', 'min' => self::LENGTH_STRING_PASSWORD_MIN],
48
            ['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'],
49
            ['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false],
50
        ]);
51
    }
52
53
    /**
54
     * @param $attribute
55
     */
56
    public function validateCurrentPassword($attribute)
57
    {
58
        if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) {
59
            if ($this->$attribute) {
60
                if (!$this->validatePassword($this->$attribute))
61
                    $this->addError($attribute, Module::t('module', 'Incorrect current password.'));
62
            } else {
63
                $this->addError($attribute, Module::t('module', 'Enter your current password.'));
64
            }
65
        } else {
66
            $this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.'));
67
        }
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function scenarios()
74
    {
75
        $scenarios = parent::scenarios();
76
        $scenarios[self::SCENARIO_ADMIN_CREATE] = ['avatar', 'username', 'email', 'status', 'newPassword', 'newPasswordRepeat', 'registration_type', 'first_name', 'last_name'];
77
        $scenarios[self::SCENARIO_ADMIN_UPDATE] = ['username', 'email', 'status', 'first_name', 'last_name'];
78
        $scenarios[self::SCENARIO_ADMIN_PASSWORD_UPDATE] = ['newPassword', 'newPasswordRepeat'];
79
        $scenarios[self::SCENARIO_PASSWORD_UPDATE] = ['currentPassword', 'newPassword', 'newPasswordRepeat'];
80
        $scenarios[self::SCENARIO_PROFILE_UPDATE] = ['email', 'first_name', 'last_name'];
81
        $scenarios[self::SCENARIO_PROFILE_DELETE] = ['status'];
82
        $scenarios['default'] = ['username', 'email', 'password_hash', 'status', 'auth_key', 'email_confirm_token'];
83
        return $scenarios;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function attributeLabels()
90
    {
91
        return ArrayHelper::merge(parent::attributeLabels(), [
92
            'userRoleName' => Module::t('module', 'Role'),
93
            'currentPassword' => Module::t('module', 'Current Password'),
94
            'newPassword' => Module::t('module', 'New Password'),
95
            'newPasswordRepeat' => Module::t('module', 'Repeat Password'),
96
        ]);
97
    }
98
99
    /**
100
     * Actions before saving
101
     *
102
     * @param bool $insert
103
     * @return bool
104
     */
105
    public function beforeSave($insert)
106
    {
107
        if (parent::beforeSave($insert)) {
108
            if (!empty($this->newPassword)) {
109
                $this->setPassword($this->newPassword);
110
                Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
111
            }
112
            return true;
113
        }
114
        return false;
115
    }
116
117
    /**
118
     * Set Status
119
     */
120
    public function setStatus()
121
    {
122
        if ($this->status == self::STATUS_ACTIVE) {
123
            $this->status = self::STATUS_BLOCKED;
124
        } else if ($this->status == self::STATUS_BLOCKED) {
125
            $this->status = self::STATUS_ACTIVE;
126
        } else if ($this->status == self::STATUS_WAIT) {
127
            $this->status = self::STATUS_ACTIVE;
128
        } else if ($this->status == self::STATUS_DELETED) {
129
            $this->status = self::STATUS_WAIT;
130
        }
131
    }
132
133
    /**
134
     * @return string $userFullName
135
     */
136
    public function getUserFullName()
137
    {
138
        $fullName = '';
139
        if (Yii::$app->user) {
140
            if ($this->first_name && $this->last_name) {
141
                $fullName = $this->first_name . ' ' . $this->last_name;
142
            } else if ($this->first_name) {
143
                $fullName = $this->first_name;
144
            } else if ($this->last_name) {
145
                $fullName = $this->last_name;
146
            } else {
147
                $fullName = $this->username;
148
            }
149
        }
150
        return Html::encode($fullName);
151
    }
152
}
153