src/Security/Voter/FineVoter.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Fine;
  4. use App\Security\Role\FineRole;
  5. use App\Security\Scope\FineScopeResolver;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Decyduje o dostępie do zawiadomienia na podstawie jego statusu weryfikacji.
  10.  *
  11.  * Uwaga: Field::setPermission() dostaje FieldDto, a nie encję, więc pól nie da się
  12.  * autoryzować tą drogą - tam zostaje setDisabled().
  13.  *
  14.  * Cała logika ról siedzi w FineScopeResolver, żeby menu, filtrowanie listy i ten voter
  15.  * nie rozjechały się między sobą.
  16.  */
  17. final class FineVoter extends Voter
  18. {
  19.     public const VIEW = 'FINE_VIEW';
  20.     public const EDIT = 'FINE_EDIT';
  21.     public const VERIFY = 'FINE_VERIFY';
  22.     public const ANON = 'FINE_ANON';
  23.     public const ATTRIBUTES = [self::VIEW, self::EDIT, self::VERIFY, self::ANON];
  24.     private const VERB_BY_ATTRIBUTE = [
  25.         self::VIEW => FineRole::VERB_VIEW,
  26.         self::EDIT => FineRole::VERB_EDIT,
  27.         self::VERIFY => FineRole::VERB_VERIFY,
  28.         self::ANON => FineRole::VERB_ANON,
  29.     ];
  30.     public function __construct(private readonly FineScopeResolver $scopeResolver)
  31.     {
  32.     }
  33.     protected function supports(string $attribute, $subject): bool
  34.     {
  35.         return isset(self::VERB_BY_ATTRIBUTE[$attribute]);
  36.     }
  37.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  38.     {
  39.         $verb = self::VERB_BY_ATTRIBUTE[$attribute];
  40.         if ($subject instanceof Fine) {
  41.             $statusId = $subject->getVerificationStatus()?->getId();
  42.             return $statusId !== null && $this->scopeResolver->can($verb, $statusId);
  43.         }
  44.         return $this->scopeResolver->canAnywhere($verb);
  45.     }
  46. }