src/Infrastructure/Security/Voter/ToastSessionLinkVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\Security\Voter;
  3. use App\Database\Domain\Entity\Sessions\ToastSessionLink;
  4. use App\Database\Domain\Entity\User\User;
  5. use App\Database\Domain\Repository\CollaboratedSessionRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ToastSessionLinkVoter extends Voter
  9. {
  10.     public const ACCESS 'toast_session_link.access';
  11.     public const ACCESS_WITH_COLLABORATORS 'toast_session_link.access_with_collaborators';
  12.     private CollaboratedSessionRepository $collaboratedSessionRepository;
  13.     public function __construct(CollaboratedSessionRepository $collaboratedSessionRepository)
  14.     {
  15.         $this->collaboratedSessionRepository $collaboratedSessionRepository;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         return $attribute === self::ACCESS || $attribute === self::ACCESS_WITH_COLLABORATORS;
  20.     }
  21.     /**
  22.      * @param ToastSessionLink $subject
  23.      */
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         /** @var User $user */
  27.         $user $token->getUser();
  28.         if (!$user instanceof User) {
  29.             return false;
  30.         }
  31.         if ($attribute === self::ACCESS) {
  32.             return $user->getId() === $subject->getSession()->getUser()->getId();
  33.         }
  34.         if ($attribute === self::ACCESS_WITH_COLLABORATORS) {
  35.             $collaboratedSession $this->collaboratedSessionRepository->findBy(['session' => $subject->getSession(), 'collaboratedUser' => $user]);
  36.             if (null !== $collaboratedSession) {
  37.                 return true;
  38.             }
  39.             return $user->getId() === $subject->getSession()->getUser()->getId();
  40.         }
  41.         if ($attribute === self::ACCESS_WITH_COLLABORATORS) {
  42.             return $user->getId() === $subject->getSession()->getUser()->getId();
  43.         }
  44.         return false;
  45.     }
  46. }