vendor/roothirsch/dam-bundle/Filter/FileAccessUserInjection.php line 68

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\DamBundle\Filter;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use Roothirsch\CoreBundle\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
  12. class FileAccessUserInjection implements EventSubscriberInterface
  13. {
  14. /**
  15. * @var ObjectManager
  16. */
  17. protected $entityManager;
  18. /**
  19. * @var RouterInterface
  20. */
  21. private $router;
  22. /**
  23. * @var TokenStorageInterface
  24. */
  25. private $tokenStorage;
  26. public function __construct(
  27. EntityManagerInterface $entityManager,
  28. RouterInterface $router,
  29. TokenStorageInterface $tokenStorage
  30. ) {
  31. $this->entityManager = $entityManager;
  32. $this->router = $router;
  33. $this->tokenStorage = $tokenStorage;
  34. }
  35. /**
  36. * Returns an array of event names this subscriber wants to listen to.
  37. *
  38. * The array keys are event names and the value can be:
  39. *
  40. * * The method name to call (priority defaults to 0)
  41. * * An array composed of the method name to call and the priority
  42. * * An array of arrays composed of the method names to call and respective
  43. * priorities, or 0 if unset
  44. *
  45. * For instance:
  46. *
  47. * * array('eventName' => 'methodName')
  48. * * array('eventName' => array('methodName', $priority))
  49. * * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  50. *
  51. * @return array The event names to listen to
  52. */
  53. public static function getSubscribedEvents(): array
  54. {
  55. return [
  56. KernelEvents::REQUEST => ['onKernelRequest', EventPriorities::PRE_READ],
  57. ];
  58. }
  59. public function onKernelRequest(\Symfony\Component\HttpKernel\Event\RequestEvent $event)
  60. {
  61. if ($this->router->getContext()->getPathInfo() === '/login') {
  62. return;
  63. }
  64. /** Blocking this filter on any request that is performing a persistent action */
  65. if(in_array($event->getRequest()->getMethod(), ["PUT", "POST", "DELETE"] )){
  66. return;
  67. }
  68. $filter = $this->entityManager->getFilters()->enable('file_access_filter');
  69. if ($this->tokenStorage->getToken() && $this->tokenStorage->getToken()->getUser() instanceof User) {
  70. $filter->setUser($this->tokenStorage->getToken()->getUser());
  71. }
  72. }
  73. }