src/Security/Voter/ProjectVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use App\Entity\Accessmeister\User;
  6. use App\Entity\Taskmeister\SelectionProject as Project;
  7. class ProjectVoter extends Voter
  8. {
  9.     private const VIEW 'view';
  10.     private const EDIT 'edit';
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         // replace with your own logic
  14.         // https://symfony.com/doc/current/security/voters.html
  15.         return in_array($attribute, [self::VIEW])
  16.             && $subject instanceof Project;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         /**
  21.          * @var Project $subject
  22.          */
  23.         $user $token->getUser();
  24.         // if the user is anonymous, do not grant access
  25.         if (!$user instanceof User) {
  26.             return false;
  27.         }
  28.         $project $subject;
  29.         // ... (check conditions and return true to grant permission) ...
  30.         switch ($attribute) {
  31.             case self::VIEW:
  32.                 return $this->canView($project$user);
  33.                 break;
  34.         }
  35.         return false;
  36.     }
  37.     private function canView(Project $projectUser $user): bool
  38.     {
  39.         return $project->getCompany() === $user->getCompany();
  40.     }
  41. }