PageAuthorizeEvent.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2022 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login\Events;
  9. use Grav\Common\Data\Data;
  10. use Grav\Common\Page\Interfaces\PageInterface;
  11. use Grav\Common\User\Interfaces\UserInterface;
  12. use Symfony\Contracts\EventDispatcher\Event;
  13. /**
  14. * Class PageAuthorizationEvent
  15. * @package Grav\Plugin\Login\Events
  16. */
  17. class PageAuthorizeEvent extends Event
  18. {
  19. /** @var PageInterface */
  20. public $page;
  21. /** @var UserInterface */
  22. public $user;
  23. /** @var Data */
  24. public $config;
  25. /** @var bool */
  26. private $protected = false;
  27. /** @var bool|null */
  28. private $access;
  29. /**
  30. * @param PageInterface $page
  31. * @param UserInterface $user
  32. * @param Data|null $config
  33. */
  34. public function __construct(PageInterface $page, UserInterface $user, Data $config = null)
  35. {
  36. $this->page = $page;
  37. $this->user = $user;
  38. $this->config = $config ?? new Data();
  39. }
  40. /**
  41. * @return bool
  42. */
  43. public function hasProtectedAccess(): bool
  44. {
  45. return $this->protected;
  46. }
  47. /**
  48. * @return bool
  49. */
  50. public function isAllowed(): bool
  51. {
  52. return $this->getAccess() === true;
  53. }
  54. /**
  55. * @return bool
  56. */
  57. public function isDenied(): bool
  58. {
  59. return $this->getAccess() === false;
  60. }
  61. /**
  62. * @return bool
  63. */
  64. public function isUndecided(): bool
  65. {
  66. return $this->getAccess() === null;
  67. }
  68. /**
  69. * @return bool|null
  70. */
  71. public function getAccess(): ?bool
  72. {
  73. return $this->access;
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function setProtectedAccess(): void
  79. {
  80. $this->protected = true;
  81. }
  82. /**
  83. * @return bool|null
  84. */
  85. public function allow(): ?bool
  86. {
  87. return $this->setAccess(true);
  88. }
  89. /**
  90. * @return bool|null
  91. */
  92. public function deny(): ?bool
  93. {
  94. return $this->setAccess(false);
  95. }
  96. /**
  97. * @param bool|null $access
  98. * @return void
  99. */
  100. public function setAccess(?bool $access): ?bool
  101. {
  102. if ($this->access !== false && is_bool($access)) {
  103. $this->access = $access;
  104. $this->setProtectedAccess();
  105. }
  106. return $this->access;
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function __debugInfo(): array
  112. {
  113. return [
  114. 'page' => $this->page->route(),
  115. 'user' => $this->user->username ?: 'guest',
  116. // 'config' => $this->config->jsonSerialize(),
  117. ];
  118. }
  119. }