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