src/Security/ModuleVoter.php line 14

Open in your IDE?
  1. <?php
  2. /**
  3. * User: bruno.ziegler
  4. * Date: 26.08.16 09:30.
  5. */
  6. namespace App\Security;
  7. use App\Entity\Sysuser\Sysuser;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class ModuleVoter extends Voter
  12. {
  13. private string $prefix = 'MODULE_';
  14. /**
  15. * Determines if the attribute and subject are supported by this voter.
  16. *
  17. * @param string $attribute An attribute
  18. * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  19. *
  20. * @return bool True if the attribute and subject are supported, false otherwise
  21. */
  22. #[\Override]
  23. protected function supports(string $attribute, $subject): bool
  24. {
  25. return str_starts_with($attribute, $this->prefix);
  26. }
  27. /**
  28. * Perform a single access check operation on a given attribute, subject and token.
  29. */
  30. #[\Override]
  31. protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  32. {
  33. /**
  34. * @var Sysuser $user;
  35. */
  36. $user = $token->getUser();
  37. // Check, ob gültiges User Objekt (kein Anonymous oder so)
  38. if (!$user instanceof UserInterface) {
  39. return false;
  40. }
  41. // Admin Zugriff auf alle MODULE erlauben
  42. if (in_array($user->getRole(), ['ROLE_ADMIN', 'ROLE_SYSADMIN'])) {
  43. return true;
  44. }
  45. // Ende
  46. // Modulberechtigungen checken (die MODULE_xyz sind ebenfalls in den ::getRoles() enthalten)
  47. // Ende
  48. return in_array($attribute, $user->getRoles());
  49. }
  50. }