src/Http/Customer/Voter/AnnotationVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Http\Customer\Voter;
  3. use App\Domain\Project\Entity\Annotation;
  4. use App\Domain\User\Entity\User;
  5. use JetBrains\PhpStorm\Pure;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  9. class AnnotationVoter extends Voter
  10. {
  11.     const ADD 'ADD_ANNOTATION';
  12.     const EDIT 'EDIT_ANNOTATION';
  13.     const REMOVE 'REMOVE_ANNOTATION';
  14.     const VALIDATE 'VALIDATE_ANNOTATION';
  15.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  16.     {
  17.         if(!($subject instanceof Annotation)) return false;
  18.         /** @var User $user */
  19.         $user $token->getUser();
  20.         return match ($attribute) {
  21.             self::ADD => $this->canAddAnnotation($subject$user),
  22.             self::EDITself::VALIDATEself::REMOVE => $this->canEditAnnotation($subject$user),
  23.             default => false
  24.         };
  25.     }
  26.     private function canAddAnnotation(Annotation $annotationUser $user): bool
  27.     {
  28.         return $annotation->getProject()->getCompany() === $user->getCompany();
  29.     }
  30.     private function canEditAnnotation(Annotation $annotationUser $user): bool
  31.     {
  32.         return $annotation->getAuthor() === $user || in_array('ROLE_ADMIN'$user->getRoles());
  33.     }
  34.     protected function supports(string $attributemixed $subject): bool
  35.     {
  36.         return in_array($attribute, [self::ADDself::EDITself::VALIDATEself::REMOVE]);
  37.     }
  38. }