src/Profile/Security/Voter/NetworkVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Profile\Security\Voter;
  3. use App\Profile\Model\Profile;
  4. use App\Profile\Model\ProfileRoles;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class NetworkVoter extends Voter
  9. {
  10.   public const ADD 'profile.network.add';
  11.   public function __construct(protected Security $security)
  12.   {
  13.   }
  14.   protected function supports(string $attribute$subject): bool
  15.   {
  16.     if (!in_array($attribute, [self::ADD])) {
  17.       return false;
  18.     }
  19.     return true;
  20.   }
  21.   protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  22.   {
  23.     $user $token->getUser();
  24.     if (!$user instanceof Profile) {
  25.       return false;
  26.     }
  27.     /** @var Profile $profileToAdd */
  28.     $profileToAdd $subject;
  29.     switch ($attribute) {
  30.       case self::ADD:
  31.         return $this->canAdd($user$profileToAdd);
  32.     }
  33.     throw new \LogicException(sprintf('Attribute `%s` not supported by object'$attribute));
  34.   }
  35.   protected function canAdd(Profile $profileProfile $profileToAdd): bool
  36.   {
  37.     if ($profileToAdd->getProfiles()->getAuctionhouseProfile() || $profileToAdd->getProfiles()->getRacetrackProfile()) {
  38.       return $this->security->isGranted([ProfileRoles::ROLE_CLUB_JOCKEYProfileRoles::ROLE_CLUB_HORSE]);
  39.     }
  40.     return true;
  41.   }
  42. }