src/Http/Api/Voter/ProjectVoter.php line 13

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