src/Security/Voters/ActualitesVoter.php line 11

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