src/Security/Voter/TimeTracking/PresenceVoter.php line 14

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