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