Completed
Push — master ( 7d233a...8ee6e7 )
by Dmitry
12:15
created

src/storage/ClientQuery.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * HIAM module for MRDP database compatibility
4
 *
5
 * @link      https://github.com/hiqdev/hiam-mrdp
6
 * @package   hiam-mrdp
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiam\mrdp\storage;
12
13
use yii\db\Query;
14
15
class ClientQuery extends \yii\db\ActiveQuery
16
{
17
    public function init()
18
    {
19
        parent::init();
20
        $this
21
            ->select([
22
                'c.obj_id       AS id',
23
                'c.login        AS username',
24
                'r.login        AS seller',
25
                'c.seller_id    AS seller_id',
26
                'y.name         AS type',
27
                'z.name         AS state',
28
                'e.roles        AS roles',
29
                'k.first_name   AS first_name',
30
                'k.last_name    AS last_name',
31
                't.value        AS totp_secret',
32
                'coalesce(i.value,l.value) AS allowed_ips',
33
                'coalesce(c.email,k.email) AS email',
34
                "encode(digest(c.password, 'sha1'), 'hex') AS password_hash",
35
                'o.value        AS email_confirmed'
36
            ])
37
            ->from('zclient             c')
38
            ->innerJoin('zclient        r', 'r.obj_id=c.seller_id')
39
            ->innerJoin('zref           y', 'y.obj_id=c.type_id')
40
            ->innerJoin('zref           z', 'z.obj_id=c.state_id')
41
            ->leftJoin('contact         k', 'k.obj_id=c.obj_id')
42
            ->leftJoin('value           t', "t.obj_id=c.obj_id AND t.prop_id=prop_id('client,access:totp_secret')")
43
            ->leftJoin('value           i', "i.obj_id=c.obj_id AND i.prop_id=prop_id('client,access:allowed_ips')")
44
            ->leftJoin('value           l', "l.obj_id=c.obj_id AND l.prop_id=prop_id('login_ips:panel')")
45
            ->leftJoin('value           o', "o.obj_id=c.obj_id AND o.prop_id=prop_id('contact:email_confirmed')")
46
            ->leftJoin('client2rolez    e', 'e.client_id=c.obj_id')
47
            ->andWhere(['in', 'z.name', ['ok', 'new']]);
48
    }
49
50
    public function andWhere($condition, $params = [])
51
    {
52
        if (!is_array($condition) || isset($condition[0])) {
53
            return parent::andWhere($condition, $params);
54
        }
55
        foreach (['id', 'username', 'password', 'email', 'active'] as $key) {
56
            /// XXX `isset` does not fit here
57
            if (array_key_exists($key, $condition)) {
58
                $this->{"where$key"}($condition[$key]);
59
                unset($condition[$key]);
60
            }
61
        }
62
        if (!empty($condition)) {
63
            parent::andWhere($condition, $params);
64
        }
65
66
        return $this;
67
    }
68
69
    public function whereId($id)
70
    {
71
        return parent::andWhere(['c.obj_id' => $id]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereId()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
72
    }
73
74
    public function whereEmail($username)
75
    {
76
        return parent::andWhere(['or', 'c.login=:username', 'c.email=:username'], [':username' => $username]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereEmail()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
77
    }
78
79
    public function whereUsername($username)
80
    {
81
        $userId = (int)$username;
82
        if ($userId > 0 && "$userId" === trim($username)) {
83
            return $this->whereId($userId);
84
        }
85
86
        return parent::andWhere(['or', 'c.login=:username', 'c.email=:username'], [':username' => $username]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereUsername()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
87
    }
88
89
    public function wherePassword($password)
90
    {
91
        return parent::andWhere(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of wherePassword()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
92
            'check_password(:password,c.password) OR check_password(:password,tmp.value)',
93
            [':password' => $password]
94
        )->leftJoin('value tmp', "tmp.obj_id=c.obj_id AND tmp.prop_id=prop_id('client,access:tmp_pwd')");
95
    }
96
97
    public function whereActive($is_active)
98
    {
99
        if (is_null($is_active)) {
100
            return $this;
101
        }
102
103
        return parent::andWhere([$is_active ? 'in' : 'not in', 'z.name', ['ok', 'active']]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereActive()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
104
    }
105
}
106