src/Security/ClientVoter.php line 12

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