src/Security/Voter/TokenVoter.php line 17

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Action;
  4. use App\Entity\UserRole;
  5. use App\Entity\EntityName;
  6. use App\Entity\Permission;
  7. use App\Entity\Refill;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Bundle\SecurityBundle\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. class TokenVoter extends Voter
  15. {
  16.     private $security$actions$doctrine$em;
  17.     public function __construct(Security $securityManagerRegistry $doctrine)
  18.     {
  19.         $this->em $doctrine->getManager();
  20.         // TODO: (whole project) only use $doctrine->getRepository or $em->getRepository, not both!!! Make cleaner!!!!!
  21.         $actionObjectList $this->em->getRepository(Action::class)->findAll();
  22.         $this->actions = array();    // [NEW, EDIT, ...]. It was CRUD_*action* but this is better
  23.         $this->security $security;
  24.         $this->doctrine $doctrine;
  25.         foreach ($actionObjectList as $action)
  26.         {
  27.             array_push($this->actions$action->getName());
  28.         }
  29.     }
  30.     
  31.     protected function supports(string $attribute$subject): bool
  32.     {
  33.         return in_array($attribute$this->actions);
  34.     }
  35.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  36.     {
  37.         return true;
  38.         
  39.         // TODO review code for user without control of refill
  40.         $user $token->getUser();
  41.         if (in_array('ROLE_ADMIN'$user->getRoles())) return true;
  42.         // Need to get the EntityName ID from $subject (new CategoryReport())
  43.         // Need to get the Action's ID from $attribute ("LIST")
  44.     
  45.         //    Need to check the right refill object, given the user ID (channel ID taken from user), the entity it's trying to access and what action they want to do...
  46.         //    I can get the entity AND action from denyAccessUnlessGranted in the controller, user from $token->getUser. I've got everything!!
  47.         $refillRepo $this->em->getRepository(Refill::class);
  48.         $entityNameRepo $this->em->getRepository(EntityName::class);
  49.         $actionRepo $this->em->getRepository(Action::class);
  50.         $userID $user->getId();
  51.         $entityID $entityNameRepo->findOneBy(['name' => $subject->getEntityName()])->getId();
  52.         $actionID $actionRepo->findOneBy(['name' => $attribute])->getId();
  53.         $refill $refillRepo->findRefillToUse($entityID$actionID$userID);
  54.         
  55.         // If I don't find a user-specific refill and the user is in a channel
  56.         if (!$refill && $user->getChannel())
  57.         {
  58.             $refill $refillRepo->findRefillToUse($entityID$actionIDnull$user->getChannel()->getId());
  59.         }
  60.         if ($refill)
  61.         {
  62.             $refill->setUsed($refill->getUsed() + 1);
  63.             $this->em->persist($refill);
  64.             $this->em->flush();
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69. }