Completed
Push — master ( 7fa57d...4a5376 )
by Andrii
15:41
created

Client::getId()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * HIAM module for MRDP database compatibility
5
 *
6
 * @link      https://github.com/hiqdev/hiam-mrdp
7
 * @package   hiam-mrdp
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiam\mrdp\storage;
13
14
use Yii;
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 $seller;
30
    public $username;
31
    public $last_name;
32
    public $first_name;
33
34
    public $allowed_ips;
35
    public $totp_secret;
36
37
    public function rules()
38
    {
39
        return [
40
            [['username', 'email', 'password', 'first_name', 'last_name'], 'trim'],
41
            [['allowed_ips', 'totp_secret'], 'trim'],
42
        ];
43
    }
44
45
    public function init()
46
    {
47
        parent::init();
48
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
49
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
50
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
51
    }
52
53
    public function onBeforeInsert()
54
    {
55
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
56
        $this->login = $this->username ?: $this->email;
57
        $this->seller_id = $seller->id;
58
    }
59
60
    public function onAfterSave()
61
    {
62
        $this->id = $this->id ?: $this->obj_id;
63
        $this->type = $this->type ?: 'client';
64
        $contact = Contact::findOne($this->id);
65
        $contact->setAttributes($this->getAttributes());
66
        $contact->save();
67
        $this->saveValue('client,access:totp_secret', $this->totp_secret);
68
        $this->saveValue('client,access:allowed_ips', $this->allowed_ips);
69
        $this->saveValue('login_ips:panel', $this->allowed_ips);
70
    }
71
72
    public function saveValue($prop, $value)
73
    {
74
        self::getDb()->createCommand('SELECT set_value(:id,:prop,:value)', [
75
            'id' => $this->id,
76
            'prop' => $prop,
77
            'value' => $value,
78
        ])->execute();
79
    }
80
81
    public static function find()
82
    {
83
        return new ClientQuery(get_called_class());
84
    }
85
86
    public function setId($value)
87
    {
88
        $this->obj_id = $value;
89
    }
90
91
    public function getId()
92
    {
93
        return $this->obj_id;
94
    }
95
96
    public function getSeller_id()
97
    {
98
        return $this->reseller_id;
99
    }
100
}
101