src/Security/Voter/TimeTracking/TimeTrackingVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter\TimeTracking;
  3. use App\Entity\TimeTracking\Summary;
  4. use App\Entity\TimeTracking\TimeEntry;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Bundle\SecurityBundle\Security;
  9. class TimeTrackingVoter extends Voter
  10. {
  11.     final public const EDIT 'TT_ENTITY_EDIT';
  12.     final public const VIEW 'TT_ENTITY_VIEW';
  13.     final public const DELETE 'TT_ENTITY_DELETE';
  14.     
  15.     public function __construct(
  16.             private readonly Security $security)
  17.     {
  18.     }
  19.     
  20.     protected function supports(string $attributemixed $subject): bool
  21.     {
  22.         if(!in_array($attribute, [self::EDITself::VIEWself::DELETE])){
  23.             return false;
  24.         }
  25.         if (!$subject instanceof TimeEntry) {
  26.             return false;
  27.         }
  28.         
  29.         return true;
  30.     }
  31.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  32.     {
  33.         $user $token->getUser();
  34.         // if the user is anonymous, do not grant access
  35.         if (!$user instanceof UserInterface) {
  36.             return false;
  37.         }
  38.         if (!$this->security->isGranted('ROLE_STAFF')){
  39.             return false;
  40.         }
  41.        
  42.         if($user->getChronoActiv() != 1){
  43.             return false;  
  44.         }
  45.         
  46.         $flag false;
  47.         $mapping $user->getMapping();
  48.         foreach($mapping->getPlugin() as $plugin){
  49.             if($plugin->getPlugin()->getId() == and $plugin->getState() == 1){
  50.                 $flag true;
  51.             }
  52.         }
  53.         
  54.         if($flag == false){
  55.             return false;
  56.         }
  57.         
  58.         // ... (check conditions and return true to grant permission) ...
  59.        return match($attribute) {
  60.             self::VIEW => $this->canView($subject$user),
  61.             self::EDIT => $this->canEdit($subject$user),
  62.             self::DELETE => $this->canDelete(),
  63.             default => throw new \LogicException('This code should not be reached!')
  64.         };
  65.     }
  66.     private function canView(TimeEntry $entityCustomerUser $user): bool
  67.     {
  68.         if ($entity->getSummary()->getStaff()->getId() == $user->getStaff()) {
  69.             return true;
  70.         }
  71.         return false;
  72.     }
  73.     private function canEdit(?TimeEntry $entityCustomerUser $user): bool
  74.     {
  75.         if($entity->getStaff() == null AND $user->getChronoActivApp() != 1){
  76.             return false;  
  77.         }
  78.                 
  79.         if ($entity->getStaff()->getId() == $user->getStaff()) {
  80.             return true;
  81.         }
  82.         
  83.         return $this->security->isGranted('ROLE_ADMIN');
  84.     }
  85.     
  86.     private function canDelete(): bool
  87.     {
  88.         return $this->security->isGranted('ROLE_ADMIN');
  89.     }
  90. }