src/Security/Voter/RequestLogVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\RequestLog;
  4. use App\Security\Scope\RequestLogScopeResolver;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Decyduje o dostępie do pojedynczego odczytu na podstawie tego, czy jest zweryfikowany.
  9.  *
  10.  * Tak jak FineVoter: atrybutów można używać w setEntityPermission() i Action::setPermission(),
  11.  * a logika ról siedzi w RequestLogScopeResolver.
  12.  */
  13. final class RequestLogVoter extends Voter
  14. {
  15.     public const VIEW = 'REQUEST_LOG_VIEW';
  16.     public const EDIT = 'REQUEST_LOG_EDIT';
  17.     public const VERIFY = 'REQUEST_LOG_VERIFY';
  18.     public const ATTRIBUTES = [self::VIEW, self::EDIT, self::VERIFY];
  19.     private const VERB_BY_ATTRIBUTE = [
  20.         self::VIEW => RequestLogScopeResolver::VERB_VIEW,
  21.         self::EDIT => RequestLogScopeResolver::VERB_EDIT,
  22.         self::VERIFY => RequestLogScopeResolver::VERB_VERIFY,
  23.     ];
  24.     public function __construct(private readonly RequestLogScopeResolver $scopeResolver)
  25.     {
  26.     }
  27.     protected function supports(string $attribute, $subject): bool
  28.     {
  29.         return isset(self::VERB_BY_ATTRIBUTE[$attribute]);
  30.     }
  31.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  32.     {
  33.         $verb = self::VERB_BY_ATTRIBUTE[$attribute];
  34.         if ($subject instanceof RequestLog) {
  35.             return $this->scopeResolver->can(
  36.                 $verb,
  37.                 RequestLogScopeResolver::scopeFor($subject->getIsVerified())
  38.             );
  39.         }
  40.         return $this->scopeResolver->canAnywhere($verb);
  41.     }
  42. }