src/Security/Voter/MenuSectionVoter.php line 15

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