Completed
Push — master ( 2baad1...97e104 )
by Dmitry
02:09
created

Client::getAuth_key()   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 4
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\db\Expression;
15
16
/**
17
 * Client model.
18
 *
19
 * @property integer $obj_id PK
20
 * @property integer $id synced with obj_id
21
 * @property integer $seller_id
22
 * @property string $password
23
 * @property string $email
24
 */
25
class Client extends \yii\db\ActiveRecord
26
{
27
    public $type;
28
    public $state;
29
    public $roles;
30
    public $seller;
31
    public $username;
32
    public $last_name;
33
    public $first_name;
34
35
    public $email_confirmed;
36
    public $allowed_ips;
37
    public $totp_secret;
38
39
    public $password_auth_key;
40
41
    public static function tableName()
42
    {
43
        return '{{zclient}}';
44
    }
45
46
    public static function primaryKey()
47
    {
48
        return ['obj_id'];
49
    }
50
51
    public function rules()
52
    {
53
        return [
54
            [['username', 'email', 'password', 'first_name', 'last_name'], 'trim'],
55
            [['seller_id'], 'integer'],
56
            [['state'], 'trim'],
57
            [['email_confirmed', 'allowed_ips', 'totp_secret'], 'trim'],
58
        ];
59
    }
60
61
    public function init()
62
    {
63
        parent::init();
64
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
65
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onBeforeSave']);
66
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
67
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
68
    }
69
70
    public function onBeforeInsert()
71
    {
72
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
73
        $this->login = $this->username ?: $this->email;
74
        $this->seller_id = $seller->id;
75
        $this->onBeforeSave();
76
    }
77
78
    public function onBeforeSave()
79
    {
80
        if (empty($this->password)) {
81
            unset($this->password);
82
        }
83
        if (!empty($this->state)) {
84
            $this->state_id = new Expression(
85
                "zref_id('state,client,{$this->state}')"
86
            );
87
        }
88
        if ($this->email_confirmed) {
89
            $this->email = $this->email_confirmed;
90
            $this->saveValue('contact:email_new', '');
91
            $this->saveValue('contact:email_confirmed', $this->email_confirmed);
92
            $this->saveValue('contact:email_confirm_date', new Expression('now()::text'));
93
        }
94
    }
95
96
    public function onAfterSave()
97
    {
98
        $this->id = $this->id ?: $this->getAgain()->id;
99
        $this->type = $this->type ?: $this->getAgain()->type;
100
101
        $contact = Contact::findOne($this->id);
102
        $contact->setAttributes($this->getAttributes($contact->safeAttributes()));
103
        $contact->save();
104
        $this->saveValue('client,access:totp_secret', $this->totp_secret);
105
        $this->saveValue('client,access:allowed_ips', $this->allowed_ips);
106
        $this->saveValue('login_ips:panel', $this->allowed_ips);
107
    }
108
109
    protected $_again;
110
111
    public function getAgain()
112
    {
113
        /// XXX this crutch is needed bacause we use `zclient` view (not table)
114
        /// XXX and yii ActiveRecord doesn't populate model properly in this case
115
        if ($this->_again === null) {
116
            $this->_again = static::find()->whereUsername($this->username)->one();
117
        }
118
119
        return $this->_again;
120
    }
121
122
    public function saveValue($prop, $value)
123
    {
124
        self::getDb()->createCommand('SELECT set_value(:id,:prop,:value)', [
125
            'id' => $this->id,
126
            'prop' => $prop,
127
            'value' => $value,
128
        ])->execute();
129
    }
130
131
    public static function find()
132
    {
133
        return new ClientQuery(get_called_class());
134
    }
135
136
    public function setId($value)
137
    {
138
        $this->obj_id = $value;
139
    }
140
141
    public function getId()
142
    {
143
        return $this->obj_id;
144
    }
145
146
    public function getSeller_id()
147
    {
148
        return $this->reseller_id;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getAuthKey()
155
    {
156
        return $this->password_auth_key;
157
    }
158
159
    public function getAuth_key()
160
    {
161
        return $this->getAuthKey();
162
    }
163
}
164