src/TSMS/AdminBundle/Security/Authorization/Voter/ContractVoter.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT.
  8. */
  9. namespace TSMS\AdminBundle\Security\Authorization\Voter;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  12. use TSMS\CoreBundle\Entity\Contract;
  13. use TSMS\CoreBundle\Entity\User;
  14. use TSMS\CoreBundle\Manager\PerimeterManager;
  15. class ContractVoter implements VoterInterface
  16. {
  17.     /**
  18.      * @var PerimeterManager
  19.      */
  20.     private $perimeterManager;
  21.     /**
  22.      * ContractVoter constructor.
  23.      *
  24.      * @param PerimeterManager $perimeterManager
  25.      */
  26.     public function __construct(PerimeterManager $perimeterManager)
  27.     {
  28.         $this->perimeterManager $perimeterManager;
  29.     }
  30.     const VIEW 'view';
  31.     /**
  32.      * @param string $attribute
  33.      *
  34.      * @return bool
  35.      */
  36.     public function supportsAttribute($attribute)
  37.     {
  38.         return self::VIEW === $attribute;
  39.     }
  40.     /**
  41.      * @param string $class
  42.      *
  43.      * @return bool
  44.      */
  45.     public function supportsClass($class)
  46.     {
  47.         $supportedClass 'TSMS\CoreBundle\Entity\Contract';
  48.         return $supportedClass === $class || is_subclass_of($class$supportedClass);
  49.     }
  50.     /**
  51.      * @param TokenInterface $token
  52.      * @param Contract       $contract
  53.      * @param array          $attributes
  54.      */
  55.     public function vote(TokenInterface $token$contract, array $attributes)
  56.     {
  57.         if (
  58.             !is_object($contract) ||
  59.             (null !== $contract && !$this->supportsClass(get_class($contract)))
  60.         ) {
  61.             return VoterInterface::ACCESS_ABSTAIN;
  62.         }
  63.         if (!== count($attributes)) {
  64.             throw new \InvalidArgumentException(
  65.                 'Only one attribute is allowed for VIEW'
  66.             );
  67.         }
  68.         $attribute $attributes[0];
  69.         if (!$this->supportsAttribute($attribute)) {
  70.             return VoterInterface::ACCESS_ABSTAIN;
  71.         }
  72.         /** @var User $user */
  73.         $user                  $token->getUser();
  74.         $administeredContracts $user->getContracts();
  75.         $userPerimeters        $user->getPerimeters();
  76.         if ($userPerimeters) {
  77.             $perimeterManager $this->perimeterManager;
  78.             $perimeters = [];
  79.             foreach ($userPerimeters as $perimeter) {
  80.                 $perimeters array_merge($perimeters$this->perimeterManager->getChildrenPerimeterCodes($perimeter));
  81.                 $perimeters[] = $perimeter->getPerimeterCode();
  82.             }
  83.             $administeredContracts array_filter(
  84.                 $administeredContracts->toArray(),
  85.                 function (Contract $contract) use ($perimeters) {
  86.                     // check if perimeter not found
  87.                     if (null == $contract->getPerimeter()) {
  88.                         return false;
  89.                     }
  90.                     return in_array($contract->getPerimeter()->getPerimeterCode(), $perimeters);
  91.                 }
  92.             );
  93.         }
  94.         foreach ($administeredContracts as $administeredContract) {
  95.             if ($administeredContract->getId() === $contract->getId()) {
  96.                 return VoterInterface::ACCESS_GRANTED;
  97.             }
  98.         }
  99.         return VoterInterface::ACCESS_DENIED;
  100.     }
  101. }