src/Security/Voter/Voter/SportObjectVoter.php line 12

Open in your IDE?
  1. <?php
  2. /* @author <storageprocedure@gmail.com> */
  3. declare(strict_types=1);
  4. namespace App\Security\Voter\Voter;
  5. use App\Entity\SportObject;
  6. use App\Entity\User;
  7. use App\Security\Voter\AbstractVoter;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. class SportObjectVoter extends AbstractVoter
  10. {
  11.     /**
  12.      * @param string $attribute
  13.      * @param $subject
  14.      * @return bool
  15.      */
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         $cond1 $subject instanceof SportObject;
  19.         $cond2 in_array($attribute, [self::VIEWself::EDIT]);
  20.         return $cond1 && $cond2;
  21.     }
  22.     /**
  23.      * @param string $attribute
  24.      * @param $subject
  25.      * @param TokenInterface $token
  26.      * @return bool
  27.      */
  28.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  29.     {
  30.         $user $token->getUser();
  31.         if (!$user instanceof User) {
  32.             return false;
  33.         }
  34.         if ($user->hasRole(User::ROLE_ADMIN)) {
  35.             return true;
  36.         }
  37.         switch ($attribute) {
  38.             case self::EDIT:
  39.                 return $this->canEdit($subject$user);
  40.         }
  41.         return false;
  42.     }
  43.     private function canEdit(SportObject $sportObjectUser $user): bool
  44.     {
  45.         $cond1 $user->hasRole(User::ROLE_SPORT_OBJECT_REDACTOR);
  46.         $cond2 $user->getSportObject()->getId() === $sportObject->getId();
  47.         return $cond1 && $cond2;
  48.     }
  49. }