ContactPointVoter.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\EntityVoter\Core;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Core\BankAccount;
  6. use App\Entity\Core\ContactPoint;
  7. use App\Security\Voter\EntityVoter\AbstractEntityVoter;
  8. use Symfony\Bundle\SecurityBundle\Security;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class ContactPointVoter extends AbstractEntityVoter
  13. {
  14. /**
  15. * @inheritdoc
  16. */
  17. protected static ?string $entityClass = ContactPoint::class;
  18. /**
  19. * @inheritdoc
  20. */
  21. protected static array $allowedOperations = [
  22. self::READ, self::EDIT, self::DELETE
  23. ];
  24. /**
  25. * Can the user interact with the ContactPoint with the given required role
  26. *
  27. * @param string $role The role needed to interract with the ContactPoint
  28. * @return bool
  29. */
  30. private function canInteractIfHasRole(object $subject, string $role): bool {
  31. return $this->isUserLoggedIn()
  32. && $subject->getOrganization()->count() === 1
  33. && $this->security->isGranted($role)
  34. && $subject->getOrganization()->current()->getId() === $this->getUser()->getOrganization()->getId();
  35. }
  36. /**
  37. * @inheritdoc
  38. *
  39. * @param $subject BankAccount
  40. * @return boolean
  41. */
  42. protected function canView(object $subject): bool {
  43. return $this->canInteractIfHasRole($subject, 'ROLE_ORGANIZATION_VIEW');
  44. }
  45. /**
  46. * @inheritdoc
  47. *
  48. * @param $subject BankAccount
  49. * @return boolean
  50. */
  51. protected function canEdit(object $subject): bool {
  52. return $this->canInteractIfHasRole($subject, 'ROLE_ORGANIZATION');
  53. }
  54. /**
  55. * @inheritdoc
  56. *
  57. * @param $subject BankAccount
  58. * @return boolean
  59. */
  60. protected function canDelete(object $subject): bool {
  61. return $this->canEdit($subject);
  62. }
  63. }