src/Security/Voters/PostsVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Posts\Posts;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class PostsVoter extends Voter
  8. {
  9.     // these strings are just invented: you can use anything
  10.     const VIEW 'view';
  11.     const EDIT 'edit';
  12.     const DELETE 'delete';
  13.     /**
  14.      * @param string $attribute
  15.      * @param mixed $subject
  16.      * @return bool
  17.      */
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         // if the attribute isn't one we support, return false
  21.         if (!in_array($attribute, [self::VIEWself::EDITself::DELETE])) {
  22.             return false;
  23.         }
  24.         // only vote on `Post` objects
  25.         if (!$subject instanceof Posts) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     /**
  31.      * @param string $attribute
  32.      * @param mixed $subject
  33.      * @param TokenInterface $token
  34.      * @return bool
  35.      */
  36.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  37.     {
  38.         $user $token->getUser();
  39.         if (!$user instanceof User) {
  40.             // the user must be logged in; if not, deny access
  41.             return false;
  42.         }
  43.         // you know $subject is a Post object, thanks to `supports()`
  44.         /** @var Posts $post */
  45.         $post $subject;
  46.         switch ($attribute) {
  47.             case self::VIEW:
  48.                 return $this->canView($post$user);
  49.             case self::EDIT:
  50.                 return $this->canEdit($post$user);
  51.             case self::DELETE:
  52.                 return $this->canDelete($post$user);
  53.         }
  54.         throw new \LogicException('This code should not be reached!');
  55.     }
  56.     /**
  57.      * @param Posts $post
  58.      * @param User $user
  59.      * @return bool
  60.      */
  61.     private function canView(Posts $postUser $user): bool
  62.     {
  63.         // if they can edit, they can view
  64.         if ($this->canEdit($post$user)) {
  65.             return true;
  66.         }
  67.         // the Post object could have, for example, a method `isPrivate()`
  68.         return !$post->getUser();
  69.     }
  70.     /**
  71.      * @param Posts $post
  72.      * @param User $user
  73.      * @return bool
  74.      */
  75.     private function canEdit(Posts $postUser $user): bool
  76.     {
  77.         // this assumes that the Post object has a `getOwner()` method
  78.         return $user === $post->getUser();
  79.     }
  80.     /**
  81.      * @param Posts $post
  82.      * @param User $user
  83.      * @return bool
  84.      */
  85.     private function canDelete(Posts $postUser $user): bool
  86.     {
  87.         // this assumes that the Post object has a `getOwner()` method
  88.         return $user === $post->getUser();
  89.     }
  90. }