1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\base\models\models; |
14
|
|
|
|
15
|
|
|
use MongoDB\BSON\Binary; |
16
|
|
|
use rhosocial\base\helpers\Number; |
17
|
|
|
use rhosocial\base\models\models\BaseUserModel; |
18
|
|
|
use rhosocial\base\models\queries\BaseMongoBlameableQuery; |
19
|
|
|
use rhosocial\base\models\traits\BlameableTrait; |
20
|
|
|
use yii\web\IdentityInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Description of BaseMongoBlameableModel |
24
|
|
|
* |
25
|
|
|
* @version 1.0 |
26
|
|
|
* @author vistart <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class BaseMongoBlameableModel extends BaseMongoEntityModel |
29
|
|
|
{ |
30
|
|
|
use BlameableTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initialize the blameable model. |
34
|
|
|
* If query class is not specified, [[BaseMongoBlameableQuery]] will be taken. |
35
|
|
|
*/ |
36
|
12 |
|
public function init() |
37
|
|
|
{ |
38
|
12 |
|
if (!is_string($this->queryClass) || empty($this->queryClass)) { |
39
|
12 |
|
$this->queryClass = BaseMongoBlameableQuery::class; |
40
|
|
|
} |
41
|
12 |
|
if ($this->skipInit) { |
42
|
12 |
|
return; |
43
|
|
|
} |
44
|
12 |
|
$this->initBlameableEvents(); |
45
|
12 |
|
parent::init(); |
46
|
12 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the query class with specified identity. |
50
|
|
|
* @param BaseUserModel $identity |
51
|
|
|
* @return BaseMongoBlameableQuery |
52
|
|
|
*/ |
53
|
3 |
|
public static function findByIdentity($identity = null) |
54
|
|
|
{ |
55
|
3 |
|
return static::find()->byIdentity($identity); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Because every document has a `MongoId" class, this class no longer needs GUID feature. |
60
|
|
|
* @var boolean determines whether enable the GUID features. |
61
|
|
|
*/ |
62
|
|
|
public $guidAttribute = false; |
63
|
|
|
public $idAttribute = '_id'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
* You can override this method if enabled fields cannot meet your requirements. |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
12 |
|
public function attributes() |
71
|
|
|
{ |
72
|
12 |
|
return $this->enabledFields(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get blame who owned this blameable model. |
77
|
|
|
* NOTICE! This method will not check whether `$userClass` exists. You should |
78
|
|
|
* specify it in `init()` method. |
79
|
|
|
* @return BaseUserQuery user. |
80
|
|
|
*/ |
81
|
|
|
public function getHost() |
82
|
|
|
{ |
83
|
|
|
$hostClass = $this->hostClass; |
84
|
|
|
$user = $hostClass::buildNoInitModel(); |
|
|
|
|
85
|
|
|
/* @var BaseUserModel $user */ |
86
|
|
|
return $this->hasOne($hostClass::className(), ['refGUID' => 'createdByAttribute']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getCreatedByAttribute() |
90
|
|
|
{ |
91
|
|
|
$createdByAttribute = $this->createdByAttribute; |
92
|
|
|
return (!is_string($createdByAttribute) || empty($createdByAttribute)) ? null : $this->$createdByAttribute->getData(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* @param IdentityInterface $host |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
12 |
|
public function setHost($host) |
101
|
|
|
{ |
102
|
12 |
|
if ($host instanceof $this->hostClass || $host instanceof IdentityInterface) { |
103
|
12 |
|
return $this->{$this->createdByAttribute} = new Binary($host->getGUID(), Binary::TYPE_UUID); |
104
|
|
|
} |
105
|
|
|
if (is_string($host) && preg_match(Number::GUID_REGEX, $host)) { |
106
|
|
|
return $this->{$this->createdByAttribute} = new Binary(Number::guid_bin($host), Binary::TYPE_UUID); |
107
|
|
|
} |
108
|
|
|
if (strlen($host) == 16) { |
109
|
|
|
return $this->{$this->createdByAttribute} = new Binary($host, Binary::TYPE_UUID); |
110
|
|
|
} |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get updater who updated this blameable model recently. |
116
|
|
|
* NOTICE! This method will not check whether `$userClass` exists. You should |
117
|
|
|
* specify it in `init()` method. |
118
|
|
|
* @return BaseUserQuery user. |
119
|
|
|
*/ |
120
|
|
|
public function getUpdater() |
121
|
|
|
{ |
122
|
|
|
if (!is_string($this->updatedByAttribute) || empty($this->updatedByAttribute)) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
$hostClass = $this->hostClass; |
126
|
|
|
$host = $hostClass::buildNoInitModel(); |
|
|
|
|
127
|
|
|
/* @var $user BaseUserModel */ |
128
|
|
|
return $this->hasOne($hostClass::className(), ['refGUID' => 'updatedByAttribute']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getUpdatedByAttribute() |
132
|
|
|
{ |
133
|
|
|
$updatedByAttribute = $this->updatedByAttribute; |
134
|
|
|
return (!is_string($updatedByAttribute) || empty($updatedByAttribute)) ? null : $this->$updatedByAttribute->getData(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @param IdentityInterface $user |
|
|
|
|
140
|
|
|
* @return boolean |
141
|
|
|
*/ |
142
|
|
|
public function setUpdater($updater) |
143
|
|
|
{ |
144
|
|
|
if (!is_string($this->updatedByAttribute) || empty($this->updatedByAttribute)) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
if ($updater instanceof $this->hostClass || $updater instanceof IdentityInterface) { |
148
|
|
|
return $this->{$this->updatedByAttribute} = new Binary($updater->getGUID(), Binary::TYPE_UUID); |
149
|
|
|
} |
150
|
|
|
if (is_string($updater) && preg_match(Number::GUID_REGEX, $updater)) { |
151
|
|
|
return $this->{$this->updatedByAttribute} = new Binary(Number::guid_bin($updater), Binary::TYPE_UUID); |
152
|
|
|
} |
153
|
|
|
if (strlen($updater) == 16) { |
154
|
|
|
return $this->{$this->updatedByAttribute} = new Binary($updater, Binary::TYPE_UUID); |
155
|
|
|
} |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return the current user's GUID if current model doesn't specify the owner |
161
|
|
|
* yet, or return the owner's GUID if current model has been specified. |
162
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
163
|
|
|
* override or modify it directly, unless you know the consequences. |
164
|
|
|
* @param ModelEvent $event |
165
|
|
|
* @return string the GUID of current user or the owner. |
166
|
|
|
*/ |
167
|
12 |
|
public function onGetCurrentUserGuid($event) |
168
|
|
|
{ |
169
|
12 |
|
$sender = $event->sender; |
170
|
|
|
/* @var $sender static */ |
171
|
12 |
|
if (isset($sender->attributes[$sender->createdByAttribute])) { |
172
|
12 |
|
return $sender->attributes[$sender->createdByAttribute]; |
173
|
|
|
} |
174
|
|
|
$identity = \Yii::$app->user->identity; |
175
|
|
|
/* @var BaseUserModel $identity */ |
176
|
|
|
if ($identity) { |
177
|
|
|
return $identity->getReadableGUID(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.