src/Security/CompanyVoter.php line 14

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Companies;
  4. use App\Entity\Users;
  5. use App\Entity\ClientsDelegation;
  6. use App\Entity\VehicleClients;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Service\UserHelper;
  12. class CompanyVoter extends Voter
  13. {
  14.     const ACCESS= [ 
  15.         'company_view_summary'  => 'company_view_summary',
  16.         'company_edit_info'     => 'company_edit_info',
  17.         'company_docs' => 'company_docs',
  18.         'company_bank_accounts' => 'company_bank_accounts'];
  19.     /**
  20.      * @var AccessDecisionManager|null
  21.      */
  22.     protected \Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface $decisionManager;
  23.     /**
  24.      * @var EntityManager|null
  25.      */
  26.     protected \Doctrine\ORM\EntityManagerInterface $entityManager;
  27.     /**
  28.      * @var EntityManager|null
  29.      */
  30.     protected \App\Service\UserHelper $userHelper;
  31.     /**
  32.      * DelegateVoter constructor.
  33.      * @param AccessDecisionManager|null $decisionManager
  34.      * @param EntityManager|null $entityManager
  35.      */
  36.     public function __construct(AccessDecisionManagerInterface $decisionManagerEntityManagerInterface $entityManagerUserHelper $userHelper)
  37.     {
  38.         $this->decisionManager $decisionManager;
  39.         $this->entityManager $entityManager;
  40.         $this->userHelper $userHelper;
  41.     }
  42.     /**
  43.      * determines if your voter should vote on the attribute/subject combination. If you return true,
  44.      * voteOnAttribute() will be called. Otherwise, your voter is done: some other voter should process this
  45.      */
  46.     protected function supports($attribute$subject): bool
  47.     {
  48.         // if the attribute isn't one we support, return false
  49.         if (!in_array($attributeself::ACCESS)) {
  50.             return false;
  51.         }
  52.         // only vote on Companies objects inside this voter
  53.         return $subject instanceof Companies;
  54.     }
  55.     /**
  56.      * If you return true from supports(), then this method is called. Your job is simple: return true to allow access
  57.      * and false to deny access
  58.      */
  59.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  60.     {
  61.         $user $token->getUser();
  62.         if (!$user instanceof Users) {
  63.             return false// the user must be logged in; if not, deny access
  64.         }
  65.         
  66.         // ROLE_SUPER_ADMIN can do anything! The power! Calling decide() on the AccessDecisionManager is essentially the same
  67.         // as calling isGranted() from a controller or other places (it's just a little lower-level, which is necessary for a voter).
  68.         if ($this->decisionManager->decide($token, ['ROLE_SUPER_ADMIN'])) {
  69.             return true;
  70.         }
  71.         // you know $subject is a VehicleClient object, thanks to supports
  72.         /** @var VehicleClients $vehicleClient */
  73.         $vehicleClient $subject;
  74.         switch ($attribute) {
  75.             case self::ACCESS['company_view_summary']:
  76.                 //Admins and delegated users can view
  77.                 return $this->isDelegated($vehicleClient$user) || $this->decisionManager->decide($token, ['ROLE_ADMIN'])|| $this->decisionManager->decide($token, ['ROLE_BENEFICIARY_REP']);
  78.             case self::ACCESS['company_edit_info']:
  79.             case self::ACCESS['company_docs']:
  80.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']); //only admins are allowed
  81.             case self::ACCESS['company_bank_accounts']:
  82.                 return $this->isDelegated($vehicleClient$user) || $this->decisionManager->decide($token, ['ROLE_ADMIN']);
  83.         }
  84.         throw new \LogicException('This code should not be reached!');
  85.     }
  86.     /**
  87.      */
  88.     private function isDelegated(Companies $delegatorUsers $user)
  89.     {
  90. //        $em = $this->entityManager;
  91. //        $delegations = $em->getRepository(ClientsDelegation::class)->findByDelegated($user->getUserId());
  92.         $delegations$this->userHelper->getUserDelegators($user);
  93.         foreach($delegations as $delegation){
  94.             if($delegation->getDelegator() && 
  95.                     $delegation->getDelegator()->getCompany()  == $delegator){
  96.                 return true;
  97.             }
  98.         }
  99.         return false;
  100.     }
  101. }