src/Http/Customer/Voter/ProjectVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Http\Customer\Voter;
  3. use App\Domain\Project\Entity\Annotation;
  4. use App\Domain\Project\Entity\Project;
  5. use App\Domain\User\Entity\User;
  6. use JetBrains\PhpStorm\Pure;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  10. class ProjectVoter extends Voter
  11. {
  12.     const VIEW 'VIEW_PROJECT';
  13.     const EDIT 'EDIT_PROJECT';
  14.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  15.     {
  16.         if(!($subject instanceof Project)) return false;
  17.         /** @var User $user */
  18.         $user $token->getUser();
  19.         return match ($attribute) {
  20.             self::VIEWself::EDIT => $this->canViewProject($subject$user),
  21.             default => false
  22.         };
  23.     }
  24.     private function canViewProject(Project $projectUser $user): bool
  25.     {
  26.         return $project->getCompany() === $user->getCompany() || $user->hasRole('ROLE_ADMIN');
  27.     }
  28.     protected function supports(string $attributemixed $subject): bool
  29.     {
  30.         return in_array($attribute, [self::VIEWself::EDIT]);
  31.     }
  32. }