src/Infrastructure/Security/Voter/ToastSessionVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\Security\Voter;
  3. use App\Database\Domain\Entity\Sessions\ToastSession;
  4. use App\Database\Domain\Entity\User\User;
  5. use App\Database\Domain\Repository\CollaboratedSessionRepository;
  6. use App\Database\Domain\Repository\ToastSessionRepository;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class ToastSessionVoter extends Voter
  10. {
  11.     public const ACCESS_BY_ROLE 'toast_session.access_by_role';
  12.     public const ACCESS 'toast_session.access';
  13.     public const ACCESS_WITH_COLLABORATORS 'toast_session.access_with_collaborators';
  14.     public const ACCESS_BY_IDS 'toast_session.access_by_ids';
  15.     private CollaboratedSessionRepository $collaboratedSessionRepository;
  16.     public function __construct(CollaboratedSessionRepository $collaboratedSessionRepository)
  17.     {
  18.         $this->collaboratedSessionRepository $collaboratedSessionRepository;
  19.     }
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         return in_array($attribute, [self::ACCESSself::ACCESS_BY_IDSself::ACCESS_WITH_COLLABORATORSself::ACCESS_BY_ROLE]);
  23.     }
  24.     /**
  25.      * @param ToastSession $subject
  26.      */
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  28.     {
  29.         /** @var User $user */
  30.         $user $token->getUser();
  31.         if (!$user instanceof User) {
  32.             return false;
  33.         }
  34.         if ($attribute === self::ACCESS_BY_ROLE) {
  35.             return $user->isFullUser() || $user->isPartialUser();
  36.         }
  37.         if ($attribute === self::ACCESS) {
  38.             return $user->getId() === $subject->getUser()->getId();
  39.         }
  40.         if ($attribute === self::ACCESS_WITH_COLLABORATORS) {
  41.             $collaboratedSession $this->collaboratedSessionRepository->findBy(['session' => $subject'collaboratedUser' => $user]);
  42.             if (count($collaboratedSession)) {
  43.                 return true;
  44.             }
  45.             return $user->getId() === $subject->getUser()->getId();
  46.         }
  47.         if ($attribute === self::ACCESS_BY_IDS) {
  48.             foreach ($subject as $uuid) {
  49.                 if (!in_array($uuid$user->getSessions()->toArray())) { // Checking if all sessions belong to the user
  50.                     $collaboratedSession $this->collaboratedSessionRepository->findBy(['session' => $subject'collaboratedUser' => $user]);
  51.                     if (null !== $collaboratedSession) {
  52.                         continue;
  53.                     }
  54.                     return false;
  55.                 }
  56.             }
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61. }