src/Platform/Security/FavoritesListVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Platform\Security;
  4. use App\Bundles\FavoritesBundle\Entity\FavoritesList;
  5. use App\Bundles\UserBundle\Entity\User;
  6. use LogicException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class FavoritesListVoter extends Voter
  10. {
  11.     public const VIEW   'view';
  12.     public const CREATE 'create';
  13.     /**
  14.      * @param string $attribute
  15.      * @param mixed  $subject
  16.      *
  17.      * @return bool
  18.      */
  19.     protected function supports(string $attributemixed $subject): bool
  20.     {
  21.         // if the attribute isn't one we support, return false
  22.         if (!in_array($attribute, [self::VIEWself::CREATE])) {
  23.             return false;
  24.         }
  25.         // only vote on `SavedSearch` objects
  26.         if (!$subject instanceof FavoritesList) {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  32.     {
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             // the user must be logged in; if not, deny access
  36.             return false;
  37.         }
  38.         // you know $subject is a SavedSearch object, thanks to `supports()`
  39.         /** @var FavoritesList $favoritesList */
  40.         $favoritesList $subject;
  41.         switch ($attribute) {
  42.             case self::VIEW:
  43.                 return $this->canView($favoritesList$user);
  44.             case self::CREATE:
  45.                 return $this->canCreate($favoritesList$user);
  46.         }
  47.         throw new LogicException('This code should not be reached!');
  48.     }
  49.     private function canView(FavoritesList $favoritesListUser $user): bool
  50.     {
  51.         return $this->canCreate($favoritesList$user);
  52.     }
  53.     private function canCreate(FavoritesList $favoritesListUser $user): bool
  54.     {
  55.         // TODO: Add check user
  56.         return true;
  57.     }
  58. }