src/Security/Voter/API/Crypto/SignedPreKey/ViewVoter.php line 13

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\API\Crypto\SignedPreKey;
  4. use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
  5. use App\Data\Crypto\Key\SignedPreKey;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ViewVoter extends Voter
  9. {
  10.     protected function supports(string $attributemixed $subject): bool
  11.     {
  12.         return $attribute === 'VIEW_SIGNED_PRE_KEYS';
  13.     }
  14.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  15.     {
  16.         /** @var ?SignedPreKey[] $items */
  17.         $items null;
  18.         if ($subject instanceof SignedPreKey) {
  19.             $items = [$subject];
  20.         }
  21.         else if ($subject instanceof Paginator) {
  22.             $items = [];
  23.             foreach ($subject as $item) {
  24.                 $items[] = $item;
  25.             }
  26.         }
  27.         if ($items !== null) {
  28.             foreach ($items as $key) {
  29.                 if ($key->getUser() !== $token->getUser()) {
  30.                     return false;
  31.                 }
  32.             }
  33.             return  true;
  34.         }
  35.         return false;
  36.     }
  37. }