src/Security/Voter/PostVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Post\Post;
  4. use App\Entity\Program\Program;
  5. use App\Entity\Program\ProgramCrewMember;
  6. use App\Entity\User;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class PostVoter extends Voter
  10. {
  11.     // these strings are just invented: you can use anything
  12.     const SUSPENDRE 'suspendre';
  13.     const SHOW_RESULT 'showResult';
  14.     protected function supports(string $attribute$subject): bool
  15.     {
  16.         // if the attribute isn't one we support, return false
  17.         if (!in_array($attribute, [self::SUSPENDREself::SHOW_RESULT])) {
  18.             return false;
  19.         }
  20.         // only vote on `Post` objects
  21.         if (!$subject instanceof Post) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  27.     {
  28.         $user $token->getUser();
  29.         if (!$user instanceof User) {
  30.             // the user must be logged in; if not, deny access
  31.             return false;
  32.         }
  33.         // you know $subject is a Post object, thanks to `supports()`
  34.         /** @var Post $post */
  35.         $post $subject;
  36.         switch ($attribute) {
  37.             case self::SUSPENDRE:
  38.                 return $this->canSuspendre($post$user);
  39.             case self::SHOW_RESULT:
  40.                 return $this->canShowResult($post$user);
  41.         }
  42.         throw new \LogicException('This code should not be reached!');
  43.     }
  44.     private function canSuspendre(Post $postUser $user): bool
  45.     {
  46.         if ($post->getMedia()->getCreatedBy() === $user) {
  47.             return true;
  48.         }
  49.         /**
  50.          * @var ProgramCrewMember[] $crews
  51.          */
  52.         $authorized false;
  53.         if ($post->getProgram()) {
  54.             $crews $post->getProgram()->getCrews();
  55.             if (sizeof($crews) > 0) {
  56.                 foreach ($crews as $crew) {
  57.                     if (
  58.                         $crew->getUser() === $user &&
  59.                         (
  60.                             $crew->getHasAdminRole() ||
  61.                             $crew->getHasPostPublisherRole()
  62.                         )
  63.                     ) {
  64.                         $authorized true;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         return $authorized;
  70.     }
  71.     private function canShowResult(Post $postUser $user): bool
  72.     {
  73.         if ($post->getMedia()->getCreatedBy() === $user) {
  74.             return true;
  75.         }
  76.         /**
  77.          * @var ProgramCrewMember[] $crews
  78.          */
  79.         $authorized false;
  80.         if ($post->getProgram()) {
  81.             $crews $post->getProgram()->getCrews();
  82.             if (sizeof($crews) > 0) {
  83.                 foreach ($crews as $crew) {
  84.                     if (
  85.                         $crew->getUser() === $user &&
  86.                         (
  87.                             $crew->getHasAdminRole() ||
  88.                             $crew->getHasPostPublisherRole()
  89.                         )
  90.                     ) {
  91.                         $authorized true;
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.         return $authorized;
  97.     }
  98. }