| Total Complexity | 90 |
| Total Lines | 677 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Group 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.
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 Group, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class Group extends DataObject |
||
| 57 | { |
||
| 58 | |||
| 59 | private static $db = [ |
||
| 60 | "Title" => "Varchar(255)", |
||
| 61 | "Description" => "Text", |
||
| 62 | "Code" => "Varchar(255)", |
||
| 63 | "Locked" => "Boolean", |
||
| 64 | "Sort" => "Int", |
||
| 65 | "HtmlEditorConfig" => "Text" |
||
| 66 | ]; |
||
| 67 | |||
| 68 | private static $has_one = [ |
||
| 69 | "Parent" => Group::class, |
||
| 70 | ]; |
||
| 71 | |||
| 72 | private static $has_many = [ |
||
| 73 | "Permissions" => Permission::class, |
||
| 74 | "Groups" => Group::class, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | private static $many_many = [ |
||
| 78 | "Members" => Member::class, |
||
| 79 | "Roles" => PermissionRole::class, |
||
| 80 | ]; |
||
| 81 | |||
| 82 | private static $extensions = [ |
||
| 83 | Hierarchy::class, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | private static $table_name = "Group"; |
||
| 87 | |||
| 88 | public function getAllChildren() |
||
| 89 | { |
||
| 90 | $doSet = new ArrayList(); |
||
| 91 | |||
| 92 | $children = Group::get()->filter("ParentID", $this->ID); |
||
| 93 | /** @var Group $child */ |
||
| 94 | foreach ($children as $child) { |
||
| 95 | $doSet->push($child); |
||
| 96 | $doSet->merge($child->getAllChildren()); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $doSet; |
||
| 100 | } |
||
| 101 | |||
| 102 | private function getDecodedBreadcrumbs() |
||
| 103 | { |
||
| 104 | $list = Group::get()->exclude('ID', $this->ID); |
||
| 105 | $groups = ArrayList::create(); |
||
| 106 | foreach ($list as $group) { |
||
| 107 | $groups->push(['ID' => $group->ID, 'Title' => $group->getBreadcrumbs(' » ')]); |
||
| 108 | } |
||
| 109 | return $groups; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Caution: Only call on instances, not through a singleton. |
||
| 114 | * The "root group" fields will be created through {@link SecurityAdmin->EditForm()}. |
||
| 115 | * |
||
| 116 | * @skipUpgrade |
||
| 117 | * @return FieldList |
||
| 118 | */ |
||
| 119 | public function getCMSFields() |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param bool $includerelations Indicate if the labels returned include relation fields |
||
| 286 | * @return array |
||
| 287 | * @skipUpgrade |
||
| 288 | */ |
||
| 289 | public function fieldLabels($includerelations = true) |
||
| 290 | { |
||
| 291 | $labels = parent::fieldLabels($includerelations); |
||
| 292 | $labels['Title'] = _t(__CLASS__ . '.GROUPNAME', 'Group name'); |
||
| 293 | $labels['Description'] = _t('SilverStripe\\Security\\Group.Description', 'Description'); |
||
| 294 | $labels['Code'] = _t('SilverStripe\\Security\\Group.Code', 'Group Code', 'Programmatical code identifying a group'); |
||
| 295 | $labels['Locked'] = _t('SilverStripe\\Security\\Group.Locked', 'Locked?', 'Group is locked in the security administration area'); |
||
| 296 | $labels['Sort'] = _t('SilverStripe\\Security\\Group.Sort', 'Sort Order'); |
||
| 297 | if ($includerelations) { |
||
| 298 | $labels['Parent'] = _t('SilverStripe\\Security\\Group.Parent', 'Parent Group', 'One group has one parent group'); |
||
| 299 | $labels['Permissions'] = _t('SilverStripe\\Security\\Group.has_many_Permissions', 'Permissions', 'One group has many permissions'); |
||
| 300 | $labels['Members'] = _t('SilverStripe\\Security\\Group.many_many_Members', 'Members', 'One group has many members'); |
||
| 301 | } |
||
| 302 | |||
| 303 | return $labels; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get many-many relation to {@link Member}, |
||
| 308 | * including all members which are "inherited" from children groups of this record. |
||
| 309 | * See {@link DirectMembers()} for retrieving members without any inheritance. |
||
| 310 | * |
||
| 311 | * @param string $filter |
||
| 312 | * @return ManyManyList |
||
| 313 | */ |
||
| 314 | public function Members($filter = '') |
||
| 315 | { |
||
| 316 | // First get direct members as a base result |
||
| 317 | $result = $this->DirectMembers(); |
||
| 318 | |||
| 319 | // Unsaved group cannot have child groups because its ID is still 0. |
||
| 320 | if (!$this->exists()) { |
||
| 321 | return $result; |
||
| 322 | } |
||
| 323 | |||
| 324 | // Remove the default foreign key filter in prep for re-applying a filter containing all children groups. |
||
| 325 | // Filters are conjunctive in DataQuery by default, so this filter would otherwise overrule any less specific |
||
| 326 | // ones. |
||
| 327 | if (!($result instanceof UnsavedRelationList)) { |
||
| 328 | $result = $result->alterDataQuery(function ($query) { |
||
| 329 | /** @var DataQuery $query */ |
||
| 330 | $query->removeFilterOn('Group_Members'); |
||
| 331 | }); |
||
| 332 | } |
||
| 333 | |||
| 334 | // Now set all children groups as a new foreign key |
||
| 335 | $familyIDs = $this->collateFamilyIDs(); |
||
| 336 | $result = $result->forForeignID($familyIDs); |
||
| 337 | |||
| 338 | return $result->where($filter); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Return only the members directly added to this group |
||
| 343 | */ |
||
| 344 | public function DirectMembers() |
||
| 345 | { |
||
| 346 | return $this->getManyManyComponents('Members'); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return a set of this record's "family" of IDs - the IDs of |
||
| 351 | * this record and all its descendants. |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public function collateFamilyIDs() |
||
| 356 | { |
||
| 357 | if (!$this->exists()) { |
||
| 358 | throw new \InvalidArgumentException("Cannot call collateFamilyIDs on unsaved Group."); |
||
| 359 | } |
||
| 360 | |||
| 361 | $familyIDs = []; |
||
| 362 | $chunkToAdd = [$this->ID]; |
||
| 363 | |||
| 364 | while ($chunkToAdd) { |
||
| 365 | $familyIDs = array_merge($familyIDs, $chunkToAdd); |
||
| 366 | |||
| 367 | // Get the children of *all* the groups identified in the previous chunk. |
||
| 368 | // This minimises the number of SQL queries necessary |
||
| 369 | $chunkToAdd = Group::get()->filter("ParentID", $chunkToAdd)->column('ID'); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $familyIDs; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns an array of the IDs of this group and all its parents |
||
| 377 | * |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function collateAncestorIDs() |
||
| 381 | { |
||
| 382 | $parent = $this; |
||
| 383 | $items = []; |
||
| 384 | while ($parent instanceof Group) { |
||
| 385 | $items[] = $parent->ID; |
||
| 386 | $parent = $parent->getParent(); |
||
| 387 | } |
||
| 388 | return $items; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Check if the group is a child of the given group or any parent groups |
||
| 393 | * |
||
| 394 | * @param string|int|Group $group Group instance, Group Code or ID |
||
| 395 | * @return bool Returns TRUE if the Group is a child of the given group, otherwise FALSE |
||
| 396 | */ |
||
| 397 | public function inGroup($group) |
||
| 398 | { |
||
| 399 | return in_array($this->identifierToGroupID($group), $this->collateAncestorIDs()); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Check if the group is a child of the given groups or any parent groups |
||
| 404 | * |
||
| 405 | * @param (string|int|Group)[] $groups |
||
| 406 | * @param bool $requireAll set to TRUE if must be in ALL groups, or FALSE if must be in ANY |
||
| 407 | * @return bool Returns TRUE if the Group is a child of any of the given groups, otherwise FALSE |
||
| 408 | */ |
||
| 409 | public function inGroups($groups, $requireAll = false) |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Turn a string|int|Group into a GroupID |
||
| 433 | * |
||
| 434 | * @param string|int|Group $groupID Group instance, Group Code or ID |
||
| 435 | * @return int|null the Group ID or NULL if not found |
||
| 436 | */ |
||
| 437 | protected function identifierToGroupID($groupID) |
||
| 438 | { |
||
| 439 | if (is_numeric($groupID) && Group::get()->byID($groupID)) { |
||
| 440 | return $groupID; |
||
| 441 | } elseif (is_string($groupID) && $groupByCode = Group::get()->filter(['Code' => $groupID])->first()) { |
||
| 442 | return $groupByCode->ID; |
||
| 443 | } elseif ($groupID instanceof Group && $groupID->exists()) { |
||
| 444 | return $groupID->ID; |
||
| 445 | } |
||
| 446 | return null; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * This isn't a descendant of SiteTree, but needs this in case |
||
| 451 | * the group is "reorganised"; |
||
| 452 | */ |
||
| 453 | public function cmsCleanup_parentChanged() |
||
| 454 | { |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Override this so groups are ordered in the CMS |
||
| 459 | */ |
||
| 460 | public function stageChildren() |
||
| 461 | { |
||
| 462 | return Group::get() |
||
| 463 | ->filter("ParentID", $this->ID) |
||
| 464 | ->exclude("ID", $this->ID) |
||
| 465 | ->sort('"Sort"'); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getTreeTitle() |
||
| 472 | { |
||
| 473 | $title = htmlspecialchars($this->Title, ENT_QUOTES); |
||
| 474 | $this->extend('updateTreeTitle', $title); |
||
| 475 | return $title; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Overloaded to ensure the code is always descent. |
||
| 480 | * |
||
| 481 | * @param string $val |
||
| 482 | */ |
||
| 483 | public function setCode($val) |
||
| 486 | } |
||
| 487 | |||
| 488 | public function validate() |
||
| 489 | { |
||
| 490 | $result = parent::validate(); |
||
| 491 | |||
| 492 | // Check if the new group hierarchy would add certain "privileged permissions", |
||
| 493 | // and require an admin to perform this change in case it does. |
||
| 494 | // This prevents "sub-admin" users with group editing permissions to increase their privileges. |
||
| 495 | if ($this->Parent()->exists() && !Permission::check('ADMIN')) { |
||
| 527 | } |
||
| 528 | |||
| 529 | public function getCMSCompositeValidator(): CompositeValidator |
||
| 538 | } |
||
| 539 | |||
| 540 | public function onBeforeWrite() |
||
| 541 | { |
||
| 542 | parent::onBeforeWrite(); |
||
| 543 | |||
| 544 | // Only set code property when the group has a custom title, and no code exists. |
||
| 545 | // The "Code" attribute is usually treated as a more permanent identifier than database IDs |
||
| 546 | // in custom application logic, so can't be changed after its first set. |
||
| 547 | if (!$this->Code && $this->Title != _t(__CLASS__ . '.NEWGROUP', "New Group")) { |
||
| 548 | $this->setCode($this->Title); |
||
| 549 | } |
||
| 550 | |||
| 551 | // Make sure the code for this group is unique. |
||
| 552 | $this->dedupeCode(); |
||
| 553 | } |
||
| 554 | |||
| 555 | public function onBeforeDelete() |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Checks for permission-code CMS_ACCESS_SecurityAdmin. |
||
| 572 | * If the group has ADMIN permissions, it requires the user to have ADMIN permissions as well. |
||
| 573 | * |
||
| 574 | * @param Member $member Member |
||
| 575 | * @return boolean |
||
| 576 | */ |
||
| 577 | public function canEdit($member = null) |
||
| 578 | { |
||
| 579 | if (!$member) { |
||
| 580 | $member = Security::getCurrentUser(); |
||
| 581 | } |
||
| 582 | |||
| 583 | // extended access checks |
||
| 584 | $results = $this->extend('canEdit', $member); |
||
| 585 | if ($results && is_array($results)) { |
||
| 586 | if (!min($results)) { |
||
| 587 | return false; |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | if (// either we have an ADMIN |
||
| 592 | (bool)Permission::checkMember($member, "ADMIN") |
||
| 593 | || ( |
||
| 594 | // or a privileged CMS user and a group without ADMIN permissions. |
||
| 595 | // without this check, a user would be able to add himself to an administrators group |
||
| 596 | // with just access to the "Security" admin interface |
||
| 597 | Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin") && |
||
| 598 | !Permission::get()->filter(['GroupID' => $this->ID, 'Code' => 'ADMIN'])->exists() |
||
| 599 | ) |
||
| 600 | ) { |
||
| 601 | return true; |
||
| 602 | } |
||
| 603 | |||
| 604 | return false; |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Checks for permission-code CMS_ACCESS_SecurityAdmin. |
||
| 609 | * |
||
| 610 | * @param Member $member |
||
| 611 | * @return boolean |
||
| 612 | */ |
||
| 613 | public function canView($member = null) |
||
| 614 | { |
||
| 615 | if (!$member) { |
||
| 616 | $member = Security::getCurrentUser(); |
||
| 617 | } |
||
| 618 | |||
| 619 | // extended access checks |
||
| 620 | $results = $this->extend('canView', $member); |
||
| 621 | if ($results && is_array($results)) { |
||
| 622 | if (!min($results)) { |
||
| 623 | return false; |
||
| 624 | } |
||
| 625 | } |
||
| 626 | |||
| 627 | // user needs access to CMS_ACCESS_SecurityAdmin |
||
| 628 | if (Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")) { |
||
| 629 | return true; |
||
| 630 | } |
||
| 631 | |||
| 632 | return false; |
||
| 633 | } |
||
| 634 | |||
| 635 | public function canDelete($member = null) |
||
| 636 | { |
||
| 637 | if (!$member) { |
||
| 638 | $member = Security::getCurrentUser(); |
||
| 639 | } |
||
| 640 | |||
| 641 | // extended access checks |
||
| 642 | $results = $this->extend('canDelete', $member); |
||
| 643 | if ($results && is_array($results)) { |
||
| 644 | if (!min($results)) { |
||
| 645 | return false; |
||
| 646 | } |
||
| 647 | } |
||
| 648 | |||
| 649 | return $this->canEdit($member); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Returns all of the children for the CMS Tree. |
||
| 654 | * Filters to only those groups that the current user can edit |
||
| 655 | * |
||
| 656 | * @return ArrayList |
||
| 657 | */ |
||
| 658 | public function AllChildrenIncludingDeleted() |
||
| 659 | { |
||
| 660 | $children = parent::AllChildrenIncludingDeleted(); |
||
| 661 | |||
| 662 | $filteredChildren = new ArrayList(); |
||
| 663 | |||
| 664 | if ($children) { |
||
| 665 | foreach ($children as $child) { |
||
| 666 | /** @var DataObject $child */ |
||
| 667 | if ($child->canView()) { |
||
| 668 | $filteredChildren->push($child); |
||
| 669 | } |
||
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | return $filteredChildren; |
||
| 674 | } |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Add default records to database. |
||
| 678 | * |
||
| 679 | * This function is called whenever the database is built, after the |
||
| 680 | * database tables have all been created. |
||
| 681 | */ |
||
| 682 | public function requireDefaultRecords() |
||
| 709 | } |
||
| 710 | |||
| 711 | // Members are populated through Member->requireDefaultRecords() |
||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Code needs to be unique as it is used to identify a specific group. Ensure no duplicate |
||
| 716 | * codes are created. |
||
| 717 | * |
||
| 718 | * @deprecated 5.0 Remove deduping in favour of throwing a validation error for duplicates. |
||
| 719 | */ |
||
| 720 | private function dedupeCode(): void |
||
| 733 | } |
||
| 734 | } |
||
| 735 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths