|
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
|
6 |
|
protected function getNoInitMemberUser() |
|
57
|
|
|
{ |
|
58
|
6 |
|
if (!$this->noInitMemberUser) { |
|
59
|
6 |
|
$class = $this->memberUserClass; |
|
60
|
6 |
|
$this->noInitMemberUser = $class::buildNoInitModel(); |
|
61
|
|
|
} |
|
62
|
6 |
|
return $this->noInitMemberUser; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
16 |
|
public function init() |
|
69
|
|
|
{ |
|
70
|
16 |
|
if (!is_string($this->queryClass)) { |
|
71
|
16 |
|
$this->queryClass = MemberQuery::class; |
|
72
|
|
|
} |
|
73
|
16 |
|
if ($this->skipInit) { |
|
74
|
16 |
|
return; |
|
75
|
|
|
} |
|
76
|
16 |
|
parent::init(); |
|
77
|
16 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
public $descriptionAttribute = 'description'; |
|
80
|
|
|
|
|
81
|
16 |
|
public function getMemberUserRules() |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
16 |
|
[$this->memberAttribute, 'required'], |
|
85
|
16 |
|
[$this->memberAttribute, 'string', 'max' => 36], |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
16 |
|
public function getMemberRoleRules() |
|
90
|
|
|
{ |
|
91
|
|
|
return [ |
|
92
|
16 |
|
['role', 'string', 'max' => 255], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set member user. |
|
98
|
|
|
* @param User|string|integer $user |
|
99
|
|
|
*/ |
|
100
|
16 |
|
public function setMemberUser($user) |
|
101
|
|
|
{ |
|
102
|
16 |
|
$class = $this->memberUserClass; |
|
103
|
16 |
|
if (is_int($user)) { |
|
104
|
|
|
$user = $class::find()->id($user)->one(); |
|
105
|
|
|
} |
|
106
|
16 |
|
if ($user instanceof $class) { |
|
107
|
16 |
|
$user = $user->getGUID(); |
|
108
|
|
|
} |
|
109
|
16 |
|
$this->user_guid = $user; |
|
110
|
16 |
|
} |
|
111
|
|
|
|
|
112
|
6 |
|
public function getMemberUser() |
|
113
|
|
|
{ |
|
114
|
6 |
|
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
|
5 |
|
public function getOrganization() |
|
123
|
|
|
{ |
|
124
|
5 |
|
return $this->getHost(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Set Organization. |
|
129
|
|
|
* @param BaseOrganization $organization |
|
130
|
|
|
* @return boolean |
|
131
|
|
|
*/ |
|
132
|
16 |
|
public function setOrganization($organization) |
|
133
|
|
|
{ |
|
134
|
16 |
|
return $this->setHost($organization); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Assign role. |
|
139
|
|
|
* @param Role $role |
|
140
|
|
|
*/ |
|
141
|
|
|
public function assignRole($role) |
|
142
|
|
|
{ |
|
143
|
|
|
$user = $this->memberUser; |
|
144
|
|
|
if (!$user) { |
|
145
|
|
|
throw new InvalidValueException('Invalid User'); |
|
146
|
|
|
} |
|
147
|
|
|
$assignment = Yii::$app->authManager->assign($role, $user); |
|
148
|
|
|
if (!$assignment) { |
|
149
|
|
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
return $this->setRole($role); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
protected function setRole($role = null) |
|
155
|
|
|
{ |
|
156
|
|
|
if (empty($role)) { |
|
157
|
|
|
$role = ''; |
|
158
|
|
|
} |
|
159
|
|
|
if ($role instanceof Role) { |
|
160
|
|
|
$role = $role->name; |
|
161
|
|
|
} |
|
162
|
|
|
$this->role = $role; |
|
163
|
|
|
return $this->save(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Revoke role. |
|
168
|
|
|
* @param Role $role |
|
169
|
|
|
*/ |
|
170
|
|
|
public function revokeRole($role) |
|
171
|
|
|
{ |
|
172
|
|
|
$user = $this->memberUser; |
|
173
|
|
|
if (!$user) { |
|
174
|
|
|
throw new InvalidValueException('Invalid User'); |
|
175
|
|
|
} |
|
176
|
|
|
$transaction = Yii::$app->db->beginTransaction(); |
|
177
|
|
|
try { |
|
178
|
|
|
$result = Yii::$app->authManager->revoke($role, $user); |
|
179
|
|
|
if (!$result) { |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
$this->setRole($role); |
|
183
|
|
|
$this->save(); |
|
184
|
|
|
$transaction->commit(); |
|
185
|
|
|
} catch (\Exception $ex) { |
|
186
|
|
|
$transaction->rollBack(); |
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
16 |
|
public function rules() |
|
193
|
|
|
{ |
|
194
|
16 |
|
return array_merge($this->getMemberUserRules(), $this->getMemberRoleRules(), parent::rules()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @inheritdoc |
|
199
|
|
|
*/ |
|
200
|
16 |
|
public static function tableName() |
|
201
|
|
|
{ |
|
202
|
16 |
|
return '{{%organization_member}}'; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @inheritdoc |
|
207
|
|
|
*/ |
|
208
|
|
|
public function attributeLabels() |
|
209
|
|
|
{ |
|
210
|
|
|
return [ |
|
211
|
|
|
'guid' => Yii::t('app', 'GUID'), |
|
212
|
|
|
'id' => Yii::t('app', 'ID'), |
|
213
|
|
|
'organization_guid' => Yii::t('app', 'Organization GUID'), |
|
214
|
|
|
'user_guid' => Yii::t('app', 'User GUID'), |
|
215
|
|
|
'nickname' => Yii::t('app', 'Nickname'), |
|
216
|
|
|
'role' => Yii::t('app', 'Role'), |
|
217
|
|
|
'description' => Yii::t('app', 'Description'), |
|
218
|
|
|
'ip' => Yii::t('app', 'IP'), |
|
219
|
|
|
'ip_type' => Yii::t('app', 'IP Address Type'), |
|
220
|
|
|
'created_at' => Yii::t('app', 'Create Time'), |
|
221
|
|
|
'updated_at' => Yii::t('app', 'Update Time'), |
|
222
|
|
|
]; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* |
|
227
|
|
|
* @return boolean |
|
228
|
|
|
*/ |
|
229
|
|
|
public function isAdministrator() |
|
230
|
|
|
{ |
|
231
|
|
|
return ($this->role == (new DepartmentAdmin)->name) || ($this->role == (new OrganizationAdmin)->name); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* |
|
236
|
|
|
* @return boolean |
|
237
|
|
|
*/ |
|
238
|
|
|
public function isCreator() |
|
239
|
|
|
{ |
|
240
|
|
|
return ($this->role == (new DepartmentCreator)->name) || ($this->role == (new OrganizationCreator)->name); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* We think it a `member` if `role` property is empty. |
|
245
|
|
|
* @return boolean |
|
246
|
|
|
*/ |
|
247
|
|
|
public function isMember() |
|
248
|
|
|
{ |
|
249
|
|
|
return empty($this->role); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|