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\Exception; |
15
|
|
|
use yii\db\Expression; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Client model. |
20
|
|
|
* |
21
|
|
|
* @property integer $obj_id PK |
22
|
|
|
* @property integer $id synced with obj_id |
23
|
|
|
* @property integer $seller_id |
24
|
|
|
* @property string $password |
25
|
|
|
* @property string $email |
26
|
|
|
*/ |
27
|
|
|
class Client extends \yii\db\ActiveRecord |
28
|
|
|
{ |
29
|
|
|
public $type; |
30
|
|
|
public $state; |
31
|
|
|
public $roles; |
32
|
|
|
public $seller; |
33
|
|
|
public $username; |
34
|
|
|
public $last_name; |
35
|
|
|
public $first_name; |
36
|
|
|
public $send_me_news; |
37
|
|
|
|
38
|
|
|
public $email_confirmed; |
39
|
|
|
public $email_new; |
40
|
|
|
public $allowed_ips; |
41
|
|
|
public $totp_secret; |
42
|
|
|
public $referralParams; |
43
|
|
|
|
44
|
|
|
public $password_hash; |
45
|
|
|
|
46
|
|
|
public static function tableName() |
47
|
|
|
{ |
48
|
|
|
return '{{zclient}}'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function primaryKey() |
52
|
|
|
{ |
53
|
|
|
return ['obj_id']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function rules() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
[['username', 'email', 'password', 'first_name', 'last_name', 'email_new'], 'trim'], |
60
|
|
|
[['username', 'email'], 'filter', 'filter' => 'strtolower'], |
61
|
|
|
[['seller_id'], 'integer'], |
62
|
|
|
[['state'], 'trim'], |
63
|
|
|
[['email_confirmed', 'allowed_ips', 'totp_secret'], 'trim'], |
64
|
|
|
['send_me_news', 'boolean'], |
65
|
|
|
['referralParams', 'safe'] |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function init() |
70
|
|
|
{ |
71
|
|
|
parent::init(); |
72
|
|
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']); |
73
|
|
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onBeforeSave']); |
74
|
|
|
$this->on(static::EVENT_AFTER_INSERT, [$this, 'onAfterInsert']); |
75
|
|
|
$this->on(static::EVENT_AFTER_UPDATE, [$this, 'onAfterSave']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function onBeforeInsert() |
79
|
|
|
{ |
80
|
|
|
$seller = static::findOne(['username' => Yii::$app->params['user.seller']]); |
81
|
|
|
$this->login = $this->username ?: $this->email; |
82
|
|
|
$this->seller_id = $seller->id; |
83
|
|
|
$this->onBeforeSave(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function onBeforeSave() |
87
|
|
|
{ |
88
|
|
|
if (empty($this->password)) { |
89
|
|
|
unset($this->password); |
90
|
|
|
} |
91
|
|
|
if (!empty($this->state)) { |
92
|
|
|
$this->state_id = new Expression("zref_id('state,client,{$this->state}')"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($this->isEmailConfirmAction()) { |
96
|
|
|
$double = static::findOne(['email' => $this->email_confirmed]); |
97
|
|
|
if (empty($double) || $this->obj_id === $double->obj_id) { |
98
|
|
|
$this->email = $this->email_confirmed; |
99
|
|
|
} |
100
|
|
|
$this->saveValue('contact:email_new', ''); |
101
|
|
|
$this->saveValue('contact:email_confirmed', $this->email_confirmed); |
102
|
|
|
$this->saveValue('contact:email_confirm_date', new Expression("date_trunc('second', now()::timestamp)::text")); |
103
|
|
|
} |
104
|
|
|
if (!empty($this->email_new)) { |
105
|
|
|
$this->saveValue('contact:email_new', $this->email_new); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function onAfterInsert() |
110
|
|
|
{ |
111
|
|
|
$this->onAfterSave(); |
112
|
|
|
$this->saveReferralParams(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function isEmailConfirmAction(): bool |
116
|
|
|
{ |
117
|
|
|
$currentConfirmedEmail = $this->readValue('contact:email_confirmed'); |
118
|
|
|
|
119
|
|
|
return !empty($this->email_confirmed) && ($currentConfirmedEmail !== $this->email_confirmed); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function onAfterSave() |
123
|
|
|
{ |
124
|
|
|
$this->id = $this->id ?: $this->getAgain()->id; |
125
|
|
|
$this->type = $this->type ?: $this->getAgain()->type; |
126
|
|
|
$send_news = $this->send_me_news === '0' ? '' : 1; |
127
|
|
|
|
128
|
|
|
$contact = Contact::findOne($this->id); |
129
|
|
|
$contact->setAttributes($this->getAttributes($contact->safeAttributes())); |
130
|
|
|
$contact->save(); |
131
|
|
|
$this->saveValue('client,access:totp_secret', $this->totp_secret); |
132
|
|
|
$this->saveValue('client,access:allowed_ips', $this->allowed_ips); |
133
|
|
|
$this->saveValue('login_ips:panel', $this->allowed_ips); |
134
|
|
|
|
135
|
|
|
$this->saveValue('contact:policy_consent', 1); |
136
|
|
|
$this->saveValue('contact:gdpr_consent', 1); |
137
|
|
|
$this->saveValue('client,mailing:commercial', $send_news); |
138
|
|
|
$this->saveValue('client,mailing:newsletters', $send_news); |
139
|
|
|
|
140
|
|
|
$this->saveReferralParams(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function saveReferralParams(): void |
144
|
|
|
{ |
145
|
|
|
if (!empty($this->referralParams['referer'])) { |
146
|
|
|
$this->saveValue("client,registration:referer", $this->referralParams['referer']); |
147
|
|
|
} |
148
|
|
|
if (!empty($this->referralParams['utmTags'])) { |
149
|
|
|
$this->saveValue("client,registration:utm_tags", Json::encode($this->referralParams['utmTags'])); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected $_again; |
154
|
|
|
|
155
|
|
|
public function getAgain() |
156
|
|
|
{ |
157
|
|
|
/// XXX this crutch is needed bacause we use `zclient` view (not table) |
158
|
|
|
/// XXX and yii ActiveRecord doesn't populate model properly in this case |
159
|
|
|
if ($this->_again === null) { |
160
|
|
|
$this->_again = static::find()->whereUsername($this->username)->one(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->_again; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function readValue(string $prop): ?string |
167
|
|
|
{ |
168
|
|
|
$params = [ |
169
|
|
|
'id' => $this->id, |
170
|
|
|
'prop' => $prop, |
171
|
|
|
]; |
172
|
|
|
return self::getDb()->createCommand("SELECT get_value(:id,:prop)", $params)->queryScalar(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function saveValue($prop, $value) |
176
|
|
|
{ |
177
|
|
|
$params = [ |
178
|
|
|
'id' => $this->id, |
179
|
|
|
'prop' => $prop, |
180
|
|
|
'value' => $value, |
181
|
|
|
]; |
182
|
|
|
$sub = ':value'; |
183
|
|
|
if ($value instanceof Expression) { |
|
|
|
|
184
|
|
|
$sub = (string)$value; |
185
|
|
|
unset($params['value']); |
186
|
|
|
} |
187
|
|
|
self::getDb()->createCommand("SELECT set_value(:id,:prop,$sub)", $params)->execute(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public static function find() |
191
|
|
|
{ |
192
|
|
|
return new ClientQuery(get_called_class()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function setId($value) |
196
|
|
|
{ |
197
|
|
|
$this->obj_id = $value; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getId() |
201
|
|
|
{ |
202
|
|
|
return $this->obj_id; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getSeller_id() |
206
|
|
|
{ |
207
|
|
|
return $this->reseller_id; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
|
|
public function getPasswordHash() |
214
|
|
|
{ |
215
|
|
|
return $this->password_hash; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function getPassword_hash() |
219
|
|
|
{ |
220
|
|
|
return $this->getAuthKey(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $email |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
public function updateEmail(string $email): bool |
228
|
|
|
{ |
229
|
|
|
if ($this->username) { |
230
|
|
|
try { |
231
|
|
|
if (Yii::$app->db->createCommand() |
232
|
|
|
->update('zclient', ['email' => $email], 'login = :login') |
233
|
|
|
->bindValue(':login', $this->username) |
234
|
|
|
->execute()) { |
235
|
|
|
return true; |
236
|
|
|
} |
237
|
|
|
} catch (Exception $e) { |
|
|
|
|
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
protected static function filterCondition(array $condition, array $aliases = []) |
245
|
|
|
{ |
246
|
|
|
/// XXX skip condition filtering |
247
|
|
|
return $condition; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
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 thecomposer.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
orrequire-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 you have not tested against this specific condition, such errors might go unnoticed.