Completed
Push — master ( b1e314...321e99 )
by Dmitry
20:11
created

Client::getPassword_hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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;
14
use yii\base\InvalidConfigException;
15
use yii\db\Expression;
16
17
/**
18
 * Client model.
19
 *
20
 * @property integer $obj_id PK
21
 * @property integer $id synced with obj_id
22
 * @property integer $seller_id
23
 * @property string $password
24
 * @property string $email
25
 */
26
class Client extends \yii\db\ActiveRecord
27
{
28
    public $type;
29
    public $state;
30
    public $roles;
31
    public $seller;
32
    public $username;
33
    public $last_name;
34
    public $first_name;
35
36
    public $email_confirmed;
37
    public $allowed_ips;
38
    public $totp_secret;
39
40
    public $password_hash;
41
42
    public static function tableName()
43
    {
44
        return '{{zclient}}';
45
    }
46
47
    public static function primaryKey()
48
    {
49
        return ['obj_id'];
50
    }
51
52
    public function rules()
53
    {
54
        return [
55
            [['username', 'email', 'password', 'first_name', 'last_name'], 'trim'],
56
            [['seller_id'], 'integer'],
57
            [['state'], 'trim'],
58
            [['email_confirmed', 'allowed_ips', 'totp_secret'], 'trim'],
59
        ];
60
    }
61
62
    public function init()
63
    {
64
        parent::init();
65
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
66
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onBeforeSave']);
67
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
68
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
69
    }
70
71
    public function onBeforeInsert()
72
    {
73
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
74
        $this->login = $this->username ?: $this->email;
75
        $this->seller_id = $seller->id;
76
        $this->onBeforeSave();
77
    }
78
79
    public function onBeforeSave()
80
    {
81
        if (empty($this->password)) {
82
            unset($this->password);
83
        }
84
        if (!empty($this->state)) {
85
            $this->state_id = new Expression(
86
                "zref_id('state,client,{$this->state}')"
87
            );
88
        }
89
        if ($this->email_confirmed) {
90
            $this->email = $this->email_confirmed;
91
            $this->saveValue('contact:email_new', '');
92
            $this->saveValue('contact:email_confirmed', $this->email_confirmed);
93
            $this->saveValue('contact:email_confirm_date', new Expression("date_trunc('second', now()::timestamp)::text"));
94
        }
95
    }
96
97
    public function onAfterSave()
98
    {
99
        $this->id = $this->id ?: $this->getAgain()->id;
100
        $this->type = $this->type ?: $this->getAgain()->type;
101
102
        $contact = Contact::findOne($this->id);
103
        $contact->setAttributes($this->getAttributes($contact->safeAttributes()));
104
        $contact->save();
105
        $this->saveValue('client,access:totp_secret', $this->totp_secret);
106
        $this->saveValue('client,access:allowed_ips', $this->allowed_ips);
107
        $this->saveValue('login_ips:panel', $this->allowed_ips);
108
    }
109
110
    protected $_again;
111
112
    public function getAgain()
113
    {
114
        /// XXX this crutch is needed bacause we use `zclient` view (not table)
115
        /// XXX and yii ActiveRecord doesn't populate model properly in this case
116
        if ($this->_again === null) {
117
            $this->_again = static::find()->whereUsername($this->username)->one();
118
        }
119
120
        return $this->_again;
121
    }
122
123
    public function saveValue($prop, $value)
124
    {
125
        $params = [
126
            'id' => $this->id,
127
            'prop' => $prop,
128
            'value' => $value,
129
        ];
130
        $sub = ':value';
131
        if ($value instanceof Expression) {
0 ignored issues
show
Bug introduced by
The class yii\db\Expression does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
132
            $sub = (string)$value;
133
            unset($params['value']);
134
        }
135
        self::getDb()->createCommand("SELECT set_value(:id,:prop,$sub)", $params)->execute();
136
    }
137
138
    public static function find()
139
    {
140
        return new ClientQuery(get_called_class());
141
    }
142
143
    public function setId($value)
144
    {
145
        $this->obj_id = $value;
146
    }
147
148
    public function getId()
149
    {
150
        return $this->obj_id;
151
    }
152
153
    public function getSeller_id()
154
    {
155
        return $this->reseller_id;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function getPasswordHash()
162
    {
163
        return $this->password_hash;
164
    }
165
166
    public function getPassword_hash()
167
    {
168
        return $this->getAuthKey();
169
    }
170
}
171