Completed
Push — master ( 22227d...41329c )
by Andrii
03:15
created

PasswordValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

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