vendor/datenwerk/ginger-bundle/Security/FileImportVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace DW\GingerBundle\Security;
  3. use DW\GingerBundle\Entity\User;
  4. use DW\GingerBundle\Import\Entity\FileImport;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class FileImportVoter extends Voter
  9. {
  10.     const CREATE 'create';
  11.     const VIEW 'view';
  12.     const EDIT 'edit';
  13.     const DELETE 'delete';
  14.     const LAUNCH 'launch';
  15.     /**
  16.      * @var AccessDecisionManagerInterface
  17.      */
  18.     private AccessDecisionManagerInterface $decisionManager;
  19.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  20.     {
  21.         $this->decisionManager $decisionManager;
  22.     }
  23.     /**
  24.      * {@inheritDoc}
  25.      */
  26.     protected function supports($attribute$subject): bool
  27.     {
  28.         // if the attribute isn't one we support, return false
  29.         if (!in_array($attribute, array(self::VIEWself::EDITself::CREATEself::DELETEself::LAUNCH))) {
  30.             return false;
  31.         }
  32.         // only vote on fileimport objects inside this voter
  33.         if (!$subject instanceof FileImport) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     /**
  39.      * {@inheritDoc}
  40.      */
  41.     protected function voteOnAttribute($attribute$fileImportTokenInterface $token): bool
  42.     {
  43.         /**
  44.          * @var FileImport $fileImport
  45.          */
  46.         $user $token->getUser();
  47.         if (!$user instanceof User) {
  48.             // the user must be logged in; if not, deny access
  49.             return false;
  50.         }
  51.         switch ($attribute) {
  52.             case self::CREATE:
  53.                 return $this->decisionManager->decide($token, ['ROLE_GINGER_FILE_IMPORT_EDITOR']);
  54.             case self::VIEW:
  55.             case self::EDIT:
  56.             case self::DELETE:
  57.             case self::LAUNCH:
  58.                 return
  59.                     $this->decisionManager->decide($token, ['ROLE_GINGER_FILE_IMPORT_ADMIN'])
  60.                     || $user === $fileImport->getOwner();
  61.         }
  62.         throw new \LogicException('This code should not be reached!');
  63.     }
  64. }