src/Security/Voter/CompanyFileVoter.php line 11

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