src/Security/Voter/DataImportVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Import\DataImport;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class DataImportVoter extends Voter
  9. {
  10. // these strings are just invented: you can use anything
  11. const VIEW = 'view';
  12. const EDIT = 'edit';
  13. const DELETE = 'delete';
  14. /**
  15. * @var Security
  16. */
  17. private Security $security;
  18. public function __construct(Security $security)
  19. {
  20. $this->security = $security;
  21. }
  22. protected function supports(string $attribute, $subject): bool
  23. {
  24. // if the attribute isn't one we support, return false
  25. if (!in_array($attribute, [self::VIEW, self::EDIT, self::DELETE], true)) {
  26. return false;
  27. }
  28. // only vote on `File` objects
  29. if (!$subject instanceof DataImport) {
  30. return false;
  31. }
  32. return true;
  33. }
  34. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  35. {
  36. $loggedUser = $token->getUser();
  37. if (!$loggedUser instanceof User) {
  38. // the user must be logged in; if not, deny access
  39. return false;
  40. }
  41. // you know $subject is a Post object, thanks to `supports()`
  42. /** @var DataImport $targetObj */
  43. $targetObj = $subject;
  44. switch ($attribute) {
  45. case self::VIEW:
  46. case self::EDIT:
  47. return $this->canSee($targetObj, $loggedUser);
  48. case self::DELETE:
  49. return $this->canDelete($targetObj, $loggedUser);
  50. }
  51. throw new \LogicException('This code should not be reached!');
  52. }
  53. private function canSee(DataImport $targetObj, User $loggedUser): bool
  54. {
  55. // sanity check (different client than logged user client)
  56. if ($targetObj->getClient()->getId() !== $loggedUser->getClient()->getId()) {
  57. return false;
  58. }
  59. if ($targetObj->isInvoiceType() && $this->security->isGranted('ROLE_CAN_IMPORT_INVOICES')) {
  60. return true;
  61. }
  62. if ($targetObj->isReadingsType() && $this->security->isGranted('ROLE_CAN_IMPORT_GAUGE_READINGS')) {
  63. return true;
  64. }
  65. if (in_array($targetObj->getType(), [DataImport::TYPE_GAUGES, DataImport::TYPE_GAUGES_EDIT]) && $this->security->isGranted('ROLE_CAN_IMPORT_GAUGES')) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. private function canDelete(DataImport $targetObj, User $loggedUser): bool
  71. {
  72. if (!$this->canSee($targetObj, $loggedUser)) {
  73. return false;
  74. }
  75. if ($targetObj->isInvoiceType() && $this->security->isGranted('ROLE_CAN_DELETE_IMPORT_INVOICES')) {
  76. return true;
  77. }
  78. if ($targetObj->isReadingsType() && $this->security->isGranted('ROLE_CAN_DELETE_IMPORT_GAUGE_READINGS')) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. }