src/TSMS/TradeBundle/Security/Voter/CanSeeFolderVoter.php line 23

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\TradeBundle\Security\Voter;
  10. use Doctrine\ORM\EntityManager;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  13. use TSMS\CoreBundle\Entity\EserviceFamily;
  14. use TSMS\CoreBundle\Entity\EserviceOption;
  15. use TSMS\CoreBundle\Manager\ContractManager;
  16. use TSMS\CoreBundle\Manager\ContractManagerInterface;
  17. use TSMS\CoreBundle\Entity\Eservice;
  18. use TSMS\CoreBundle\Entity\User;
  19. use TSMS\TradeBundle\Entity\Folder;
  20. class CanSeeFolderVoter implements VoterInterface
  21. {
  22.     const DOCUMENT_CAN_SEE_FOLDER 'DOCUMENT_CAN_SEE_FOLDER';
  23.     /**
  24.      * @var EntityManager
  25.      */
  26.     private $em;
  27.     /**
  28.      * @param EntityManager $em
  29.      */
  30.     public function __construct(EntityManager $em)
  31.     {
  32.         $this->em $em;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function supportsAttribute($attribute)
  38.     {
  39.         return in_array($attribute, [self::DOCUMENT_CAN_SEE_FOLDER]);
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function supportsClass($class)
  45.     {
  46.         $supportedClass 'TSMS\TradeBundle\Entity\Folder';
  47.         return $supportedClass === $class || is_subclass_of($class$supportedClass);
  48.     }
  49.     /**
  50.      * @param TokenInterface $token
  51.      * @param null|Folder    $folder
  52.      * @param array          $attributes
  53.      *
  54.      * @return int
  55.      */
  56.     public function vote(TokenInterface $token$folder, array $attributes)
  57.     {
  58.         // check if the class of this object is supported by this voter
  59.         if (
  60.             is_array($folder) ||
  61.             (null !== $folder && !$this->supportsClass(get_class($folder)))
  62.         ) {
  63.             return VoterInterface::ACCESS_ABSTAIN;
  64.         }
  65.         // check if the voter is used correctly, only allow one attribute
  66.         // this isn't a requirement, it's just one easy way for you to
  67.         // design your voter
  68.         if (!== count($attributes)) {
  69.             throw new \InvalidArgumentException(
  70.                 'Only one attribute is allowed for DOCUMENT_CAN_SEE_FOLDER'
  71.             );
  72.         }
  73.         // set the attribute to check against
  74.         $attribute $attributes[0];
  75.         // check if the given attribute is covered by this voter
  76.         if (!$this->supportsAttribute($attribute)) {
  77.             return VoterInterface::ACCESS_ABSTAIN;
  78.         }
  79.         // get current logged in user
  80.         $user $token->getUser();
  81.         // make sure there is a user object (i.e. that the user is logged in)
  82.         if (!$user instanceof User) {
  83.             return VoterInterface::ACCESS_DENIED;
  84.         }
  85.         switch ($attribute) {
  86.             case self::DOCUMENT_CAN_SEE_FOLDER:
  87.                $eserviceOptionRepository $this->em->getRepository(EserviceOption::class);
  88.                 $writeOption =  $eserviceOptionRepository
  89.                     ->getUserEServiceOptionByCodeAndFamily(
  90.                         $user ,
  91.                         EserviceFamily::DOC_ESERVICE_FAMILY,
  92.                         "W_".$folder->getEservice());
  93.                 if ($writeOption !==null) {
  94.                     return VoterInterface::ACCESS_GRANTED;
  95.                 }
  96.                 $readOption =  $eserviceOptionRepository
  97.                     ->getUserEServiceOptionByCodeAndFamily(
  98.                         $user ,
  99.                         EserviceFamily::DOC_ESERVICE_FAMILY,
  100.                         "R_".$folder->getEservice());
  101.                 if ($readOption !==null) {
  102.                     return VoterInterface::ACCESS_GRANTED;
  103.                 }
  104.                 break;
  105.         }
  106.         return VoterInterface::ACCESS_DENIED;
  107.     }
  108. }