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\organization; |
14
|
|
|
|
15
|
|
|
use rhosocial\base\models\models\BaseBlameableModel; |
16
|
|
|
use rhosocial\user\rbac\Role; |
17
|
|
|
use rhosocial\user\User; |
18
|
|
|
use rhosocial\organization\rbac\roles\DepartmentAdmin; |
19
|
|
|
use rhosocial\organization\rbac\roles\DepartmentCreator; |
20
|
|
|
use rhosocial\organization\rbac\roles\OrganizationAdmin; |
21
|
|
|
use rhosocial\organization\rbac\roles\OrganizationCreator; |
22
|
|
|
use rhosocial\organization\queries\OrganizationQuery; |
23
|
|
|
use rhosocial\organization\queries\MemberQuery; |
24
|
|
|
use Yii; |
25
|
|
|
use yii\base\InvalidValueException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Organization member. |
29
|
|
|
* |
30
|
|
|
* @property string $organization_guid |
31
|
|
|
* @property string $user_guid store guid of user who represents this member. |
32
|
|
|
* @property string $nickname |
33
|
|
|
* @property string $role |
34
|
|
|
* @property string $description |
35
|
|
|
* |
36
|
|
|
* @property string $department_guid |
37
|
|
|
* @property string $member_guid |
38
|
|
|
* @property User $memberUser |
39
|
|
|
* |
40
|
|
|
* @version 1.0 |
41
|
|
|
* @author vistart <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
class Member extends BaseBlameableModel |
44
|
|
|
{ |
45
|
|
|
public $createdByAttribute = 'organization_guid'; |
46
|
|
|
public $updatedByAttribute = false; |
47
|
|
|
public $hostClass = Organization::class; |
48
|
|
|
|
49
|
|
|
public $memberAttribute = 'user_guid'; |
50
|
|
|
public $memberUserClass = User::class; |
51
|
|
|
public $contentAttribute = 'nickname'; |
52
|
|
|
private $noInitMemberUser; |
53
|
|
|
/** |
54
|
|
|
* @return User |
55
|
|
|
*/ |
56
|
23 |
|
protected function getNoInitMemberUser() |
57
|
|
|
{ |
58
|
23 |
|
if (!$this->noInitMemberUser) { |
59
|
23 |
|
$class = $this->memberUserClass; |
60
|
23 |
|
$this->noInitMemberUser = $class::buildNoInitModel(); |
61
|
|
|
} |
62
|
23 |
|
return $this->noInitMemberUser; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
23 |
|
public function init() |
69
|
|
|
{ |
70
|
23 |
|
if (!is_string($this->queryClass)) { |
71
|
23 |
|
$this->queryClass = MemberQuery::class; |
72
|
|
|
} |
73
|
23 |
|
if ($this->skipInit) { |
74
|
23 |
|
return; |
75
|
|
|
} |
76
|
23 |
|
parent::init(); |
77
|
23 |
|
} |
78
|
|
|
|
79
|
|
|
public $descriptionAttribute = 'description'; |
80
|
|
|
|
81
|
23 |
|
public function getMemberUserRules() |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
23 |
|
[$this->memberAttribute, 'required'], |
85
|
23 |
|
[$this->memberAttribute, 'string', 'max' => 36], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
23 |
|
public function getMemberRoleRules() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
23 |
|
['role', 'string', 'max' => 255], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set member user. |
98
|
|
|
* @param User|string|integer $user |
99
|
|
|
*/ |
100
|
23 |
|
public function setMemberUser($user) |
101
|
|
|
{ |
102
|
23 |
|
$class = $this->memberUserClass; |
103
|
23 |
|
if (is_int($user)) { |
104
|
|
|
$user = $class::find()->id($user)->one(); |
105
|
|
|
} |
106
|
23 |
|
if ($user instanceof $class) { |
107
|
23 |
|
$user = $user->getGUID(); |
108
|
|
|
} |
109
|
23 |
|
$this->user_guid = $user; |
110
|
23 |
|
} |
111
|
|
|
|
112
|
23 |
|
public function getMemberUser() |
113
|
|
|
{ |
114
|
23 |
|
return $this->hasOne($this->memberUserClass, [$this->getNoInitMemberUser()->guidAttribute => $this->memberAttribute]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get Organization Query. |
119
|
|
|
* Alias of `getHost` method. |
120
|
|
|
* @return OrganizationQuery |
121
|
|
|
*/ |
122
|
16 |
|
public function getOrganization() |
123
|
|
|
{ |
124
|
16 |
|
return $this->getHost(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set Organization. |
129
|
|
|
* @param BaseOrganization $organization |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
23 |
|
public function setOrganization($organization) |
133
|
|
|
{ |
134
|
23 |
|
return $this->setHost($organization); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Assign role. |
139
|
|
|
* The setting role operation will not take effect immediately. You should |
140
|
|
|
* wrap this method and the subsequent save operations together into a |
141
|
|
|
* transaction, in order to ensure data cosistency. |
142
|
|
|
* @param Role $role |
143
|
|
|
*/ |
144
|
23 |
|
public function assignRole($role) |
145
|
|
|
{ |
146
|
23 |
|
$user = $this->memberUser; |
147
|
23 |
|
if (!$user) { |
148
|
|
|
throw new InvalidValueException('Invalid User'); |
149
|
|
|
} |
150
|
23 |
|
$assignment = Yii::$app->authManager->getAssignment($role->name, $user); |
151
|
23 |
|
if (!$assignment) { |
152
|
23 |
|
$assignment = Yii::$app->authManager->assign($role, $user->getGUID()); |
|
|
|
|
153
|
|
|
} |
154
|
23 |
|
return $this->setRole($role); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set role. |
159
|
|
|
* @param Role $role |
160
|
|
|
* @return boolean |
161
|
|
|
*/ |
162
|
23 |
|
public function setRole($role = null) |
163
|
|
|
{ |
164
|
23 |
|
if (empty($role)) { |
165
|
13 |
|
$role = ''; |
166
|
|
|
} |
167
|
23 |
|
if ($role instanceof Role) { |
168
|
23 |
|
$role = $role->name; |
169
|
|
|
} |
170
|
23 |
|
$this->role = $role; |
171
|
23 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Revoke role. |
175
|
|
|
* @param Role $role |
176
|
|
|
*/ |
177
|
13 |
|
public function revokeRole($role) |
178
|
|
|
{ |
179
|
13 |
|
$user = $this->memberUser; |
180
|
13 |
|
if (!$user) { |
181
|
|
|
throw new InvalidValueException('Invalid User'); |
182
|
|
|
} |
183
|
13 |
|
$transaction = Yii::$app->db->beginTransaction(); |
184
|
|
|
try { |
185
|
13 |
|
$assignment = Yii::$app->authManager->getAssignment($role->name, $user); |
186
|
13 |
|
if ($assignment) { |
187
|
13 |
|
$count = (int)($user->getOfMembers()->role($role->name)->count()); |
188
|
13 |
|
if ($count == 1) { |
189
|
13 |
|
Yii::$app->authManager->revoke($role, $user); |
190
|
|
|
} |
191
|
|
|
} |
192
|
13 |
|
$this->setRole(); |
193
|
13 |
|
if (!$this->save()) { |
194
|
|
|
throw new \yii\db\IntegrityException('Save failed.'); |
195
|
|
|
} |
196
|
13 |
|
$transaction->commit(); |
197
|
|
|
} catch (\Exception $ex) { |
198
|
|
|
$transaction->rollBack(); |
199
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
200
|
|
|
throw $ex; |
201
|
|
|
} |
202
|
13 |
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
23 |
|
public function rules() |
206
|
|
|
{ |
207
|
23 |
|
return array_merge($this->getMemberUserRules(), $this->getMemberRoleRules(), parent::rules()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritdoc |
212
|
|
|
*/ |
213
|
23 |
|
public static function tableName() |
214
|
|
|
{ |
215
|
23 |
|
return '{{%organization_member}}'; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritdoc |
220
|
|
|
*/ |
221
|
|
|
public function attributeLabels() |
222
|
|
|
{ |
223
|
|
|
return [ |
224
|
|
|
'guid' => Yii::t('app', 'GUID'), |
225
|
|
|
'id' => Yii::t('app', 'ID'), |
226
|
|
|
'organization_guid' => Yii::t('app', 'Organization GUID'), |
227
|
|
|
'user_guid' => Yii::t('app', 'User GUID'), |
228
|
|
|
'nickname' => Yii::t('app', 'Nickname'), |
229
|
|
|
'role' => Yii::t('app', 'Role'), |
230
|
|
|
'description' => Yii::t('app', 'Description'), |
231
|
|
|
'ip' => Yii::t('app', 'IP'), |
232
|
|
|
'ip_type' => Yii::t('app', 'IP Address Type'), |
233
|
|
|
'created_at' => Yii::t('app', 'Create Time'), |
234
|
|
|
'updated_at' => Yii::t('app', 'Update Time'), |
235
|
|
|
]; |
236
|
|
|
} |
237
|
|
|
|
238
|
2 |
|
public function isDepartmentAdministrator() |
239
|
|
|
{ |
240
|
2 |
|
return $this->role == (new DepartmentAdmin())->name; |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
public function isDepartmentCreator() |
244
|
|
|
{ |
245
|
1 |
|
return $this->role == (new DepartmentCreator())->name; |
246
|
|
|
} |
247
|
|
|
|
248
|
2 |
|
public function isOrganizationAdministrator() |
249
|
|
|
{ |
250
|
2 |
|
return $this->role == (new OrganizationAdmin())->name; |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
public function isOrganizationCreator() |
254
|
|
|
{ |
255
|
1 |
|
return $this->role == (new OrganizationCreator())->name; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* |
260
|
|
|
* @return boolean |
261
|
|
|
*/ |
262
|
2 |
|
public function isAdministrator() |
263
|
|
|
{ |
264
|
2 |
|
return $this->isDepartmentAdministrator() || $this->isOrganizationAdministrator(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* |
269
|
|
|
* @return boolean |
270
|
|
|
*/ |
271
|
1 |
|
public function isCreator() |
272
|
|
|
{ |
273
|
1 |
|
return $this->isDepartmentCreator() || $this->isOrganizationCreator(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* We think it a `member` if `role` property is empty. |
278
|
|
|
* @return boolean |
279
|
|
|
*/ |
280
|
|
|
public function isMember() |
281
|
|
|
{ |
282
|
|
|
return empty($this->role); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
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.