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 |
||
| 66 | class Organization extends User |
||
| 67 | { |
||
| 68 | use SelfBlameableTrait; |
||
| 69 | |||
| 70 | const TYPE_ORGANIZATION = 1; |
||
| 71 | const TYPE_DEPARTMENT = 2; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var boolean Organization does not need password and corresponding features. |
||
| 75 | */ |
||
| 76 | public $passwordHashAttribute = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var boolean Organization does not need password and corresponding features. |
||
| 80 | */ |
||
| 81 | public $passwordResetTokenAttribute = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var boolean Organization does not need password and corresponding features. |
||
| 85 | */ |
||
| 86 | public $passwordHistoryClass = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var boolean Organization does not need source. |
||
| 90 | */ |
||
| 91 | public $sourceAttribute = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var boolean Organization does not need auth key. |
||
| 95 | */ |
||
| 96 | public $authKeyAttribute = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var boolean Organization does not need access token. |
||
| 100 | */ |
||
| 101 | public $accessTokenAttribute = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * |
||
| 105 | * @var boolean Organization does not need login log. |
||
| 106 | */ |
||
| 107 | public $loginLogClass = false; |
||
| 108 | |||
| 109 | public $profileClass = Profile::class; |
||
| 110 | |||
| 111 | public $memberClass = Member::class; |
||
| 112 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 113 | public $memberLimitClass = MemberLimit::class; |
||
| 114 | public $searchClass = OrganizationSearch::class; |
||
| 115 | /** |
||
| 116 | * @var Member |
||
| 117 | */ |
||
| 118 | private $noInitMember; |
||
| 119 | /** |
||
| 120 | * @var SubordinateLimit |
||
| 121 | */ |
||
| 122 | private $noInitSubordinateLimit; |
||
| 123 | /** |
||
| 124 | * @var MemberLimit |
||
| 125 | */ |
||
| 126 | private $noInitMemberLimit; |
||
| 127 | public $creatorModel; |
||
| 128 | public $profileConfig; |
||
| 129 | |||
| 130 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 131 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 132 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 133 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return Member |
||
| 137 | */ |
||
| 138 | 51 | public function getNoInitMember() |
|
| 139 | { |
||
| 140 | 51 | if (!$this->noInitMember) { |
|
| 141 | 51 | $class = $this->memberClass; |
|
| 142 | 51 | $this->noInitMember = $class::buildNoInitModel(); |
|
| 143 | 51 | } |
|
| 144 | 51 | return $this->noInitMember; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return SubordinateLimit |
||
| 149 | */ |
||
| 150 | 2 | public function getNoInitSubordinateLimit() |
|
| 151 | { |
||
| 152 | 2 | if (!$this->noInitSubordinateLimit) { |
|
| 153 | 2 | $class = $this->subordinateLimitClass; |
|
| 154 | 2 | $this->noInitSubordinateLimit = $class::buildNoInitModel(); |
|
| 155 | 2 | } |
|
| 156 | 2 | return $this->noInitSubordinateLimit; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return MemberLimit |
||
| 161 | */ |
||
| 162 | 1 | public function getNoInitMemberLimit() |
|
| 163 | { |
||
| 164 | 1 | if (!$this->noInitMemberLimit) { |
|
| 165 | 1 | $class = $this->memberLimitClass; |
|
| 166 | 1 | $this->noInitMemberLimit = $class::buildNoInitModel(); |
|
| 167 | 1 | } |
|
| 168 | 1 | return $this->noInitMemberLimit; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return null|OrganizationSearch |
||
| 173 | */ |
||
| 174 | 50 | public function getSearchModel() |
|
| 175 | { |
||
| 176 | $class = $this->searchClass; |
||
| 177 | if (empty($class) || !class_exists($class)) { |
||
| 178 | return null; |
||
| 179 | } |
||
| 180 | return new $class; |
||
| 181 | 50 | } |
|
| 182 | |||
| 183 | 52 | public function init() |
|
| 184 | { |
||
| 185 | 52 | $this->parentAttribute = 'parent_guid'; |
|
| 186 | 52 | if (class_exists($this->memberClass)) { |
|
| 187 | 52 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
|
| 188 | 52 | } |
|
| 189 | 52 | if ($this->skipInit) { |
|
| 190 | 52 | return; |
|
| 191 | } |
||
| 192 | 52 | $this->on(static::$eventAfterRegister, [$this, 'onAddProfile'], $this->profileConfig); |
|
| 193 | 52 | $this->on(static::$eventAfterRegister, [$this, 'onAssignCreator'], $this->creatorModel); |
|
| 194 | 52 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokeCreator']); |
|
| 195 | 52 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokeAdministrators']); |
|
| 196 | 52 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokePermissions']); |
|
| 197 | 52 | $this->initSelfBlameableEvents(); |
|
| 198 | 52 | parent::init(); |
|
| 199 | 52 | } |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @inheritdoc |
||
| 203 | */ |
||
| 204 | 18 | public function attributeLabels() |
|
| 205 | 17 | { |
|
| 206 | return [ |
||
| 207 | 11 | 'guid' => Yii::t('user', 'GUID'), |
|
| 208 | 1 | 'id' => Yii::t('user', 'ID'), |
|
| 209 | 1 | 'ip' => Yii::t('user', 'IP Address'), |
|
| 210 | 18 | 'ip_type' => Yii::t('user', 'IP Address Type'), |
|
| 211 | 1 | 'parent' => Yii::t('organization', 'Parent'), |
|
| 212 | 1 | 'created_at' => Yii::t('user', 'Creation Time'), |
|
| 213 | 1 | 'updated_at' => Yii::t('user', 'Last Updated Time'), |
|
| 214 | 1 | 'status' => Yii::t('user', 'Status'), |
|
| 215 | 1 | 'type' => Yii::t('user', 'Type'), |
|
| 216 | 1 | 'isExcludeOtherMembers' => Yii::t('organization', 'Exclude Other Members'), |
|
| 217 | 1 | 'isDisallowMemberJoinOther' => Yii::t('organization', 'Disallow Member to Join in Other Organizations'), |
|
| 218 | 1 | 'isOnlyAcceptCurrentOrgMember' => Yii::t('organization', 'Only Accept Current Organization Members'), |
|
| 219 | 1 | 'isOnlyAcceptSuperiorOrgMember' => Yii::t('organization', 'Only Accept Superior Organization Members'), |
|
| 220 | 1 | ]; |
|
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @inheritdoc |
||
| 225 | */ |
||
| 226 | 52 | public static function tableName() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Find. |
||
| 233 | * Friendly to IDE. |
||
| 234 | * @return OrganizationQuery |
||
| 235 | */ |
||
| 236 | 52 | public static function find() |
|
| 240 | |||
| 241 | 51 | protected function getTypeRules() |
|
| 242 | { |
||
| 243 | return [ |
||
| 244 | 51 | ['type', 'default', 'value' => static::TYPE_ORGANIZATION], |
|
| 245 | 51 | ['type', 'required'], |
|
| 246 | 51 | ['type', 'in', 'range' => [static::TYPE_ORGANIZATION, static::TYPE_DEPARTMENT]], |
|
| 247 | 51 | [['eom', 'djo', 'oacm', 'oasm'], 'default', 'value' => 0], |
|
| 248 | 51 | ]; |
|
| 249 | } |
||
| 250 | |||
| 251 | 51 | public function rules() |
|
| 252 | 20 | { |
|
| 253 | 51 | return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules()); |
|
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get Member Query. |
||
| 258 | * @return MemberQuery |
||
| 259 | */ |
||
| 260 | 50 | public function getMembers() |
|
| 261 | 20 | { |
|
| 262 | 50 | return $this->hasMany($this->memberClass, [ |
|
| 263 | 50 | $this->getNoInitMember()->createdByAttribute => $this->guidAttribute |
|
| 264 | 50 | ])->inverseOf('organization'); |
|
| 265 | 18 | } |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Get organization member users' query. |
||
| 269 | * @return BaseUserQuery |
||
| 270 | */ |
||
| 271 | 6 | public function getMemberUsers() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Get subordinate limit query. |
||
| 283 | * @return null|BaseBlameableQuery |
||
| 284 | */ |
||
| 285 | 2 | public function getSubordinateLimit() |
|
| 286 | { |
||
| 287 | 2 | if (empty($this->subordinateLimitClass)) { |
|
| 288 | return null; |
||
| 289 | 1 | } |
|
| 290 | 2 | return $this->hasOne($this->subordinateLimitClass, [ |
|
| 291 | 2 | $this->getNoInitSubordinateLimit()->createdByAttribute => $this->guidAttribute |
|
| 292 | 2 | ]); |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get member limit query. |
||
| 297 | * @return null|BaseBlameableQuery |
||
| 298 | */ |
||
| 299 | 1 | public function getMemberLimit() |
|
| 300 | 1 | { |
|
| 301 | 1 | if (empty($this->memberLimitClass)) { |
|
| 302 | return null; |
||
| 303 | 1 | } |
|
| 304 | 1 | return $this->hasOne($this->memberLimitClass, [ |
|
| 305 | 1 | $this->getNoInitMemberLimit()->createdByAttribute => $this->guidAttribute |
|
| 306 | 1 | ]); |
|
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get member with specified user. |
||
| 311 | * @param User|string|integer $user |
||
| 312 | * @return Member Null if `user` is not in this organization. |
||
| 313 | */ |
||
| 314 | 50 | public function getMember($user) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Add member to organization. |
||
| 321 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 322 | * If member is created, it will be re-assigned to this parameter. |
||
| 323 | * @see createMemberModel |
||
| 324 | * @see createMemberModelWithUser |
||
| 325 | * @return boolean |
||
| 326 | * @throws DisallowMemberJoinOtherException |
||
| 327 | * @throws ExcludeOtherMembersException |
||
| 328 | * @throws OnlyAcceptCurrentOrgMemberException |
||
| 329 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
| 330 | */ |
||
| 331 | 50 | public function addMember(&$member) |
|
| 332 | { |
||
| 333 | 50 | if ($this->getIsNewRecord()) { |
|
| 334 | return false; |
||
| 335 | } |
||
| 336 | 50 | if ($this->hasReachedMemberLimit()) { |
|
| 337 | 1 | return false; |
|
| 338 | } |
||
| 339 | 50 | $user = null; |
|
| 340 | 50 | if ($member instanceof Member) { |
|
| 341 | if ($member->getIsNewRecord()) { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | $user = $member->memberUser; |
||
| 345 | } |
||
| 346 | 50 | if ($member instanceof User) { |
|
| 347 | 50 | $user = $member; |
|
| 348 | 50 | } |
|
| 349 | 50 | if (is_string($member) || is_int($member)) { |
|
| 350 | $class = Yii::$app->user->identityClass; |
||
| 351 | $user = $class::find()->guidOrId($member)->one(); |
||
| 352 | } |
||
| 353 | 50 | if ($this->hasMember($user)) { |
|
| 354 | return false; |
||
| 355 | } |
||
| 356 | 50 | $orgs = $user->getAtOrganizations()->all(); |
|
| 357 | /* @var $orgs Organization[] */ |
||
| 358 | 50 | foreach ($orgs as $org) { |
|
| 359 | 31 | if ($org->topOrganization->isDisallowMemberJoinOther && !$org->topOrganization->equals($this->topOrganization)) { |
|
| 360 | 1 | throw new DisallowMemberJoinOtherException(Yii::t('organization', "An organization in which the user is located does not allow its members to join other organizations.")); |
|
| 361 | } |
||
| 362 | 31 | if ($this->topOrganization->isExcludeOtherMembers && !$org->topOrganization->equals($this->topOrganization)) { |
|
| 363 | 1 | throw new ExcludeOtherMembersException(Yii::t('organization', "The organization does not allow users who have joined other organizations to join.")); |
|
| 364 | } |
||
| 365 | 50 | } |
|
| 366 | 50 | if ($this->isDepartment() && $this->isOnlyAcceptCurrentOrgMember && !$this->topOrganization->hasMember($user)) { |
|
| 367 | 1 | throw new OnlyAcceptCurrentOrgMemberException(Yii::t('organization' ,'This department is only accepted by members of the organization.')); |
|
| 368 | } |
||
| 369 | 50 | if ($this->isDepartment() && $this->isOnlyAcceptSuperiorOrgMember && !$this->parent->hasMember($user)) { |
|
| 370 | 1 | throw new OnlyAcceptSuperiorOrgMemberException(Yii::t('organization', 'This department only accepts members of the parent organization or department.')); |
|
| 371 | } |
||
| 372 | |||
| 373 | 50 | $this->trigger(self::EVENT_BEFORE_ADD_MEMBER); |
|
| 374 | 50 | $model = null; |
|
| 375 | 50 | if ($member instanceof Member) { |
|
| 376 | $model = $this->createMemberModel($member); |
||
| 377 | 50 | } elseif (($member instanceof User) || is_string($member) || is_int($member)) { |
|
| 378 | 50 | $model = $this->createMemberModelWithUser($member); |
|
| 379 | 50 | } |
|
| 380 | 50 | $member = $model; |
|
| 381 | 50 | $result = ($member instanceof Member) ? $member->save() : false; |
|
| 382 | 50 | $this->trigger(self::EVENT_AFTER_ADD_MEMBER); |
|
| 383 | 50 | return $result; |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Create member model, and set organization with this. |
||
| 388 | * @param Member $member If this parameter is not new record, it's organization |
||
| 389 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 390 | * model and create new `Member` model. |
||
| 391 | * @see createMemberModelWithUser |
||
| 392 | * @return Member |
||
| 393 | */ |
||
| 394 | public function createMemberModel($member) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Create member model with user, and set organization with this. |
||
| 405 | * @param User|string|integer $user |
||
| 406 | * @return Member |
||
| 407 | */ |
||
| 408 | 50 | public function createMemberModelWithUser($user) |
|
| 409 | { |
||
| 410 | $config = [ |
||
| 411 | 50 | 'memberUser' => $user, |
|
| 412 | 50 | 'organization' => $this, |
|
| 413 | 50 | 'nickname' => '', |
|
| 414 | 50 | ]; |
|
| 415 | 50 | $member = $this->createMember($config); |
|
| 416 | 50 | $member->nickname = $member->memberUser->profile->nickname; |
|
| 417 | 50 | return $member; |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Remove member. |
||
| 422 | * Note: the creator cannot be removed. |
||
| 423 | * @param Member|User $member |
||
| 424 | * @return boolean |
||
| 425 | */ |
||
| 426 | 4 | public function removeMember(&$member) |
|
| 427 | { |
||
| 428 | 4 | if ($this->getIsNewRecord()) { |
|
| 429 | return false; |
||
| 430 | } |
||
| 431 | 4 | $this->trigger(self::EVENT_BEFORE_REMOVE_MEMBER); |
|
| 432 | 4 | if ($member instanceof $this->memberClass) { |
|
| 433 | 4 | $member = $member->{$member->memberAttribute}; |
|
| 434 | 4 | } |
|
| 435 | 4 | $member = $this->getMember($member); |
|
| 436 | 4 | if (!$member || $member->isCreator()) { |
|
| 437 | return false; |
||
| 438 | } |
||
| 439 | 4 | $result = $member->delete() > 0; |
|
| 440 | 4 | $this->trigger(self::EVENT_AFTER_REMOVE_MEMBER); |
|
| 441 | 4 | return $result; |
|
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Remove administrator. |
||
| 446 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 447 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 448 | * @return boolean |
||
| 449 | * @throws IntegrityException |
||
| 450 | */ |
||
| 451 | 17 | public function removeAdministrator(&$member, $keep = true) |
|
| 452 | { |
||
| 453 | if ($this->getIsNewRecord()) { |
||
| 454 | return false; |
||
| 455 | } |
||
| 456 | if ($member instanceof $this->memberClass) { |
||
| 457 | $member = $member->{$member->memberAttribute}; |
||
| 458 | } |
||
| 459 | $member = $this->getMember($member); |
||
| 460 | if ($member && $member->isAdministrator()) { |
||
| 461 | if ($keep) { |
||
| 462 | 17 | return $member->revokeAdministrator(); |
|
| 463 | 17 | } |
|
| 464 | return $this->removeMember($member); |
||
| 465 | } |
||
| 466 | return false; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * |
||
| 471 | * @param Event $event |
||
| 472 | * @throws IntegrityException |
||
| 473 | * @return boolean |
||
| 474 | */ |
||
| 475 | 51 | public function onAddProfile($event) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * |
||
| 486 | * @param Event $event |
||
| 487 | */ |
||
| 488 | 51 | public function onAssignCreator($event) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * |
||
| 495 | * @param Event $event |
||
| 496 | * @return boolean |
||
| 497 | */ |
||
| 498 | 20 | public function onRevokeCreator($event) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * |
||
| 510 | * @param Event $event |
||
| 511 | * @return boolean |
||
| 512 | */ |
||
| 513 | 20 | public function onRevokeAdministrators($event) |
|
| 514 | { |
||
| 515 | 20 | $sender = $event->sender; |
|
| 516 | /* @var $sender static */ |
||
| 517 | 20 | $members = $sender->getMemberAdministrators()->all(); |
|
| 518 | /* @var $members Member[] */ |
||
| 519 | 20 | foreach ($members as $member) |
|
| 520 | { |
||
| 521 | 1 | $member->revokeAdministrator(); |
|
| 522 | 20 | } |
|
| 523 | 20 | return true; |
|
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * |
||
| 528 | * @param Event $event |
||
| 529 | */ |
||
| 530 | 20 | public function onRevokePermissions($event) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Check whether current instance is an organization. |
||
| 537 | * @return boolean |
||
| 538 | */ |
||
| 539 | 50 | public function isOrganization() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Check whether current instance if a department. |
||
| 546 | * @return boolean |
||
| 547 | */ |
||
| 548 | 50 | public function isDepartment() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Check whether the current organization has a member. |
||
| 555 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 556 | * @return boolean |
||
| 557 | */ |
||
| 558 | 50 | public function hasMember($user) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Get member query which role is specified `Creator`. |
||
| 565 | * @return MemberQuery |
||
| 566 | */ |
||
| 567 | 24 | public function getMemberCreators() |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Get member query which role is specified `Administrator`. |
||
| 574 | * @return MemberQuery |
||
| 575 | */ |
||
| 576 | 22 | public function getMemberAdministrators() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Get user query which role is specified `Creator`. |
||
| 583 | * @return BaseUserQuery |
||
| 584 | */ |
||
| 585 | 4 | public function getCreator() |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Get user query which role is specified `Administrator`. |
||
| 597 | * @return BaseUserQuery |
||
| 598 | */ |
||
| 599 | 2 | public function getAdministrators() |
|
| 608 | |||
| 609 | /** |
||
| 610 | * |
||
| 611 | * @param User $user |
||
| 612 | * @return boolean |
||
| 613 | * @throws \Exception |
||
| 614 | * @throws IntegrityException |
||
| 615 | */ |
||
| 616 | 51 | protected function addCreator($user) |
|
| 617 | { |
||
| 618 | 51 | if (!$user) { |
|
| 619 | 1 | throw new InvalidParamException('Creator Invalid.'); |
|
| 620 | } |
||
| 621 | 50 | $member = $user; |
|
| 622 | 50 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 623 | try { |
||
| 624 | 50 | if (!$this->addMember($member)) { |
|
| 625 | throw new IntegrityException('Failed to add member.'); |
||
| 626 | } |
||
| 627 | 50 | $role = $this->isOrganization() ? (new OrganizationCreator)->name : (new DepartmentCreator)->name; |
|
| 628 | 50 | $member->assignRole($role); |
|
| 629 | 50 | if (!$member->save()) { |
|
| 630 | throw new IntegrityException('Failed to assign creator.'); |
||
| 631 | } |
||
| 632 | 50 | $transaction->commit(); |
|
| 633 | 50 | } catch (\Exception $ex) { |
|
| 634 | $transaction->rollBack(); |
||
| 635 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 636 | throw $ex; |
||
| 637 | } |
||
| 638 | 50 | return true; |
|
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Add administrator. |
||
| 643 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 644 | * @return boolean |
||
| 645 | * @throws \Exception |
||
| 646 | * @throws IntegrityException |
||
| 647 | */ |
||
| 648 | 17 | public function addAdministrator($user) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Check whether the current organization has administrator. |
||
| 668 | * @param User|integer|string $user |
||
| 669 | * @return boolean |
||
| 670 | */ |
||
| 671 | 2 | public function hasAdministrator($user) |
|
| 672 | 2 | { |
|
| 673 | 2 | $member = $this->getMember($user); |
|
| 674 | 2 | if (!$member) { |
|
| 675 | return false; |
||
| 676 | } |
||
| 677 | 2 | return $member->isAdministrator(); |
|
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 682 | * @return boolean |
||
| 683 | */ |
||
| 684 | 18 | public function hasReachedSubordinateLimit() |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Check whether this organization has reached the upper limit of members. |
||
| 700 | * @return boolean |
||
| 701 | */ |
||
| 702 | 50 | public function hasReachedMemberLimit() |
|
| 715 | |||
| 716 | 31 | public function getIsExcludeOtherMembers() |
|
| 748 | |||
| 749 | /** |
||
| 750 | * @return $this|null|static |
||
| 751 | */ |
||
| 752 | 31 | public function getTopOrganization() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Check whether the subordinates have the [[$user]] |
||
| 763 | * Note, this operation may consume the quantity of database selection. |
||
| 764 | * @param User $user |
||
| 765 | * @return bool |
||
| 766 | */ |
||
| 767 | 2 | public function hasMemberInSubordinates($user) |
|
| 768 | { |
||
| 769 | 2 | if ($this->getChildren()->joinWith(['memberUsers mu_alias']) |
|
| 770 | 2 | ->andWhere(['mu_alias.' . $user->guidAttribute => $user->getGUID()])->exists()) { |
|
| 771 | 1 | return true; |
|
| 772 | } |
||
| 773 | 2 | $children = $this->children; |
|
| 774 | /* @var $children static[] */ |
||
| 775 | 2 | foreach ($children as $child) { |
|
| 776 | 2 | if ($child->hasMemberInSubordinates($user)) { |
|
| 777 | 1 | return true; |
|
| 778 | } |
||
| 782 | } |
||
| 783 |