src/Security/ClientVoter.php line 12

Open in your IDE?
  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 $decisionManager;
  25.     /**
  26.      * @var EntityManager|null
  27.      */
  28.     protected $entityManager;
  29.     /**
  30.      * @var EntityManager|null
  31.      */
  32.     protected $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)
  49.     {
  50.         // if the attribute isn't one we support, return false
  51.         if (!in_array($attributeself::ACCESS)) {
  52.             return false;
  53.         }
  54.         return true;
  55.     }
  56.     /**
  57.      * If you return true from supports(), then this method is called. Your job is simple: return true to allow access 
  58.      * and false to deny access
  59.      */
  60.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  61.     {
  62.         $user $token->getUser();
  63.         if (!$user instanceof Users) {
  64.             return false// the user must be logged in; if not, deny access
  65.         }
  66.         
  67.         // ROLE_SUPER_ADMIN can do anything! The power! Calling decide() on the AccessDecisionManager is essentially the same
  68.         // as calling isGranted() from a controller or other places (it's just a little lower-level, which is necessary for a voter).
  69.         if ($this->decisionManager->decide($token, ['ROLE_SUPER_ADMIN'])) {
  70.             return true;
  71.         }
  72.         // you know $subject is a VehicleClient object, thanks to supports
  73.         /** @var VehicleClients $vehicleClient */
  74.         $vehicleClient $subject;
  75.         switch ($attribute) {
  76.             case self::ACCESS['client_vehicles_list']:
  77.                 return $vehicleClient->getUser() == $user || $this->isDelegated($vehicleClient$user) || $this->decisionManager->decide($token, ['ROLE_ADMIN']);
  78.             case self::ACCESS['client_view_delegations']:
  79.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']);
  80.             case self::ACCESS['client_add_delegations']:
  81.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']);
  82.             case self::ACCESS['client_transactions']:
  83.             case self::ACCESS['client_bonds']:
  84.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']);
  85.             case self::ACCESS['client_bidding']:
  86.                 return $vehicleClient->getUser() == $user || $this->isDelegated($vehicleClient$user);
  87.             case self::ACCESS['client_current_auction_deposit']:
  88.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']);
  89.             case self::ACCESS['client_financial']:
  90.                 return $this->decisionManager->decide($token, ['ROLE_ACCOUNTANT']);
  91.         }
  92.         throw new \LogicException('This code should not be reached!');
  93.     }
  94.     /**
  95.      */
  96.     private function isDelegated(VehicleClients $delegatorUsers $user)
  97.     {
  98. //        $em = $this->entityManager;
  99. //        $delegations = $em->getRepository(ClientsDelegation::class)->findByDelegated($user->getUserId());
  100.         $delegations$this->userHelper->getUserDelegators($user);
  101.         foreach($delegations as $delegation){
  102.             if($delegation->getDelegator() && 
  103.                     ($delegation->getDelegator()->getCompany()->getCompanyId() == $delegator->getClientId()
  104.                         || $delegation->getDelegator()->getUser()  == $delegator->getClientId() )
  105.                     ){
  106.                 return true;
  107.             }
  108.         }
  109.         return false;
  110.     }
  111.     /**
  112.      */
  113.     private function canView(Post $postUser $user)
  114.     {
  115.         // if they can edit, they can view
  116.         if ($this->canEdit($post$user)) {
  117.             return true;
  118.         }
  119.         // the Post object could have, for example, a method isPrivate()
  120.         // that checks a boolean $private property
  121.         return !$post->isPrivate();
  122.     }
  123. }