src/Security/Voter/File/ShowVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\File;
  3. use App\Entity\Account\User;
  4. use App\Entity\ChannelUserData\ChannelUserData;
  5. use App\Entity\Chat\File;
  6. use App\Security\Voter\AbstractVoter;
  7. use App\Service\Account\UserService;
  8. use Symfony\Component\Security\Core\Security;
  9. class ShowVoter extends AbstractVoter
  10. {
  11.     public function __construct(
  12.         private Security $security,
  13.         UserService      $userService,
  14.     )
  15.     {
  16.         parent::__construct($userService);
  17.     }
  18.     protected function supportedAttribute(): string
  19.     {
  20.         return 'NIM_VIEW';
  21.     }
  22.     /**
  23.      * @param User $user
  24.      * @param File $subject
  25.      * @param ChannelUserData $channelUserData
  26.      * @return int
  27.      */
  28.     protected function grant(User $user$subjectChannelUserData $channelUserData): int
  29.     {
  30.         $conversation $subject->getFileMessage()?->getConversation();
  31.         if ($conversation) {
  32.             return $this->security->isGranted('NIM_VIEW'$conversation) ? self::ACCESS_GRANTED self::ACCESS_DENIED;
  33.         }
  34.         return self::ACCESS_DENIED;
  35.     }
  36.     /**
  37.      * @param User $user
  38.      * @param File $subject
  39.      * @return ChannelUserData|null
  40.      */
  41.     protected function getChannelUserData(User $user$subject): ?ChannelUserData
  42.     {
  43.         return $user->getChannelUserDataByChannel($subject->getChannel());
  44.     }
  45.     protected function supportSubject($subject): bool
  46.     {
  47.         return $subject instanceof File;
  48.     }
  49. }