src/Infrastructure/Security/Voter/ToastSessionSectionVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\Security\Voter;
  3. use App\Database\Domain\Entity\Sessions\ToastSessionSection;
  4. use App\Database\Domain\Entity\User\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ToastSessionSectionVoter extends Voter
  8. {
  9.     public const ACCESS_BY_ROLE 'toast_session_section.access_by_role';
  10.     public const ACCESS 'toast_session_section.access';
  11.     public const ACCESS_BY_IDS 'toast_session_section.access_by_ids';
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         return in_array($attribute, [self::ACCESSself::ACCESS_BY_ROLEself::ACCESS_BY_IDS]);
  15.     }
  16.     /**
  17.      * @param ToastSessionSection $subject
  18.      */
  19.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  20.     {
  21.         /** @var User $user */
  22.         $user $token->getUser();
  23.         if (!$user instanceof User) {
  24.             return false;
  25.         }
  26.         if ($attribute === self::ACCESS_BY_ROLE) {
  27.             return $user->isFullUser() || $user->isPartialUser();
  28.         }
  29.         if ($attribute === self::ACCESS) {
  30.             return $user->getId() === $subject->getUser()->getId();
  31.         }
  32.         if ($attribute === self::ACCESS_BY_IDS) {
  33.             $userSections array_map(
  34.                 static fn (ToastSessionSection $item) => $item->getId(),
  35.                 $user->getSections()->toArray()
  36.             );
  37.             foreach ($subject as $uuid) {
  38.                 if (!in_array($uuid$userSections)) { // Checking if all sessions belong to the user
  39.                     return false;
  40.                 }
  41.             }
  42.             return true;
  43.         }
  44.         return false;
  45.     }
  46. }