src/Common/Security/Voter/DossierVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace PaperKite\Common\Security\Voter;
  3. use PaperKite\Common\Service\DossierReaderService;
  4. use PaperKite\EmployeeApi\Entity\EmployeeInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class DossierVoter extends Voter
  10. {
  11.     public const EDIT 'DOSSIER_EDIT';
  12.     public const VIEW 'DOSSIER_VIEW';
  13.     public function __construct(
  14.         private Security $security,
  15.         private DossierReaderService $dossierReaderService,
  16.     ) {
  17.     }
  18.     protected function supports(string $attribute$subject): bool
  19.     {
  20.         // https://symfony.com/doc/current/security/voters.html
  21.         return in_array($attribute, [self::EDITself::VIEW]);
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         /** @var EmployeeInterface $user */
  26.         $user $token->getUser();
  27.         // if the user is anonymous, do not grant access
  28.         if (!$user instanceof UserInterface) {
  29.             return false;
  30.         }
  31.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  32.             return true;
  33.         }
  34.         $dossier $this->dossierReaderService->getById($subject);
  35.         return self::EDIT === $attribute && $dossier->getAssignedEmployee() === $user->getUserIdentifier();
  36.     }
  37. }