src/Security/CompanyVoter.php line 14

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