src/TSMS/TradeBundle/Security/Voter/CanWriteInFolderVoter.php line 25

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