src/Security/Voter/CursusDriverVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\ChannelUserData\Cursus\AbstractCursus;
  4. use App\Entity\ChannelUserData\Cursus\FileAbstractCursus;
  5. use Nellapp\Bundle\SDKBundle\Permission\Enum\ChannelUserPermissionEnum;
  6. use Nellapp\Bundle\SDKBundle\Permission\Security\Voter\UserOwnerVoter;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class CursusDriverVoter extends Voter
  11. {
  12.     public const VIEW 'view';
  13.     public const DELETE 'delete';
  14.     public const PUT 'put';
  15.     public const POST 'post';
  16.     public const DOWNLOAD 'download';
  17.     public function __construct(
  18.         private Security $security,
  19.     ) {}
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         return ($subject instanceof AbstractCursus || $subject instanceof FileAbstractCursus) && in_array($attribute, [self::VIEWself::DELETEself::PUTself::POSTself::DOWNLOAD]);
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         if ($subject instanceof AbstractCursus) {
  27.             $cursus $subject;
  28.             $file null;
  29.         } else {
  30.             $cursus $subject->getAbstractCursus();
  31.             $file $subject;
  32.         }
  33.         $channel $cursus->getChannel();
  34.         $isFileOwner $file !== null && $this->security->isGranted(UserOwnerVoter::USER_OWNER$file);
  35.         $isDriveOwner $this->security->isGranted(UserOwnerVoter::USER_OWNER$cursus);
  36.         if ($attribute === self::VIEW) {
  37.             return
  38.                 $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DSA_SHOW_DRIVE$channel)
  39.                 || $isDriveOwner && (!($file !== null) || $file->isShare())
  40.                 ;
  41.         } else if ($attribute === self::DELETE) {
  42.             return
  43.                 $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DRIVE_DELETE$channel)
  44.                 || $isFileOwner
  45.                 ;
  46.         } else if ($attribute === self::PUT) {
  47.             return
  48.                 $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DRIVE_UPDATE$channel)
  49.                 || $isFileOwner
  50.                 ;
  51.         } else if ($attribute === self::POST) {
  52.             return
  53.                 $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DRIVE_IMPORT$channel)
  54.                 || $isDriveOwner
  55.                 ;
  56.         } else if ($attribute === self::DOWNLOAD) {
  57.             return
  58.                 $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DRIVE_DOWNLOAD$channel)
  59.                 || $isDriveOwner && $file?->isShare()
  60.                 ;
  61.         }
  62.         return false;
  63.     }
  64. }