Completed
Push — master ( ccfeeb...7d9725 )
by Alexey
02:50 queued 46s
created

Profile::validateCurrentPassword()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 2
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('module', 'This username is already taken.')],
57
            ['username', 'string', 'min' => 2, 'max' => 255],
58
59
            ['email', 'required'],
60
            ['email', 'email'],
61
            ['email', 'unique', 'targetClass' => self::class, 'message' => Module::t('module', 'This email is already taken.')],
62
            ['email', 'string', 'max' => 255],
63
64
            ['first_name', 'string', 'max' => 45],
65
            ['last_name', 'string', 'max' => 45],
66
67
            [['currentPassword', 'newPassword', 'newPasswordRepeat'], 'required', 'on' => [self::SCENARIO_PASSWORD_UPDATE]],
68
            ['newPassword', 'string', 'min' => self::LENGTH_STRING_PASSWORD_MIN, 'on' => [self::SCENARIO_PASSWORD_UPDATE]],
69
            ['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword', 'on' => [self::SCENARIO_PASSWORD_UPDATE]],
70
            ['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false, 'on' => [self::SCENARIO_PASSWORD_UPDATE]],
71
        ];
72
    }
73
74
    /**
75
     * @param string $attribute
76
     */
77
    public function validateCurrentPassword($attribute)
78
    {
79
        if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) {
80
            $this->processValidatePassword($attribute);
81
        } else {
82
           $this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.'));
83
        }
84
    }
85
86
    /**
87
     * @param string $attribute
88
     */
89
    public function processValidatePassword($attribute)
90
    {
91
        if ($attribute) {
92
            if (!$this->validatePassword($this->$attribute))
93
                $this->addError($attribute, Module::t('module', 'Incorrect current password.'));
94
        } else {
95
            $this->addError($attribute, Module::t('module', 'Enter your current password.'));
96
        }
97
    }
98
99
    /**
100
     * @inheritdoc
101
     * @return array
102
     */
103
    public function attributeLabels()
104
    {
105
        return ArrayHelper::merge(parent::attributeLabels(), [
106
            'currentPassword' => Module::t('module', 'Current Password'),
107
            'newPassword' => Module::t('module', 'New Password'),
108
            'newPasswordRepeat' => Module::t('module', 'Repeat Password'),
109
        ]);
110
    }
111
112
    /**
113
     * Actions before saving
114
     *
115
     * @param bool $insert
116
     * @return bool
117
     * @throws \yii\base\Exception
118
     */
119
    public function beforeSave($insert)
120
    {
121
        if (parent::beforeSave($insert)) {
122
            if (!empty($this->newPassword)) {
123
                $this->setPassword($this->newPassword);
124
            }
125
            return true;
126
        }
127
        return false;
128
    }
129
}
130