src/Security/Voter/ToolFileVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\ToolFile;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ToolFileVoter extends Voter
  9. {
  10.     public const DOWNLOAD 'download';
  11.     public const DELETE 'delete';
  12.     /**
  13.      * @param string $attribute
  14.      * @param mixed  $subject
  15.      *
  16.      * @return bool
  17.      */
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         $supports true;
  21.         if (!\in_array($attribute, [self::DOWNLOADself::DELETE], true)) {
  22.             $supports false;
  23.         }
  24.         if (!$subject instanceof ToolFile) {
  25.             $supports false;
  26.         }
  27.         return $supports;
  28.     }
  29.     /**
  30.      * @param string         $attribute
  31.      * @param mixed          $subject
  32.      * @param TokenInterface $token
  33.      *
  34.      * @return bool
  35.      */
  36.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  37.     {
  38.         $user $token->getUser();
  39.         $hasVotePassed false;
  40.         if (!$user instanceof User) {
  41.             return $hasVotePassed;
  42.         }
  43.         /** @var ToolFile $toolFile */
  44.         $toolFile $subject;
  45.         switch ($attribute) {
  46.             case self::DOWNLOAD:
  47.                 $hasVotePassed =  $this->canDownload($toolFile$user);
  48.                 break;
  49.             case self::DELETE:
  50.                 $hasVotePassed =  $this->canDelete($toolFile$user);
  51.                 break;
  52.         }
  53.         return $hasVotePassed;
  54.     }
  55.     /**
  56.      * @param ToolFile $toolFile
  57.      * @param User        $user
  58.      *
  59.      * @return bool
  60.      */
  61.     public function canDownload(ToolFile $toolFileUser $user): bool
  62.     {
  63.         if ($user->isSystemUser()) {
  64.             return $toolFile->getUser()->isSystemUser() && ($toolFile->getStatus() !== ToolFile::$STATUS_DELETED);
  65.         }
  66.         return ($toolFile->isSystemFile() || ($toolFile->getUser()->getOrganization()->getId() === $user->getOrganization()->getId()))
  67.             && ($toolFile->getStatus() !== ToolFile::$STATUS_DELETED);
  68.     }
  69.     /**
  70.      * @param ToolFile $toolFile
  71.      * @param User        $user
  72.      *
  73.      * @return bool
  74.      */
  75.     public function canDelete(ToolFile $toolFileUser $user): bool
  76.     {
  77.         if ($user->isSystemUser()) {
  78.             return $toolFile->isSystemFile() && ($toolFile->getStatus() !== ToolFile::$STATUS_DELETED);
  79.         }
  80.         return $toolFile->isRegularUserFile($user) && ($toolFile->getStatus() !== ToolFile::$STATUS_DELETED);
  81.     }
  82. }