UserGroupCollection.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Common\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Common\Flex\Types\UserGroups;
  10. use Grav\Common\Flex\FlexCollection;
  11. /**
  12. * Class UserGroupCollection
  13. * @package Grav\Common\Flex\Types\UserGroups
  14. *
  15. * @extends FlexCollection<UserGroupObject>
  16. */
  17. class UserGroupCollection extends FlexCollection
  18. {
  19. /**
  20. * @return array
  21. */
  22. public static function getCachedMethods(): array
  23. {
  24. return [
  25. 'authorize' => false,
  26. ] + parent::getCachedMethods();
  27. }
  28. /**
  29. * Checks user authorization to the action.
  30. *
  31. * @param string $action
  32. * @param string|null $scope
  33. * @return bool|null
  34. */
  35. public function authorize(string $action, string $scope = null): ?bool
  36. {
  37. $authorized = null;
  38. /** @var UserGroupObject $object */
  39. foreach ($this as $object) {
  40. $auth = $object->authorize($action, $scope);
  41. if ($auth === true) {
  42. $authorized = true;
  43. } elseif ($auth === false) {
  44. return false;
  45. }
  46. }
  47. return $authorized;
  48. }
  49. }