src/Security/Voter/GruppiVoter.php line 13

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\User;
  6. use App\Entity\Gruppi;
  7. use App\Repository\GruppiRepository;
  8. use App\Repository\RolesRepository;
  9. class GruppiVoter extends Voter
  10. {
  11. private $m_RolesRep;
  12. public function __construct(RolesRepository $a_RolesRep)
  13. {
  14. $this->m_RolesRep = $a_RolesRep;
  15. }
  16. protected function supports($attribute, $subject)
  17. {
  18. // replace with your own logic
  19. // https://symfony.com/doc/current/security/voters.html
  20. $roles = $this->m_RolesRep->findAll();
  21. $aroles = array();
  22. foreach ($roles as $role) {
  23. $aroles[] = $role->getNome();
  24. }
  25. return in_array($attribute, $aroles) && ($subject === null);
  26. }
  27. protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  28. {
  29. $user = $token->getUser();
  30. // if the user is anonymous, do not grant access
  31. if (!($user instanceof User)) {
  32. return false;
  33. }
  34. $user_group_roles = $user->getGroupRoles();
  35. return in_array($attribute, $user_group_roles);
  36. }
  37. }