src/Security/Voters/ArticlesVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Articles\Articles;
  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 ArticlesVoter 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 `Article` objects
  25.         if (!$subject instanceof Articles) {
  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 Article object, thanks to `supports()`
  44.         /** @var Articles $article */
  45.         $article $subject;
  46.         switch ($attribute) {
  47.             case self::VIEW:
  48.                 return $this->canView($article$user);
  49.             case self::EDIT:
  50.                 return $this->canEdit($article$user);
  51.             case self::DELETE:
  52.                 return $this->canDelete($article$user);
  53.         }
  54.         throw new \LogicException('This code should not be reached!');
  55.     }
  56.     /**
  57.      * @param Articles $article
  58.      * @param User $user
  59.      * @return bool
  60.      */
  61.     private function canView(Articles $articleUser $user): bool
  62.     {
  63.         // if they can edit, they can view
  64.         if ($this->canEdit($article$user)) {
  65.             return true;
  66.         }
  67.         // the Article object could have, for example, a method `isPrivate()`
  68.         return !$article->getUser();
  69.     }
  70.     /**
  71.      * @param Articles $article
  72.      * @param User $user
  73.      * @return bool
  74.      */
  75.     private function canEdit(Articles $articleUser $user): bool
  76.     {
  77.         // this assumes that the Article object has a `getOwner()` method
  78.         return $user === $article->getUser();
  79.     }
  80.     /**
  81.      * @param Articles $article
  82.      * @param User $user
  83.      * @return bool
  84.      */
  85.     private function canDelete(Articles $articleUser $user): bool
  86.     {
  87.         // this assumes that the Article object has a `getOwner()` method
  88.         return $user === $article->getUser();
  89.     }
  90. }