Profile::processValidatePassword()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace modules\users\models;
4
5
use yii\helpers\ArrayHelper;
6
use modules\users\traits\ModuleTrait;
7
use modules\users\Module;
8
9
/**
10
 * Class Profile
11
 * @package modules\users\models
12
 *
13
 * This is the model class extends User.
14
 *
15
 * @property string $username Username
16
 * @property string $currentPassword Current Password
17
 * @property string $newPassword New Password
18
 * @property string $newPasswordRepeat Repeat New Password
19
 * @property string $email Email
20
 * @property string $first_name First Name
21
 * @property string $last_name Last Name
22
 */
23
class Profile extends User
24
{
25
    use ModuleTrait;
26
27
    /**
28
     * Scenarios
29
     */
30
    const SCENARIO_PASSWORD_UPDATE = 'updatePassword';
31
32
    /**
33
     * @var string
34
     */
35
    public $currentPassword;
36
37
    /**
38
     * @var string
39
     */
40
    public $newPassword;
41
42
    /**
43
     * @var string
44
     */
45
    public $newPasswordRepeat;
46
47
    /**
48
     * @inheritdoc
49
     * @return array
50
     */
51
    public function rules()
52
    {
53
        return [
54
            ['username', 'required'],
55
            ['username', 'match', 'pattern' => '#^[\w_-]+$#i'],
56
            ['username', 'unique', 'targetClass' => self::class, 'message' => Module::t(
57
                'module',
58
                'This username is already taken.'
59
            )],
60
            ['username', 'string', 'min' => 2, 'max' => 255],
61
62
            ['email', 'required'],
63
            ['email', 'email'],
64
            ['email', 'unique', 'targetClass' => self::class, 'message' => Module::t(
65
                'module',
66
                'This email is already taken.'
67
            )],
68
            ['email', 'string', 'max' => 255],
69
70
            ['first_name', 'string', 'max' => 45],
71
            ['last_name', 'string', 'max' => 45],
72
73
            [
74
                ['currentPassword', 'newPassword', 'newPasswordRepeat'],
75
                'required',
76
                'on' => [self::SCENARIO_PASSWORD_UPDATE]
77
            ],
78
            [
79
                'newPassword', 'string',
80
                'min' => self::LENGTH_STRING_PASSWORD_MIN,
81
                'max' => self::LENGTH_STRING_PASSWORD_MAX
82
            ],
83
            [
84
                'newPasswordRepeat', 'compare',
85
                'compareAttribute' => 'newPassword',
86
                'on' => [self::SCENARIO_PASSWORD_UPDATE]
87
            ],
88
            ['currentPassword', 'validateCurrentPassword',
89
                'skipOnEmpty' => false,
90
                'skipOnError' => false,
91
                'on' => [self::SCENARIO_PASSWORD_UPDATE]
92
            ],
93
        ];
94
    }
95
96
    /**
97
     * @param string $attribute
98
     */
99
    public function validateCurrentPassword($attribute)
100
    {
101
        if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) {
102
            $this->processValidatePassword($attribute);
103
        } else {
104
            $this->addError($attribute, Module::t(
105
                'module',
106
                'Not all fields are filled in correctly.'
107
            ));
108
        }
109
    }
110
111
    /**
112
     * @param string $attribute
113
     */
114
    public function processValidatePassword($attribute)
115
    {
116
        if ($attribute) {
117
            if (!$this->validatePassword($this->$attribute)) {
118
                $this->addError($attribute, Module::t('module', 'Incorrect current password.'));
119
            }
120
        } else {
121
            $this->addError($attribute, Module::t('module', 'Enter your current password.'));
122
        }
123
    }
124
125
    /**
126
     * @inheritdoc
127
     * @return array
128
     */
129
    public function attributeLabels()
130
    {
131
        return ArrayHelper::merge(parent::attributeLabels(), [
132
            'currentPassword' => Module::t('module', 'Current Password'),
133
            'newPassword' => Module::t('module', 'New Password'),
134
            'newPasswordRepeat' => Module::t('module', 'Repeat Password'),
135
        ]);
136
    }
137
138
    /**
139
     * Actions before saving
140
     *
141
     * @param bool $insert
142
     * @return bool
143
     * @throws \yii\base\Exception
144
     */
145
    public function beforeSave($insert)
146
    {
147
        if (parent::beforeSave($insert)) {
148
            if (!empty($this->newPassword)) {
149
                $this->setPassword($this->newPassword);
150
            }
151
            return true;
152
        }
153
        return false;
154
    }
155
}
156