src/Security/Voter/UserVoter.php line 15

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