src/TSMS/TradeBundle/Security/Voter/FolderTreeVoter.php line 16

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. /**@todo check use / delete  */
  13. class FolderTreeVoter implements VoterInterface
  14. {
  15.     const DOCUMENT_IS_FOLDER_TREE_CORRECT 'DOCUMENT_IS_FOLDER_TREE_CORRECT';
  16.     /**
  17.      * {@inheritdoc}
  18.      */
  19.     public function supportsAttribute($attribute)
  20.     {
  21.         return in_array($attribute, [self::DOCUMENT_IS_FOLDER_TREE_CORRECT]);
  22.     }
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public function supportsClass($class)
  27.     {
  28.         return is_array($class) && isset($class['foldersId']) && isset($class['folderPath']) && isset($class['contractId']);
  29.     }
  30.     /**
  31.      * @param TokenInterface $token
  32.      * @param null|object    $object
  33.      * @param array          $attributes
  34.      *
  35.      * @return int
  36.      */
  37.     public function vote(TokenInterface $token$object, array $attributes)
  38.     {
  39.         // check if the class of this object is supported by this voter
  40.         if (!$this->supportsClass($object)) {
  41.             return VoterInterface::ACCESS_ABSTAIN;
  42.         }
  43.         // check if the voter is used correctly, only allow one attribute
  44.         // this isn't a requirement, it's just one easy way for you to
  45.         // design your voter
  46.         if (!== count($attributes)) {
  47.             throw new \InvalidArgumentException(
  48.                 'Only one attribute is allowed for DOCUMENT_IS_FOLDER_TREE_CORRECT'
  49.             );
  50.         }
  51.         // set the attribute to check against
  52.         $attribute $attributes[0];
  53.         // check if the given attribute is covered by this voter
  54.         if (!$this->supportsAttribute($attribute)) {
  55.             return VoterInterface::ACCESS_ABSTAIN;
  56.         }
  57.         switch ($attribute) {
  58.             case self::DOCUMENT_IS_FOLDER_TREE_CORRECT:
  59.                 $isSameContract intval($object['contractId']) === intval(end($object['folderPath'])['contract']);
  60.                 $isTreeCorrect  true;
  61.                 foreach ($object['foldersId'] as $depth => $folderId) {
  62.                     if (
  63.                         !isset($object['folderPath'][$depth]) ||
  64.                         $object['folderPath'][$depth]['parentId'] !== $folderId
  65.                     ) {
  66.                         $isTreeCorrect false;
  67.                     }
  68.                 }
  69.                 if ($isSameContract && $isTreeCorrect) {
  70.                     return VoterInterface::ACCESS_GRANTED;
  71.                 }
  72.                 break;
  73.         }
  74.         return VoterInterface::ACCESS_DENIED;
  75.     }
  76. }