src/Security/Voter/BusinessVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Business;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. use App\Entity\City;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. class BusinessVoter extends Voter
  11. {
  12.     private $em;
  13.     // these strings are just invented: you can use anything
  14.     const VIEW 'view';
  15.     const EDIT 'edit';
  16.     public function __construct(protected Security $security, protected ManagerRegistry $doctrine)
  17.     {
  18.         $this->em $this->doctrine->getManager();
  19.     }
  20.     #https://symfony.com/doc/6.2/security/voters.html
  21.     // protected function supports($attribute, $subject)
  22.     protected function supports(string $attributemixed $subject): bool
  23.     {
  24.         // replace with your own logic
  25.         // https://symfony.com/doc/current/security/voters.html
  26.         return in_array($attribute, [self::EDITself::VIEW])
  27.             && $subject instanceof \App\Entity\Business;
  28.     }
  29.     // protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  30.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  31.     {
  32.         $user $token->getUser();
  33.         /** @var Business $business */
  34.         $business $subject;
  35.         // if the user is anonymous, do not grant access
  36.         if (!$user instanceof UserInterface || $business->getDeleted()) {
  37.             return false;
  38.         }
  39.         // ROLE_ADMIN can do anything! The power!
  40.         if ($this->security->isGranted('ROLE_ADMIN')) {
  41.             return true;
  42.         }
  43.         // ... (check conditions and return true to grant permission) ...
  44.         switch ($attribute) {
  45.             case self::EDIT:
  46.                 if ($this->security->isGranted('ROLE_CONTRACTOR')) {
  47.                     $cityIds = [];
  48.                     $city $this->em->getRepository(City::class)->findOneBy(['contractor' => $user->getId()]);
  49.                     if ($city->getSuburb() == null) {
  50.                         $cityIdStr $this->em->getRepository(City::class)->getSubCityIds($city->getId());
  51.                         $cityIds = [];
  52.                         if ($cityIdStr) {
  53.                             $cityIds explode(','$cityIdStr);
  54.                         }
  55.                     }
  56.                     array_push($cityIds$city->getId());
  57.                     return in_array($business->getCityId()->getId(), $cityIds);
  58.                 }
  59.                 break;
  60.             case self::VIEW:
  61.                 if ($this->security->isGranted('ROLE_CONTRACTOR')) {
  62.                     $cityIds = [];
  63.                     $city $this->em->getRepository(City::class)->findOneBy(['contractor' => $user->getId()]);
  64.                     if ($city->getSuburb() == null) {
  65.                         $cityIdStr $this->em->getRepository(City::class)->getSubCityIds($city->getId());
  66.                         $cityIds = [];
  67.                         if ($cityIdStr) {
  68.                             $cityIds explode(','$cityIdStr);
  69.                         }
  70.                     }
  71.                     array_push($cityIds$city->getId());
  72.                     return in_array($business->getCityId()->getId(), $cityIds);
  73.                 }
  74.                 break;
  75.         }
  76.         return false;
  77.     }
  78. }