src/Security/Voter/Customer/CalendarAbsenceVoter.php line 14

  1. <?php
  2. namespace App\Security\Voter\Customer;
  3. use App\Entity\Customer\Illness;
  4. use App\Entity\Admin\CustomerUser;
  5. use App\Repository\Customer\SettingsRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Bundle\SecurityBundle\Security;
  10. class CalendarAbsenceVoter extends Voter
  11. {
  12.     final public const VIEW   'CA_ENTITY_VIEW';
  13.      
  14.     public function __construct(
  15.             private readonly Security $security, private readonly SettingsRepository $settingsRepository)
  16.     {
  17.     }
  18.     
  19.     protected function supports(string $attributemixed $subject): bool
  20.     {
  21.         if(!in_array($attribute, [self::VIEW])){
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  27.     {
  28.         $user $token->getUser();
  29.         // if the user is anonymous, do not grant access
  30.         if (!$user instanceof UserInterface) {
  31.             return false;
  32.         }
  33.         if (!$this->security->isGranted('ROLE_STAFF')){
  34.             return false;
  35.         }
  36.        
  37.         // ... (check conditions and return true to grant permission) ...
  38.        return match($attribute) {
  39.             self::VIEW => $this->canView(),
  40.             default => throw new \LogicException('This code should not be reached!')
  41.         };
  42.     }
  43.     
  44.     private function canView(): bool
  45.     {
  46.         $settings $this->settingsRepository->getByKey("staff_absences_view");
  47.         
  48.         if ($settings->getMetaValueString() == "1") {
  49.             return true;
  50.         }  
  51.         return false;
  52.     }
  53. }