src/Security/Voter/OrderVoter.php line 14

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Orders;
  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. class OrderVoter extends Voter
  12. {
  13.     private $em;
  14.     // these strings are just invented: you can use anything
  15.     const VIEW 'view';
  16.     const EDIT 'edit';
  17.     public function __construct(protected Security $security,protected ManagerRegistry $doctrine)
  18.     {
  19.         $this->em $this->doctrine->getManager();
  20.     }
  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::VIEW])
  27.             && $subject instanceof \App\Entity\Orders;
  28.     }
  29.     // protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  30.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  31.     {
  32. //        dump("ddd");
  33. //        die();
  34.         $user $token->getUser();
  35.         /** @var Orders $order */
  36.         $order $subject;
  37.         // if the user is anonymous, do not grant access
  38.         if (!$user instanceof UserInterface || $order->getDeleted()) {
  39.             return false;
  40.         }
  41.         // ROLE_ADMIN can do anything! The power!
  42.         if ($this->security->isGranted('ROLE_ADMIN')) {
  43.             return true;
  44.         }
  45.         // ... (check conditions and return true to grant permission) ...
  46.         switch ($attribute) {
  47.             case self::VIEW:
  48.                 if ($this->security->isGranted('ROLE_CONTRACTOR')) {
  49.                     $cityIds = [];
  50.                     $city $this->em->getRepository(City::class)->findOneBy(['contractor' => $user->getId()]);
  51.                     if ($city->getSuburb() == null) {
  52.                         $cityIdStr $this->em->getRepository(City::class)->getSubCityIds($city->getId());
  53.                         $cityIds = [];
  54.                         if ($cityIdStr) {
  55.                             $cityIds explode(','$cityIdStr);
  56.                         }
  57.                     }
  58.                     array_push($cityIds$city->getId());
  59.                     return in_array($order->getCityId()->getId(), $cityIds);
  60.                 }
  61.                 elseif ($this->security->isGranted('ROLE_OWNER')) {
  62.                     $restaurants $this->em->getRepository(Restaurants::class)->findOneBy(['userId'=>$user->getId()]);
  63.                     if($restaurants!=null)
  64.                     {
  65.                         return $order->getRestaurantId()->getId() === $restaurants->getId();
  66.                     }
  67.                 }
  68.                 break;
  69.         }
  70.         return false;
  71.     }
  72. }