src/Security/Voter/SaleVoter.php line 13

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Sale\Upwork\Rss;
  5. use App\Entity\User\User;
  6. use App\Enum\User\UserRoleEnum;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class SaleVoter extends Voter
  10. {
  11.     const EDIT 'EDIT';
  12.     const CHANGE_STATUS 'CHANGE_STATUS';
  13.     protected function supports(string $attributemixed $subject): bool
  14.     {
  15.         if (!\in_array($attribute, [self::CHANGE_STATUSself::EDIT])) {
  16.             return false;
  17.         }
  18.         if (!$subject instanceof Rss) {
  19.             return false;
  20.         }
  21.         return true;
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof User) {
  27.             return false;
  28.         }
  29.         return match ($attribute) {
  30.             self::EDIT => $this->hasAccessSale($user),
  31.             self::CHANGE_STATUS => $this->hasAccessSale($user),
  32.             default => throw new \LogicException('This code should not be reached!'),
  33.         };
  34.     }
  35.     private function hasAccessSale(User $user): bool
  36.     {
  37.         if (\in_array(UserRoleEnum::ROLE_SALE->value$user->getRoles())) {
  38.             return true;
  39.         }
  40.         return false;
  41.     }
  42. }