src/Security/Voter/RestaurantsVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Restaurants;
  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 RestaurantsVoter 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.     // protected function supports($attribute, $subject)
  21.     protected function supports(string $attributemixed $subject): bool
  22.     {
  23.         // replace with your own logic
  24.         // https://symfony.com/doc/current/security/voters.html
  25.         return in_array($attribute, [self::EDITself::VIEW])
  26.             && $subject instanceof \App\Entity\Restaurants;
  27.     }
  28.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  29.     // protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  30.     {
  31.         $user $token->getUser();
  32.         /** @var Restaurants $restaurants */
  33.         $restaurants $subject;
  34.         // if the user is anonymous, do not grant access
  35.         if (!$user instanceof UserInterface || $restaurants->getDeleted()) {
  36.             return false;
  37.         }
  38.         // ROLE_ADMIN can do anything! The power!
  39.         if ($this->security->isGranted('ROLE_ADMIN')) {
  40.             return true;
  41.         }
  42.         // ... (check conditions and return true to grant permission) ...
  43.         switch ($attribute) {
  44.             case self::EDIT:
  45.                 if ($this->security->isGranted('ROLE_CONTRACTOR')) {
  46.                     $cityIds = [];
  47.                     $city $this->em->getRepository(City::class)->findOneBy(['contractor' => $user->getId()]);
  48.                     if ($city->getSuburb() == null) {
  49.                         $cityIdStr $this->em->getRepository(City::class)->getSubCityIds($city->getId());
  50.                         $cityIds = [];
  51.                         if ($cityIdStr) {
  52.                             $cityIds explode(','$cityIdStr);
  53.                         }
  54.                     }
  55.                     array_push($cityIds$city->getId());
  56.                     return in_array($restaurants->getCityId()->getId(), $cityIds);
  57.                 } elseif ($this->security->isGranted('ROLE_OWNER')) {
  58.                     return $user === $restaurants->getUserId();
  59.                 }
  60.                 break;
  61.             case self::VIEW:
  62.                 if ($this->security->isGranted('ROLE_CONTRACTOR')) {
  63.                     $cityIds = [];
  64.                     $city $this->em->getRepository(City::class)->findOneBy(['contractor' => $user->getId()]);
  65.                     if ($city->getSuburb() == null) {
  66.                         $cityIdStr $this->em->getRepository(City::class)->getSubCityIds($city->getId());
  67.                         $cityIds = [];
  68.                         if ($cityIdStr) {
  69.                             $cityIds explode(','$cityIdStr);
  70.                         }
  71.                     }
  72.                     array_push($cityIds$city->getId());
  73.                     return in_array($restaurants->getCityId()->getId(), $cityIds);
  74.                 } elseif ($this->security->isGranted('ROLE_OWNER')) {
  75.                     return $user === $restaurants->getUserId();
  76.                 }
  77.                 break;
  78.         }
  79.         return false;
  80.     }
  81. }