src/Security/Voters/ManufacturerVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Manufacturer;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. class ManufacturerVoter extends Voter
  8. {
  9.     private Security $security;
  10.     public const CAN_EDIT_HOLIDAYS 'manufacturer.holidays.edit';
  11.     private const ATTRIBUTES = [
  12.         self::CAN_EDIT_HOLIDAYS
  13.     ];
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.     }
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         return in_array($attributeself::ATTRIBUTES);
  21.     }
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  23.     {
  24.         switch ($attribute) {
  25.             case self::CAN_EDIT_HOLIDAYS:
  26.                 return $this->canUpdateHolidays($subject);
  27.         }
  28.         throw new \LogicException('Invalid attribute: '.$attribute);
  29.     }
  30.     private function canUpdateHolidays(?Manufacturer $manufacturer): bool
  31.     {
  32.         if ( !$this->security->getUser() ) {
  33.             return false;
  34.         }
  35.         if ( $this->security->isGranted('ROLE_LOGISTICS_MANAGER') || $this->security->isGranted('ROLE_MANUFACTURING_MANAGER') ) {
  36.             return true;
  37.         }
  38.         return false;
  39.     }
  40. }