src/TSMS/TradeBundle/Security/Voter/CanEditDeleteFolderVoter.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\User;
  13. /**
  14.  * Class CanEditDeleteFolderVoter.
  15.  */
  16. class CanEditDeleteFolderVoter implements VoterInterface
  17. {
  18.     const DOCUMENT_CAN_EDIT_DELETE_FOLDER 'DOCUMENT_CAN_EDIT_DELETE_FOLDER';
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public function supportsAttribute($attribute)
  23.     {
  24.         return in_array($attribute, [self::DOCUMENT_CAN_EDIT_DELETE_FOLDER]);
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function supportsClass($class)
  30.     {
  31.         $supportedClass 'TSMS\TradeBundle\Entity\DocumentFolder';
  32.         return $supportedClass === $class || is_subclass_of($class$supportedClass);
  33.     }
  34.     /**
  35.      * @param TokenInterface $token
  36.      * @param $folder
  37.      * @param array $attributes
  38.      *
  39.      * @return int
  40.      */
  41.     public function vote(TokenInterface $token$folder, array $attributes)
  42.     {
  43.         if (null === $folder) {
  44.             return VoterInterface::ACCESS_ABSTAIN;
  45.         }
  46.         // If $file is no a file, skip.
  47.         if (!$this->supportsClass(get_class($folder))) {
  48.             return VoterInterface::ACCESS_ABSTAIN;
  49.         }
  50.         if ($folder->getParentId() === 0) {
  51.             return VoterInterface::ACCESS_DENIED;
  52.         }
  53.         /** @var User $user */
  54.         $user $token->getUser();
  55.         // If user is super-administrator, deletion is granted
  56.         if ($user->hasRole('ROLE_ADMIN_TSMS') || $user->hasRole('ROLE_ADMIN_PERIMETER')) {
  57.             return VoterInterface::ACCESS_GRANTED;
  58.         }
  59.         $authorId $folder->getMetadataValue('author_id');
  60.         if ((int) $authorId === $user->getId()) {
  61.             return VoterInterface::ACCESS_GRANTED;
  62.         }
  63.         return VoterInterface::ACCESS_DENIED;
  64.     }
  65. }