UserGroupObject.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\FlexObject;
  11. use Grav\Common\Grav;
  12. use Grav\Common\User\Access;
  13. use Grav\Common\User\Interfaces\UserGroupInterface;
  14. use function is_bool;
  15. /**
  16. * Flex User Group
  17. *
  18. * @package Grav\Common\User
  19. *
  20. * @property string $groupname
  21. * @property Access $access
  22. */
  23. class UserGroupObject extends FlexObject implements UserGroupInterface
  24. {
  25. /** @var Access */
  26. protected $_access;
  27. /** @var array|null */
  28. protected $access;
  29. /**
  30. * @return array
  31. */
  32. public static function getCachedMethods(): array
  33. {
  34. return [
  35. 'authorize' => false,
  36. ] + parent::getCachedMethods();
  37. }
  38. /**
  39. * @return string
  40. */
  41. public function getTitle(): string
  42. {
  43. return $this->getProperty('readableName');
  44. }
  45. /**
  46. * Checks user authorization to the action.
  47. *
  48. * @param string $action
  49. * @param string|null $scope
  50. * @return bool|null
  51. */
  52. public function authorize(string $action, string $scope = null): ?bool
  53. {
  54. if ($scope === 'test') {
  55. $scope = null;
  56. } elseif (!$this->getProperty('enabled', true)) {
  57. return null;
  58. }
  59. $access = $this->getAccess();
  60. $authorized = $access->authorize($action, $scope);
  61. if (is_bool($authorized)) {
  62. return $authorized;
  63. }
  64. return $access->authorize('admin.super') ? true : null;
  65. }
  66. public static function groupNames(): array
  67. {
  68. $groups = [];
  69. $user_groups = Grav::instance()['user_groups'];
  70. foreach ($user_groups as $key => $group) {
  71. $groups[$key] = $group->readableName;
  72. }
  73. return $groups;
  74. }
  75. /**
  76. * @return Access
  77. */
  78. protected function getAccess(): Access
  79. {
  80. if (null === $this->_access) {
  81. $this->getProperty('access');
  82. }
  83. return $this->_access;
  84. }
  85. /**
  86. * @param mixed $value
  87. * @return array
  88. */
  89. protected function offsetLoad_access($value): array
  90. {
  91. if (!$value instanceof Access) {
  92. $value = new Access($value);
  93. }
  94. $this->_access = $value;
  95. return $value->jsonSerialize();
  96. }
  97. /**
  98. * @param mixed $value
  99. * @return array
  100. */
  101. protected function offsetPrepare_access($value): array
  102. {
  103. return $this->offsetLoad_access($value);
  104. }
  105. /**
  106. * @param array|null $value
  107. * @return array|null
  108. */
  109. protected function offsetSerialize_access(?array $value): ?array
  110. {
  111. return $value;
  112. }
  113. }