src/Security/Voter/PartnerApi/ScholarVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\PartnerApi;
  3. use App\Entity\Channel\Channel;
  4. use App\Entity\Exercise\Exercise;
  5. use App\Entity\PartnerApi\PartnerApiUser;
  6. use App\Entity\Scholar\Chapter\Chapter;
  7. use App\Entity\Scholar\PracticalCase\PracticalCase;
  8. use App\Entity\Scholar\ScholarInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. class ScholarVoter extends Voter
  12. {
  13.     public const LIST = 'SCHOLAR_LIST';
  14.     public const CREATE 'SCHOLAR_CREATE';
  15.     public const EDIT 'SCHOLAR_EDIT';
  16.     public const VIEW 'SCHOLAR_VIEW';
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         return match ($attribute) {
  20.             self::LIST, self::CREATE => $subject instanceof Channel,
  21.             self::EDITself::VIEW =>
  22.                 $subject instanceof ScholarInterface
  23.                 || $subject instanceof Chapter
  24.                 || $subject instanceof PracticalCase
  25.                 || $subject instanceof Exercise,
  26.             default => false,
  27.         };
  28.     }
  29.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  30.     {
  31.         $user $token->getUser();
  32.         // if the user is anonymous, do not grant access
  33.         if (!$user instanceof PartnerApiUser) {
  34.             return false;
  35.         }
  36.         // ... (check conditions and return true to grant permission) ...
  37.         return match ($attribute) {
  38.             self::LIST => $this->canList($user$subject),
  39.             self::CREATE => $this->canCreate($user$subject),
  40.             self::EDIT => $this->canEdit($user$subject),
  41.             self::VIEW => $this->canView($user$subject),
  42.             default => false,
  43.         };
  44.     }
  45.     private function canList(PartnerApiUser $userChannel $channel): bool
  46.     {
  47.         return $user->getChannels()->contains($channel);
  48.     }
  49.     private function canCreate(PartnerApiUser $userChannel $channel): bool
  50.     {
  51.         return $this->canList($user$channel);
  52.     }
  53.     private function canEdit(PartnerApiUser $userScholarInterface|Chapter|PracticalCase|Exercise $scholarObject): bool
  54.     {
  55.         $createdBy null;
  56.         if (method_exists($scholarObject'getCreatedBy')) {
  57.             $createdBy $scholarObject->getCreatedBy();
  58.         } elseif (method_exists($scholarObject'getLesson')) {
  59.             $createdBy $scholarObject->getLesson()->getCreatedBy();
  60.         }
  61.         return $this->canView($user$scholarObject)
  62.             && $createdBy === $user->getUser();
  63.     }
  64.     private function canView(PartnerApiUser $userScholarInterface|Chapter|PracticalCase|Exercise $scholarObject): bool
  65.     {
  66.         $channel null;
  67.         if (method_exists($scholarObject'getOwnerChannel')) {
  68.             $channel $scholarObject->getOwnerChannel();
  69.         } elseif (method_exists($scholarObject'getLesson')) {
  70.             $channel $scholarObject->getLesson()->getOwnerChannel();
  71.         }
  72.         return $this->canList($user$channel);
  73.     }
  74. }