Completed
Pull Request — master (#37)
by
unknown
01:50
created

SettingsForm::rules()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
ccs 0
cts 29
cp 0
rs 8.8571
cc 2
eloc 22
nc 1
nop 0
crap 6
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Form;
13
14
use Da\User\Factory\EmailChangeStrategyFactory;
15
use Da\User\Helper\SecurityHelper;
16
use Da\User\Model\User;
17
use Da\User\Traits\ContainerAwareTrait;
18
use Da\User\Traits\ModuleAwareTrait;
19
use Yii;
20
use yii\base\Model;
21
22
class SettingsForm extends Model
23
{
24
    use ModuleAwareTrait;
25
    use ContainerAwareTrait;
26
27
    /**
28
     * @var string
29
     */
30
    public $email;
31
    /**
32
     * @var string
33
     */
34
    public $username;
35
    /**
36
     * @var string
37
     */
38
    public $new_password;
39
    /**
40
     * @var string
41
     */
42
    public $current_password;
43
    /**
44
     * @var SecurityHelper
45
     */
46
    protected $securityHelper;
47
48
    /** @var User */
49
    protected $user;
50
51
    public function __construct(SecurityHelper $securityHelper, array $config = [])
52
    {
53
        $this->securityHelper = $securityHelper;
54
        parent::__construct($config);
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function rules()
61
    {
62
        return [
63
            'usernameRequired' => ['username', 'required'],
64
            'usernameTrim' => ['username', 'filter', 'filter' => 'trim'],
65
            'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
66
            'usernamePattern' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'],
67
            'emailRequired' => ['email', 'required'],
68
            'emailTrim' => ['email', 'filter', 'filter' => 'trim'],
69
            'emailPattern' => ['email', 'email'],
70
            'emailUsernameUnique' => [
71
                ['email', 'username'],
72
                'unique',
73
                'when' => function ($model, $attribute) {
74
                    return $this->getUser()->$attribute != $model->$attribute;
75
                },
76
                'targetClass' => $this->getClassMap()->get(User::class),
77
            ],
78
            'newPasswordLength' => ['new_password', 'string', 'max' => 72, 'min' => 6],
79
            'currentPasswordRequired' => ['current_password', 'required'],
80
            'currentPasswordValidate' => [
81
                'current_password',
82
                function ($attribute) {
83
                    if (!$this->securityHelper->validatePassword($this->$attribute, $this->getUser()->password_hash)) {
0 ignored issues
show
Bug introduced by
Accessing password_hash on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
84
                        $this->addError($attribute, Yii::t('usuario', 'Current password is not valid'));
85
                    }
86
                },
87
            ],
88
        ];
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function attributeLabels()
95
    {
96
        return [
97
            'email' => Yii::t('usuario', 'Email'),
98
            'username' => Yii::t('usuario', 'Username'),
99
            'new_password' => Yii::t('usuario', 'New password'),
100
            'current_password' => Yii::t('usuario', 'Current password'),
101
        ];
102
    }
103
104
    /**
105
     * @return User|null|\yii\web\IdentityInterface
106
     */
107
    public function getUser()
108
    {
109
        if ($this->user == null) {
110
            $this->user = Yii::$app->user->identity;
111
        }
112
113
        return $this->user;
114
    }
115
116
    /**
117
     * Saves new account settings.
118
     *
119
     * @return bool
120
     */
121
    public function save()
122
    {
123
        if ($this->validate()) {
124
            $user = $this->getUser();
125
            if ($user instanceof User) {
126
                $user->scenario = 'settings';
127
                $user->username = $this->username;
128
                $user->password = $this->new_password;
129
                if ($this->email == $user->email && $user->unconfirmed_email != null) {
130
                    $user->unconfirmed_email = null;
131
                } elseif ($this->email != $user->email) {
132
                    $strategy = EmailChangeStrategyFactory::makeByStrategyType(
133
                        $this->getModule()->emailChangeStrategy,
134
                        $this
135
                    );
136
137
                    return $strategy->run();
138
                }
139
140
                return $user->save();
141
            }
142
        }
143
144
        return false;
145
    }
146
}
147