Complex classes like Organization often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Organization, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
85 | class Organization extends User |
||
86 | { |
||
87 | use SelfBlameableTrait; |
||
88 | |||
89 | const TYPE_ORGANIZATION = 1; |
||
90 | const TYPE_DEPARTMENT = 2; |
||
91 | |||
92 | /** |
||
93 | * @var boolean Organization does not need password and corresponding features. |
||
94 | */ |
||
95 | public $passwordHashAttribute = false; |
||
96 | |||
97 | /** |
||
98 | * @var boolean Organization does not need password and corresponding features. |
||
99 | */ |
||
100 | public $passwordResetTokenAttribute = false; |
||
101 | |||
102 | /** |
||
103 | * @var boolean Organization does not need password and corresponding features. |
||
104 | */ |
||
105 | public $passwordHistoryClass = false; |
||
106 | |||
107 | /** |
||
108 | * @var boolean Organization does not need source. |
||
109 | */ |
||
110 | public $sourceAttribute = false; |
||
111 | |||
112 | /** |
||
113 | * @var boolean Organization does not need auth key. |
||
114 | */ |
||
115 | public $authKeyAttribute = false; |
||
116 | |||
117 | /** |
||
118 | * @var boolean Organization does not need access token. |
||
119 | */ |
||
120 | public $accessTokenAttribute = false; |
||
121 | |||
122 | /** |
||
123 | * @var boolean Organization does not need login log. |
||
124 | */ |
||
125 | public $loginLogClass = false; |
||
126 | |||
127 | /** |
||
128 | * @var string The Organization Profile Class |
||
129 | */ |
||
130 | public $profileClass = Profile::class; |
||
131 | |||
132 | /** |
||
133 | * @var string The Member Class. |
||
134 | */ |
||
135 | public $memberClass = Member::class; |
||
136 | |||
137 | /** |
||
138 | * @var string The Subordinate Limit Class |
||
139 | */ |
||
140 | public $subordinateLimitClass = SubordinateLimit::class; |
||
141 | |||
142 | /** |
||
143 | * @var string The Member Limit Class |
||
144 | */ |
||
145 | public $memberLimitClass = MemberLimit::class; |
||
146 | |||
147 | /** |
||
148 | * @var string The Organization Search Class |
||
149 | */ |
||
150 | public $searchClass = OrganizationSearch::class; |
||
151 | |||
152 | /** |
||
153 | * @var string The Organization Setting Class |
||
154 | */ |
||
155 | public $organizationSettingClass = OrganizationSetting::class; |
||
156 | |||
157 | /** |
||
158 | * @var Member |
||
159 | */ |
||
160 | private $noInitMember; |
||
161 | |||
162 | /** |
||
163 | * @var SubordinateLimit |
||
164 | */ |
||
165 | private $noInitSubordinateLimit; |
||
166 | |||
167 | /** |
||
168 | * @var MemberLimit |
||
169 | */ |
||
170 | private $noInitMemberLimit; |
||
171 | |||
172 | /** |
||
173 | * @var OrganizationSetting |
||
174 | */ |
||
175 | private $noInitOrganizationSetting; |
||
176 | |||
177 | /** |
||
178 | * @var User the creator of current Organization or Department. |
||
179 | * This property is only available after registration. |
||
180 | * Please do not access it at other times. |
||
181 | * If you want to get creator model except registration, please |
||
182 | * access [[$creator]] magic-property instead. |
||
183 | */ |
||
184 | public $creatorModel; |
||
185 | |||
186 | /** |
||
187 | * @var array The configuration array of Organization Profile. |
||
188 | * This property is only available after registration. |
||
189 | * Please do not access it at other times. |
||
190 | * If you want to get profile model except registration, please |
||
191 | * access [[$profile]] magic-property instead. |
||
192 | */ |
||
193 | public $profileConfig; |
||
194 | |||
195 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
196 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
197 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
198 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
199 | |||
200 | public $cacheTagPrefix = 'tag_organization_'; |
||
201 | |||
202 | /** |
||
203 | * @return Member |
||
204 | */ |
||
205 | 51 | public function getNoInitMember() |
|
206 | { |
||
207 | 51 | if (!$this->noInitMember) { |
|
208 | 51 | $class = $this->memberClass; |
|
209 | 51 | $this->noInitMember = $class::buildNoInitModel(); |
|
210 | } |
||
211 | 51 | return $this->noInitMember; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return SubordinateLimit |
||
216 | */ |
||
217 | 2 | public function getNoInitSubordinateLimit() |
|
218 | { |
||
219 | 2 | if (!$this->noInitSubordinateLimit) { |
|
220 | 2 | $class = $this->subordinateLimitClass; |
|
221 | 2 | $this->noInitSubordinateLimit = $class::buildNoInitModel(); |
|
222 | } |
||
223 | 2 | return $this->noInitSubordinateLimit; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return MemberLimit |
||
228 | */ |
||
229 | 1 | public function getNoInitMemberLimit() |
|
230 | { |
||
231 | 1 | if (!$this->noInitMemberLimit) { |
|
232 | 1 | $class = $this->memberLimitClass; |
|
233 | 1 | $this->noInitMemberLimit = $class::buildNoInitModel(); |
|
234 | } |
||
235 | 1 | return $this->noInitMemberLimit; |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * @return null|OrganizationSetting |
||
240 | */ |
||
241 | 31 | public function getNoInitOrganizationSetting() |
|
242 | { |
||
243 | 31 | if (!$this->noInitOrganizationSetting) { |
|
244 | 31 | $class = $this->organizationSettingClass; |
|
245 | 31 | if (empty($class)) { |
|
246 | return null; |
||
247 | } |
||
248 | 31 | $this->noInitOrganizationSetting = $class::buildNoInitModel(); |
|
249 | } |
||
250 | 31 | return $this->noInitOrganizationSetting; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * @return null|OrganizationSearch |
||
255 | */ |
||
256 | public function getSearchModel() |
||
257 | { |
||
258 | $class = $this->searchClass; |
||
259 | if (empty($class) || !class_exists($class)) { |
||
260 | return null; |
||
261 | } |
||
262 | return new $class; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 52 | public function init() |
|
269 | { |
||
270 | 52 | $this->parentAttribute = 'parent_guid'; |
|
271 | 52 | if (class_exists($this->memberClass)) { |
|
272 | 52 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
|
273 | } |
||
274 | 52 | if ($this->skipInit) { |
|
275 | 52 | return; |
|
276 | } |
||
277 | 52 | $this->on(static::$eventAfterRegister, [$this, 'onAddProfile'], $this->profileConfig); |
|
278 | 52 | $this->on(static::$eventAfterRegister, [$this, 'onAssignCreator'], $this->creatorModel); |
|
279 | 52 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokeCreator']); |
|
280 | 52 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokeAdministrators']); |
|
281 | 52 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokePermissions']); |
|
282 | 52 | $this->initSelfBlameableEvents(); |
|
283 | 52 | parent::init(); |
|
284 | 52 | } |
|
285 | |||
286 | /** |
||
287 | * @inheritdoc |
||
288 | */ |
||
289 | 1 | public function attributeLabels() |
|
290 | { |
||
291 | return [ |
||
292 | 1 | $this->guidAttribute => Yii::t('user', 'GUID'), |
|
293 | 1 | $this->idAttribute => Yii::t('user', 'ID'), |
|
294 | 1 | $this->ipAttribute => Yii::t('user', 'IP Address'), |
|
295 | 1 | $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'), |
|
296 | 1 | $this->parentAttribute => Yii::t('organization', 'Parent'), |
|
297 | 1 | $this->createdAtAttribute => Yii::t('user', 'Creation Time'), |
|
298 | 1 | $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'), |
|
299 | 1 | $this->statusAttribute => Yii::t('user', 'Status'), |
|
300 | 1 | 'type' => Yii::t('user', 'Type'), |
|
301 | 1 | 'isExcludeOtherMembers' => Yii::t('organization', 'Exclude Other Members'), |
|
302 | 1 | 'isDisallowMemberJoinOther' => Yii::t('organization', 'Disallow Member to Join in Other Organizations'), |
|
303 | 1 | 'isOnlyAcceptCurrentOrgMember' => Yii::t('organization', 'Only Accept Current Organization Members'), |
|
304 | 1 | 'isOnlyAcceptSuperiorOrgMember' => Yii::t('organization', 'Only Accept Superior Organization Members'), |
|
305 | ]; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @inheritdoc |
||
310 | */ |
||
311 | 52 | public static function tableName() |
|
312 | { |
||
313 | 52 | return '{{%organization}}'; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * Find. |
||
318 | * Friendly to IDE. |
||
319 | * @return OrganizationQuery |
||
320 | */ |
||
321 | 52 | public static function find() |
|
322 | { |
||
323 | 52 | return parent::find(); |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * Get rules associated with type attribute. |
||
328 | * @return array |
||
329 | */ |
||
330 | 51 | protected function getTypeRules() |
|
331 | { |
||
332 | return [ |
||
333 | 51 | ['type', 'default', 'value' => static::TYPE_ORGANIZATION], |
|
334 | ['type', 'required'], |
||
335 | 51 | ['type', 'in', 'range' => [static::TYPE_ORGANIZATION, static::TYPE_DEPARTMENT]], |
|
336 | ]; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @inheritdoc |
||
341 | */ |
||
342 | 51 | public function rules() |
|
343 | { |
||
344 | 51 | return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules()); |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * Get Member Query. |
||
349 | * @return MemberQuery |
||
350 | */ |
||
351 | 50 | public function getMembers() |
|
357 | |||
358 | /** |
||
359 | * Get organization member users' query. |
||
360 | * @return BaseUserQuery |
||
361 | */ |
||
362 | 6 | public function getMemberUsers() |
|
363 | { |
||
364 | 6 | $noInit = $this->getNoInitMember(); |
|
365 | 6 | $class = $noInit->memberUserClass; |
|
366 | 6 | $noInitUser = $class::buildNoInitModel(); |
|
367 | 6 | return $this->hasMany($class, [ |
|
368 | 6 | $noInitUser->guidAttribute => $this->getNoInitMember()->memberAttribute |
|
369 | 6 | ])->via('members')->inverseOf('atOrganizations'); |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * Get subordinate limit query. |
||
374 | * @return null|BaseBlameableQuery |
||
375 | */ |
||
376 | 2 | public function getSubordinateLimit() |
|
377 | { |
||
378 | 2 | if (empty($this->subordinateLimitClass)) { |
|
379 | return null; |
||
380 | } |
||
381 | 2 | return $this->hasOne($this->subordinateLimitClass, [ |
|
382 | 2 | $this->getNoInitSubordinateLimit()->createdByAttribute => $this->guidAttribute |
|
383 | ]); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Get member limit query. |
||
388 | * @return null|BaseBlameableQuery |
||
389 | */ |
||
390 | 1 | public function getMemberLimit() |
|
391 | { |
||
392 | 1 | if (empty($this->memberLimitClass)) { |
|
393 | return null; |
||
394 | } |
||
395 | 1 | return $this->hasOne($this->memberLimitClass, [ |
|
396 | 1 | $this->getNoInitMemberLimit()->createdByAttribute => $this->guidAttribute |
|
397 | ]); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @param string|null $item If you want to get all settings, please set it null. |
||
402 | * @return null |
||
403 | */ |
||
404 | 31 | public function getSettings($item = null) |
|
405 | { |
||
406 | 31 | if (empty($this->organizationSettingClass) || !is_string($this->organizationSettingClass)) { |
|
407 | return null; |
||
408 | } |
||
409 | 31 | $query = $this->hasMany($this->organizationSettingClass, [$this->getNoInitOrganizationSetting()->createdByAttribute => $this->guidAttribute]); |
|
410 | 31 | if (!empty($item)) { |
|
411 | 31 | $query = $query->andWhere([$this->getNoInitOrganizationSetting()->idAttribute => $item]); |
|
412 | } |
||
413 | 31 | return $query; |
|
414 | } |
||
415 | |||
416 | /** |
||
417 | * Set organization setting. |
||
418 | * @param string $item |
||
419 | * @param string $value |
||
420 | * @param bool $unique |
||
421 | * @return bool|null Null if organization setting not enabled. |
||
422 | * @throws IntegrityException throw if "item-value" unique broke. |
||
423 | */ |
||
424 | 31 | public function setSetting($item, $value, $unique = false) |
|
425 | { |
||
426 | 31 | if (empty($this->organizationSettingClass) || !is_string($this->organizationSettingClass)) { |
|
427 | return null; |
||
428 | } |
||
429 | 31 | $setting = $this->getSettings($item)->one(); |
|
430 | /* @var $setting OrganizationSetting */ |
||
431 | 31 | if (!$setting) { |
|
432 | 31 | $setting = $this->create($this->organizationSettingClass, [ |
|
433 | 31 | $this->getNoInitOrganizationSetting()->idAttribute => $item, |
|
434 | ]); |
||
435 | } |
||
436 | 31 | $setting->value = $value; |
|
437 | 31 | if ($unique) { |
|
438 | $class = $this->organizationSettingClass; |
||
439 | if ($class::find()->andWhere([ |
||
440 | $this->getNoInitOrganizationSetting()->idAttribute => $item, |
||
441 | $this->getNoInitOrganizationSetting()->contentAttribute => $value |
||
442 | ])->exists()) { |
||
443 | throw new IntegrityException("`$item` : `$value` existed."); |
||
444 | } |
||
445 | } |
||
446 | 31 | return $setting->save(); |
|
447 | } |
||
448 | |||
449 | /** |
||
450 | * Get member with specified user. |
||
451 | * @param User|string|integer $user |
||
452 | * @return Member Null if `user` is not in this organization. |
||
453 | */ |
||
454 | 50 | public function getMember($user) |
|
455 | { |
||
456 | 50 | return $this->getMembers()->user($user)->one(); |
|
|
|||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Add member to organization. |
||
461 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
462 | * If member is created, it will be re-assigned to this parameter. |
||
463 | * @see createMemberModel |
||
464 | * @see createMemberModelWithUser |
||
465 | * @return boolean |
||
466 | * @throws DisallowMemberJoinOtherException |
||
467 | * @throws ExcludeOtherMembersException |
||
468 | * @throws OnlyAcceptCurrentOrgMemberException |
||
469 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
470 | */ |
||
471 | 50 | public function addMember(&$member) |
|
472 | { |
||
473 | 50 | if ($this->getIsNewRecord()) { |
|
474 | return false; |
||
475 | } |
||
476 | 50 | if ($this->hasReachedMemberLimit()) { |
|
477 | 1 | return false; |
|
478 | } |
||
479 | 50 | $user = null; |
|
480 | 50 | if ($member instanceof Member) { |
|
481 | if ($member->getIsNewRecord()) { |
||
482 | return false; |
||
483 | } |
||
484 | $user = $member->memberUser; |
||
485 | } |
||
486 | 50 | if ($member instanceof User) { |
|
487 | 50 | $user = $member; |
|
488 | } |
||
489 | 50 | if (is_string($member) || is_int($member)) { |
|
490 | $class = Yii::$app->user->identityClass; |
||
491 | $user = $class::find()->guidOrId($member)->one(); |
||
492 | } |
||
493 | 50 | if ($this->hasMember($user)) { |
|
494 | return false; |
||
495 | } |
||
496 | 50 | $orgs = $user->getAtOrganizations()->all(); |
|
497 | /* @var $orgs Organization[] */ |
||
498 | 50 | foreach ($orgs as $org) { |
|
499 | 31 | if ($org->topOrganization->isDisallowMemberJoinOther && !$org->topOrganization->equals($this->topOrganization)) { |
|
500 | 1 | throw new DisallowMemberJoinOtherException(Yii::t('organization', "An organization in which the user is located does not allow its members to join other organizations.")); |
|
501 | } |
||
502 | 31 | if ($this->topOrganization->isExcludeOtherMembers && !$org->topOrganization->equals($this->topOrganization)) { |
|
503 | 31 | throw new ExcludeOtherMembersException(Yii::t('organization', "The organization does not allow users who have joined other organizations to join.")); |
|
504 | } |
||
505 | } |
||
506 | 50 | if ($this->isDepartment() && $this->isOnlyAcceptCurrentOrgMember && !$this->topOrganization->hasMember($user)) { |
|
507 | 1 | throw new OnlyAcceptCurrentOrgMemberException(Yii::t('organization' ,'This department is only accepted by members of the organization.')); |
|
508 | } |
||
509 | 50 | if ($this->isDepartment() && !$this->parent->equals($this->topOrganization) && $this->isOnlyAcceptSuperiorOrgMember && !$this->parent->hasMember($user)) { |
|
510 | 1 | throw new OnlyAcceptSuperiorOrgMemberException(Yii::t('organization', 'This department only accepts members of the parent organization or department.')); |
|
511 | } |
||
512 | |||
513 | 50 | $this->trigger(self::EVENT_BEFORE_ADD_MEMBER); |
|
514 | 50 | $model = null; |
|
515 | 50 | if ($member instanceof Member) { |
|
516 | $model = $this->createMemberModel($member); |
||
517 | 50 | } elseif (($member instanceof User) || is_string($member) || is_int($member)) { |
|
518 | 50 | $model = $this->createMemberModelWithUser($member); |
|
519 | } |
||
520 | 50 | $member = $model; |
|
521 | 50 | $result = ($member instanceof Member) ? $member->save() : false; |
|
522 | 50 | $this->trigger(self::EVENT_AFTER_ADD_MEMBER); |
|
523 | 50 | return $result; |
|
524 | } |
||
525 | |||
526 | /** |
||
527 | * Create member model, and set organization with this. |
||
528 | * @param Member $member If this parameter is not new record, it's organization |
||
529 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
530 | * model and create new `Member` model. |
||
531 | * @see createMemberModelWithUser |
||
532 | * @return Member |
||
533 | */ |
||
534 | public function createMemberModel($member) |
||
535 | { |
||
536 | if (!$member->getIsNewRecord()) { |
||
537 | $member->setOrganization($this); |
||
538 | return $member; |
||
539 | } |
||
540 | return $this->createMemberModelWithUser($member->memberUser); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Create member model with user, and set organization with this. |
||
545 | * @param User|string|integer $user |
||
546 | * @return Member |
||
547 | */ |
||
548 | 50 | public function createMemberModelWithUser($user) |
|
549 | { |
||
550 | $config = [ |
||
551 | 50 | 'memberUser' => $user, |
|
552 | 50 | 'organization' => $this, |
|
553 | 50 | 'nickname' => '', |
|
554 | ]; |
||
555 | 50 | $member = $this->createMember($config); |
|
556 | 50 | $member->nickname = $member->memberUser->profile->nickname; |
|
557 | 50 | return $member; |
|
558 | } |
||
559 | |||
560 | /** |
||
561 | * Remove member. |
||
562 | * Note: the creator cannot be removed. |
||
563 | * @param Member|User $member |
||
564 | * @return boolean |
||
565 | */ |
||
566 | 4 | public function removeMember(&$member) |
|
567 | { |
||
568 | 4 | if ($this->getIsNewRecord()) { |
|
569 | return false; |
||
570 | } |
||
571 | 4 | $this->trigger(self::EVENT_BEFORE_REMOVE_MEMBER); |
|
572 | 4 | if ($member instanceof $this->memberClass) { |
|
573 | 4 | $member = $member->{$member->memberAttribute}; |
|
574 | } |
||
575 | 4 | $member = $this->getMember($member); |
|
576 | 4 | if (!$member || $member->isCreator()) { |
|
577 | return false; |
||
578 | } |
||
579 | 4 | $result = $member->delete() > 0; |
|
580 | 4 | $this->trigger(self::EVENT_AFTER_REMOVE_MEMBER); |
|
581 | 4 | return $result; |
|
582 | } |
||
583 | |||
584 | /** |
||
585 | * Remove administrator. |
||
586 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
587 | * @param boolean $keep Keep member after administrator being revoked. |
||
588 | * @return boolean |
||
589 | * @throws IntegrityException |
||
590 | */ |
||
591 | public function removeAdministrator(&$member, $keep = true) |
||
592 | { |
||
593 | if ($this->getIsNewRecord()) { |
||
594 | return false; |
||
595 | } |
||
596 | if ($member instanceof $this->memberClass) { |
||
597 | $member = $member->{$member->memberAttribute}; |
||
598 | } |
||
599 | $member = $this->getMember($member); |
||
600 | if ($member && $member->isAdministrator()) { |
||
601 | if ($keep) { |
||
602 | return $member->revokeAdministrator(); |
||
603 | } |
||
604 | return $this->removeMember($member); |
||
605 | } |
||
606 | return false; |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * |
||
611 | * @param Event $event |
||
612 | * @throws IntegrityException |
||
613 | * @return boolean |
||
614 | */ |
||
615 | 51 | public function onAddProfile($event) |
|
616 | { |
||
617 | 51 | $profile = $event->sender->createProfile($event->data); |
|
618 | 51 | if (!$profile->save()) { |
|
619 | throw new IntegrityException('Profile Save Failed.'); |
||
620 | } |
||
621 | 51 | return true; |
|
622 | } |
||
623 | |||
624 | /** |
||
625 | * |
||
626 | * @param Event $event |
||
627 | */ |
||
628 | 51 | public function onAssignCreator($event) |
|
629 | { |
||
630 | 51 | return $event->sender->addCreator($event->data); |
|
631 | } |
||
632 | |||
633 | /** |
||
634 | * |
||
635 | * @param Event $event |
||
636 | * @return boolean |
||
637 | */ |
||
638 | 20 | public function onRevokeCreator($event) |
|
639 | { |
||
640 | 20 | $sender = $event->sender; |
|
641 | /* @var $sender static */ |
||
642 | 20 | $member = $sender->getMemberCreators()->one(); |
|
643 | /* @var $member Member */ |
||
644 | 20 | $role = $this->isOrganization() ? (new OrganizationCreator)->name : (new DepartmentCreator)->name; |
|
645 | 20 | return $member->revokeRole($role); |
|
646 | } |
||
647 | |||
648 | /** |
||
649 | * |
||
650 | * @param Event $event |
||
651 | * @return boolean |
||
652 | */ |
||
653 | 20 | public function onRevokeAdministrators($event) |
|
654 | { |
||
655 | 20 | $sender = $event->sender; |
|
656 | /* @var $sender static */ |
||
657 | 20 | $members = $sender->getMemberAdministrators()->all(); |
|
658 | /* @var $members Member[] */ |
||
659 | 20 | foreach ($members as $member) |
|
660 | { |
||
661 | 1 | $member->revokeAdministrator(); |
|
662 | } |
||
663 | 20 | return true; |
|
664 | } |
||
665 | |||
666 | /** |
||
667 | * |
||
668 | * @param Event $event |
||
669 | */ |
||
670 | 20 | public function onRevokePermissions($event) |
|
671 | { |
||
672 | |||
673 | 20 | } |
|
674 | |||
675 | /** |
||
676 | * Check whether current instance is an organization. |
||
677 | * @return boolean |
||
678 | */ |
||
679 | 50 | public function isOrganization() |
|
680 | { |
||
681 | 50 | return $this->type == static::TYPE_ORGANIZATION; |
|
682 | } |
||
683 | |||
684 | /** |
||
685 | * Check whether current instance if a department. |
||
686 | * @return boolean |
||
687 | */ |
||
688 | 50 | public function isDepartment() |
|
689 | { |
||
690 | 50 | return $this->type == static::TYPE_DEPARTMENT; |
|
691 | } |
||
692 | |||
693 | /** |
||
694 | * Check whether the current organization has a member. |
||
695 | * @param User|string|integer $user User instance, GUID or ID. |
||
696 | * @return boolean |
||
697 | */ |
||
698 | 50 | public function hasMember($user) |
|
699 | { |
||
700 | 50 | return !empty($this->getMember($user)); |
|
701 | } |
||
702 | |||
703 | /** |
||
704 | * Get member query which role is specified `Creator`. |
||
705 | * @return MemberQuery |
||
706 | */ |
||
707 | 24 | public function getMemberCreators() |
|
708 | { |
||
709 | 24 | return $this->getMembers()->andWhere(['role' => [(new DepartmentCreator)->name, (new OrganizationCreator)->name]]); |
|
710 | } |
||
711 | |||
712 | /** |
||
713 | * Get member query which role is specified `Administrator`. |
||
714 | * @return MemberQuery |
||
715 | */ |
||
716 | 22 | public function getMemberAdministrators() |
|
720 | |||
721 | /** |
||
722 | * Get user query which role is specified `Creator`. |
||
723 | * @return BaseUserQuery |
||
724 | */ |
||
725 | 4 | public function getCreator() |
|
726 | { |
||
727 | 4 | $noInit = $this->getNoInitMember(); |
|
728 | 4 | $class = $noInit->memberUserClass; |
|
729 | 4 | $noInitUser = $class::buildNoInitModel(); |
|
730 | 4 | return $this->hasOne($class, [ |
|
731 | 4 | $noInitUser->guidAttribute => $this->getNoInitMember()->memberAttribute |
|
732 | 4 | ])->via('memberCreators')->inverseOf('creatorsAtOrganizations'); |
|
733 | } |
||
734 | |||
735 | /** |
||
736 | * Get user query which role is specified `Administrator`. |
||
737 | * @return BaseUserQuery |
||
738 | */ |
||
739 | 2 | public function getAdministrators() |
|
740 | { |
||
741 | 2 | $noInit = $this->getNoInitMember(); |
|
742 | 2 | $class = $noInit->memberUserClass; |
|
743 | 2 | $noInitUser = $class::buildNoInitModel(); |
|
744 | 2 | return $this->hasMany($class, [ |
|
745 | 2 | $noInitUser->guidAttribute => $this->getNoInitMember()->memberAttribute |
|
746 | 2 | ])->via('memberAdministrators')->inverseOf('administratorsAtOrganizations'); |
|
747 | } |
||
748 | |||
749 | /** |
||
750 | * |
||
751 | * @param User $user |
||
752 | * @return boolean |
||
753 | * @throws \Exception |
||
754 | * @throws IntegrityException |
||
755 | */ |
||
756 | 51 | protected function addCreator($user) |
|
780 | |||
781 | /** |
||
782 | * Add administrator. |
||
783 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
784 | * @return boolean |
||
785 | * @throws \Exception |
||
786 | * @throws IntegrityException |
||
787 | */ |
||
788 | 17 | public function addAdministrator($user) |
|
805 | |||
806 | /** |
||
807 | * Check whether the current organization has administrator. |
||
808 | * @param User|integer|string $user |
||
809 | * @return boolean |
||
810 | */ |
||
811 | 2 | public function hasAdministrator($user) |
|
819 | |||
820 | /** |
||
821 | * Check whether this organization has reached the upper limit of subordinates. |
||
822 | * @return boolean |
||
823 | */ |
||
824 | 19 | public function hasReachedSubordinateLimit() |
|
832 | |||
833 | /** |
||
834 | * Get the remaining places of subordinates. |
||
835 | * @return bool|int False if no limit |
||
836 | */ |
||
837 | 19 | public function getRemainingSubordinatePlaces() |
|
850 | |||
851 | /** |
||
852 | * Check whether this organization has reached the upper limit of members. |
||
853 | * @return boolean |
||
854 | */ |
||
855 | 50 | public function hasReachedMemberLimit() |
|
863 | |||
864 | /** |
||
865 | * Get the remaining places of members. |
||
866 | * @return bool|int False if no limit. |
||
867 | */ |
||
868 | 50 | public function getRemainingMemberPlaces() |
|
881 | |||
882 | const SETTING_ITEM_EXCLUDE_OTHER_MEMBERS = 'exclude_other_members'; |
||
883 | |||
884 | /** |
||
885 | * @return bool |
||
886 | */ |
||
887 | 31 | public function getIsExcludeOtherMembers() |
|
896 | |||
897 | /** |
||
898 | * @param bool $value |
||
899 | * @return bool |
||
900 | */ |
||
901 | 31 | public function setIsExcludeOtherMembers($value = true) |
|
905 | |||
906 | const SETTING_ITEM_DISALLOW_MEMBER_JOIN_OTHER = 'disallow_member_join_other'; |
||
907 | |||
908 | /** |
||
909 | * @return bool |
||
910 | */ |
||
911 | 31 | public function getIsDisallowMemberJoinOther() |
|
920 | |||
921 | /** |
||
922 | * @param bool $value |
||
923 | * @return bool |
||
924 | */ |
||
925 | 31 | public function setIsDisallowMemberJoinOther($value = true) |
|
929 | |||
930 | const SETTING_ITEM_ONLY_ACCEPT_CURRENT_ORG_MEMBER = 'only_accept_current_org_member'; |
||
931 | |||
932 | /** |
||
933 | * @return bool |
||
934 | */ |
||
935 | 18 | public function getIsOnlyAcceptCurrentOrgMember() |
|
944 | |||
945 | /** |
||
946 | * @param bool $value |
||
947 | * @return bool |
||
948 | */ |
||
949 | 18 | public function setIsOnlyAcceptCurrentOrgMember($value = true) |
|
953 | |||
954 | const SETTING_ITEM_ONLY_ACCEPT_SUPERIOR_ORG_MEMBER = 'only_accept_superior_org_member'; |
||
955 | |||
956 | /** |
||
957 | * @return bool |
||
958 | */ |
||
959 | 10 | public function getIsOnlyAcceptSuperiorOrgMember() |
|
971 | |||
972 | /** |
||
973 | * @param bool $value |
||
974 | * @return bool |
||
975 | */ |
||
976 | 10 | public function setIsOnlyAcceptSuperiorOrgMember($value = true) |
|
983 | |||
984 | const SETTING_ITEM_JOIN_PASSWORD = 'join_password'; |
||
985 | |||
986 | /** |
||
987 | * Get join password. |
||
988 | * @return mixed |
||
989 | */ |
||
990 | public function getJoinPassword() |
||
999 | |||
1000 | /** |
||
1001 | * Set join password. |
||
1002 | * @param string $value |
||
1003 | * @return bool|null |
||
1004 | */ |
||
1005 | public function setJoinPassword($value = '') |
||
1009 | |||
1010 | const SETTING_ITEM_JOIN_IP_ADDRESS = 'join_ip_address'; |
||
1011 | |||
1012 | /** |
||
1013 | * Get Join IP address |
||
1014 | * @return mixed |
||
1015 | */ |
||
1016 | public function getJoinIpAddress() |
||
1025 | |||
1026 | /** |
||
1027 | * Set join IP address. |
||
1028 | * @param $value |
||
1029 | * @return bool|null |
||
1030 | */ |
||
1031 | public function setJoinIpAddress($value = '') |
||
1035 | |||
1036 | const SETTING_ITEM_JOIN_ENTRANCE_URL = 'join_entrance_url'; |
||
1037 | |||
1038 | /** |
||
1039 | * Get join entrance URL. |
||
1040 | * This setting should be confirmed unique. |
||
1041 | * @return string |
||
1042 | */ |
||
1043 | public function getJoinEntranceUrl() |
||
1052 | |||
1053 | /** |
||
1054 | * Set join entrance URL. |
||
1055 | * @param string $value |
||
1056 | * @return bool|null |
||
1057 | */ |
||
1058 | public function setJoinEntranceUrl($value = '') |
||
1062 | |||
1063 | /** |
||
1064 | * @return $this|null|static |
||
1065 | */ |
||
1066 | 31 | public function getTopOrganization() |
|
1074 | |||
1075 | /** |
||
1076 | * Check whether the subordinates have the [[$user]] |
||
1077 | * Note, this operation may consume the quantity of database selection. |
||
1078 | * @param User $user |
||
1079 | * @return bool |
||
1080 | */ |
||
1081 | 2 | public function hasMemberInSubordinates($user) |
|
1096 | } |
||
1097 |