src/Security/ListingBuilder/PpcVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\ListingBuilder;
  3. use App\Entity\ListingBuilder\PpcKeyword;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. /**
  7.  * Class ListBuildVoter
  8.  *
  9.  * @package App\Security\ListingBuilder
  10.  */
  11. class PpcVoter extends Voter
  12. {
  13.     const EDIT   'edit';
  14.     const VIEW   'view';
  15.     const DELETE 'delete';
  16.     /**
  17.      * base voter actions
  18.      */
  19.     private const ATTRIBUTES = [
  20.         self::EDIT,
  21.         self::VIEW,
  22.         self::DELETE
  23.     ];
  24.     /**
  25.      * @param string $attribute
  26.      * @param mixed  $subject
  27.      *
  28.      * @return bool
  29.      */
  30.     protected function supports($attribute$subject): bool
  31.     {
  32.         return $subject instanceof PpcKeyword
  33.             && in_array($attributeself::ATTRIBUTES);
  34.     }
  35.     /**
  36.      * @param                $attribute
  37.      * @param PpcKeyword     $ppcKeyword
  38.      * @param TokenInterface $token
  39.      *
  40.      * @return bool
  41.      */
  42.     protected function voteOnAttribute(
  43.         $attribute,
  44.         $ppcKeyword,
  45.         TokenInterface $token
  46.     ): bool {
  47.         switch ($attribute) {
  48.             case self::EDIT:
  49.             case self::DELETE:
  50.             case self::VIEW:
  51.                 return $ppcKeyword->getUser() == $token->getUser();
  52.                 break;
  53.         }
  54.         throw new \LogicException('Invalid attribute: ' $attribute);
  55.     }
  56. }