Completed
Push — master ( 17af2a...d4404d )
by Andrii
01:58
created

PasswordValidator::inlineFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace hiam\mrdp\validators;
4
5
use hiam\validators\PasswordValidatorInterface;
6
use Yii;
7
use yii\base\Model;
8
use yii\validators\Validator;
9
10
class PasswordValidator extends Validator implements PasswordValidatorInterface
11
{
12
    /**
13
     * @var string login attribute name
14
     */
15
    public $loginAttribute = 'login';
16
17
    /**
18
     * @var string password attribute name
19
     */
20
    public $passwordAttribute = 'current_password';
21
22
    public function init()
23
    {
24
        parent::init();
25
        if ($this->message !== null) {
26
            return;
27
        }
28
        $this->message = Yii::t('hiam', 'The current password is incorrect');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function validateAttribute($model, $attribute)
35
    {
36
        if ($model->{$this->loginAttribute} && $model->{$this->passwordAttribute}) {
37
            $valid = Yii::$app->db->createCommand("
38
                    SELECT      zc.obj_id
39
                    FROM        zclient zc
40
                    WHERE       zc.state_id = ANY(state_ids('client', 'ok,active,new'))
41
                                AND NOT check_password('', zc.password)
42
                                AND login = :login AND check_password(:password, zc.password)
43
                ")->bindValues([':login' => $model->{$this->loginAttribute}, ':password' => $model->{$this->passwordAttribute}])->queryScalar();
44
            if (!$valid) {
45
                $this->addError($model, $attribute, $this->message);
46
            }
47
        }
48
    }
49
50
    public function inlineFor(Model $model): \Closure
51
    {
52
        return function (string $attribute) use ($model) {
53
            $this->validateAttribute($model, $attribute);
54
        };
55
    }
56
}
57