src/Security/Voter/CallVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Call;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class CallVoter extends Voter
  10. {
  11.     private const ATTRIBUTES = ['UPDATE''READ_RECORDING'];
  12.     private Security $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     /**
  18.      * @param mixed $subject
  19.      * @return bool
  20.      */
  21.     protected function supports(string $attribute$subject)
  22.     {
  23.         return ($subject instanceof Call);
  24.     }
  25.     /**
  26.      * @param Call $subject
  27.      * @return bool
  28.      */
  29.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  30.     {
  31.         if (!in_array($attributeself::ATTRIBUTEStrue)) {
  32.             return false;
  33.         }
  34.         /** @var User $currentUser */
  35.         $currentUser $token->getUser();
  36.         if (!$currentUser instanceof User) {
  37.             return false;
  38.         }
  39.         if ($attribute === 'READ_RECORDING') {
  40.             return (
  41.                 ($subject->getUser() !== null
  42.                     && $subject->getUser()->getId() === $currentUser->getId()
  43.                 )
  44.                 || $subject->getStatus() === Call::STATUS_VOICEMAIL
  45.                 || $this->security->isGranted('ROLE_SUPERVISOR')
  46.             );
  47.         } elseif ($attribute === 'UPDATE') {
  48.             return (
  49.                 $subject->getUser() === null
  50.                 || $subject->getUser()->getId() === $currentUser->getId()
  51.                 || $this->security->isGranted('ROLE_SUPERVISOR')
  52.             );
  53.         }
  54.         
  55.         // unreachable
  56.         return false;
  57.     }
  58. }