src/Security/Voter/CandidatureVoter.php line 9

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 Symfony\Component\Security\Core\User\UserInterface;
  6. class CandidatureVoter extends Voter
  7. {
  8. protected function supports(string $attribute, $subject): bool
  9. {
  10. // replace with your own logic
  11. // https://symfony.com/doc/current/security/voters.html
  12. return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
  13. && $subject instanceof \App\Entity\Candidature;
  14. }
  15. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  16. {
  17. $user = $token->getUser();
  18. // if the user is anonymous, do not grant access
  19. if (!$user instanceof UserInterface) {
  20. return false;
  21. }
  22. // ... (check conditions and return true to grant permission) ...
  23. switch ($attribute) {
  24. case 'POST_EDIT':
  25. // logic to determine if the user can EDIT
  26. // return true or false
  27. break;
  28. case 'POST_VIEW':
  29. // logic to determine if the user can VIEW
  30. // return true or false
  31. break;
  32. }
  33. return false;
  34. }
  35. }