src/TSMS/TradeBundle/Security/Voter/CanAddFileVoter.php line 19

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