src/Security/Voter/DeductionVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Deductions;
  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 DeductionVoter 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::EDIT])
  26.             && $subject instanceof \App\Entity\Deductions;
  27.     }
  28.     // protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  29.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  30.     {
  31.         $user $token->getUser();
  32.         /** @var Deductions $deduction */
  33.         $deduction $subject;
  34.         // if the user is anonymous, do not grant access
  35.         if (!$user instanceof UserInterface) {
  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.                     if ($deduction->getDeductionType() == Deductions::DEDUCTION_TYPE_DEDUCTION) {
  57.                         if ($deduction->getOrderID() == null) {
  58.                             return in_array($deduction->getRestaurantId()->getCityId()->getId(), $cityIds);
  59.                         } else {
  60.                             return in_array($deduction->getOrderID()->getCityId()->getId(), $cityIds);
  61.                         }
  62.                     }
  63.                 }
  64.                 break;
  65.         }
  66.         return false;
  67.     }
  68. }