src/Security/Voter/UserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. class UserVoter extends Voter
  8. {
  9.     const MANAGE 'manage';
  10.     public function __construct(private readonly Security $security)
  11.     {
  12.     }
  13.     protected function supports(string $attribute$subject): bool
  14.     {
  15.         if ($attribute !== self::MANAGE) {
  16.             return false;
  17.         }
  18.         if (!$subject instanceof User) {
  19.             return false;
  20.         }
  21.         return true;
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof User) {
  27.             // the user must be logged in; if not, deny access
  28.             return false;
  29.         }
  30.         if($this->security->isGranted('ROLE_SUPPORT')) {
  31.             return true;
  32.         }
  33.         /** @var User $patient */
  34.         $patient $subject;
  35.         if ($this->security->isGranted('ROLE_DOCTOR') || $this->security->isGranted('ROLE_ESTABLISHMENT_ADMIN')) {
  36.             return $user->getEstablishment()->getId() === $patient->getEstablishment()->getId();
  37.         }
  38.         return false;
  39.     }
  40. }