AbstractEntityVoter.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Security\Voter\EntityVoter;
  3. use App\Entity\Access\Access;
  4. use App\Service\Access\Utils;
  5. use App\Service\Security\InternalRequestsService;
  6. use App\Service\Security\SwitchUser;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\SecurityBundle\Security;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. /**
  12. * Base class for custom Voters.
  13. *
  14. * This class also defines a default behavior for entity based voters (ex: FileVoter)
  15. *
  16. * @see doc/security.md
  17. */
  18. abstract class AbstractEntityVoter extends Voter
  19. {
  20. protected const READ = 'READ';
  21. protected const EDIT = 'EDIT';
  22. protected const CREATE = 'CREATE';
  23. protected const DELETE = 'DELETE';
  24. /**
  25. * The current user if any; access it trough isUserLoggedIn or getUser methods
  26. * If the current user is null, it has not been fetched already
  27. * If it is false, there is no user logged in.
  28. * @phpstan-ignore-next-line faux positif
  29. */
  30. private Access|false|null $user = null;
  31. /**
  32. * The supported class name. Override it in subclass.
  33. * Ex:
  34. * protected ?string $entityClass = File::class;.
  35. */
  36. protected static ?string $entityClass = null;
  37. /**
  38. * List of supported operations. Override it to restrict.
  39. *
  40. * @var array<string>
  41. */
  42. protected static array $allowedOperations = [
  43. self::READ, self::EDIT, self::CREATE, self::DELETE,
  44. ];
  45. public function __construct(
  46. protected Security $security,
  47. protected Utils $accessUtils,
  48. private InternalRequestsService $internalRequestsService,
  49. private EntityManagerInterface $em,
  50. private SwitchUser $switchUser,
  51. ) {
  52. }
  53. /**
  54. * Default `supports` method, that uses self::entityClass and self::allowedOperations to determine if the voter
  55. * supports the subject and attribute.
  56. */
  57. protected function supports(string $attribute, mixed $subject): bool
  58. {
  59. if (static::$entityClass === null) {
  60. throw new \RuntimeException('Setup the self::$entityClass property, or override the supports() method');
  61. }
  62. return $subject !== null && $subject::class === static::$entityClass && in_array($attribute, static::$allowedOperations);
  63. }
  64. /**
  65. * Default `voteOnAttribute` method, calling one of the `canXxxx()` method, according to the attribute.
  66. */
  67. protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  68. {
  69. switch ($attribute) {
  70. case self::READ:
  71. return $this->canView($subject);
  72. case self::EDIT:
  73. return $this->canEdit($subject);
  74. case self::CREATE:
  75. return $this->canCreate($subject);
  76. case self::DELETE:
  77. return $this->canDelete($subject);
  78. }
  79. return false;
  80. }
  81. /**
  82. * Does the client have the right to view this resource?
  83. */
  84. protected function canView(object $subject): bool
  85. {
  86. return false;
  87. }
  88. /**
  89. * Does the client have the right to edit this resource?
  90. */
  91. protected function canEdit(object $subject): bool
  92. {
  93. return false;
  94. }
  95. /**
  96. * Does the client have the right to create this resource?
  97. */
  98. protected function canCreate(object $subject): bool
  99. {
  100. return false;
  101. }
  102. /**
  103. * Does the client have the right to delete this resource?
  104. */
  105. protected function canDelete(object $subject): bool
  106. {
  107. return false;
  108. }
  109. /**
  110. * Returns the current logged in user.
  111. */
  112. protected function getUser(): ?Access
  113. {
  114. if ($this->user === null) {
  115. /** @var Access $user */
  116. $user = $this->security->getUser();
  117. // <-- Special case of impersonated users: the switch user is not setup yet by symfony, we have to do it "manually"
  118. $switchHeaderId = $_SERVER['HTTP_X_SWITCH_USER'] ?? null;
  119. if ($switchHeaderId !== null) {
  120. $switchAs = $this->em->find(Access::class, $switchHeaderId);
  121. if (
  122. $switchAs
  123. && (
  124. $this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')
  125. || $this->switchUser->isAllowedToSwitch($user, $switchAs)
  126. )
  127. ) {
  128. $user = $switchAs;
  129. }
  130. }
  131. // -->
  132. // If the user is not anonymous, remember it
  133. $this->user = $user instanceof Access ? $user : false;
  134. }
  135. return $this->user !== false ? $this->user : null;
  136. }
  137. /**
  138. * Is the client an authenticated user ?
  139. */
  140. protected function isUserLoggedIn(): bool
  141. {
  142. return $this->getUser() !== null;
  143. }
  144. /**
  145. * Is the current request a valid internal request?
  146. *
  147. * @see doc/internal_requests.md
  148. * @see doc/security.md
  149. */
  150. protected function isValidInternalRequest(): bool
  151. {
  152. $clientIp = $_SERVER['REMOTE_ADDR'] ?? null;
  153. $internalRequestToken = $_SERVER['HTTP_INTERNAL_REQUESTS_TOKEN'] ?? '';
  154. return $this->internalRequestsService->isAllowed($clientIp, $internalRequestToken);
  155. }
  156. }