src/Profile/Security/Voter/TimelinePostVoter.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 TimelinePostVoter extends Voter
  9. {
  10.   public const CREATE 'profile.timeline.post.create';
  11.   public const CREATE_SERVICE 'profile.timeline.post.create.service';
  12.   public function __construct(protected Security $security)
  13.   {
  14.   }
  15.   protected function supports(string $attribute$subject): bool
  16.   {
  17.     if (!in_array($attribute, [self::CREATEself::CREATE_SERVICE])) {
  18.       return false;
  19.     }
  20.     return true;
  21.   }
  22.   protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.   {
  24.     $user $this->security->getUser();
  25.     if (!$user instanceof Profile) {
  26.       return false;
  27.     }
  28.     switch ($attribute) {
  29.       case self::CREATE:
  30.         return $this->canCreate($user);
  31.         break;
  32.       case self::CREATE_SERVICE:
  33.         return $this->canCreateService($user);
  34.         break;
  35.     }
  36.     throw new \LogicException(sprintf('Attribute `%s` not supported by object'$attribute));
  37.   }
  38.   protected function canCreate(Profile $profile): bool
  39.   {
  40.     return $this->security->isGranted(ProfileRoles::ROLE_USER);
  41.   }
  42.   protected function canCreateService(Profile $profile): bool
  43.   {
  44.     return $this->security->isGranted(ProfileRoles::ROLE_CLUB_BUSINESS) || $this->security->isGranted(ProfileRoles::ROLE_CLUB_JOCKEY);
  45.   }
  46. }