FeaturesController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Drupal\features\Controller;
  3. use Drupal\Core\Access\CsrfTokenGenerator;
  4. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  5. use Drupal\system\FileDownloadController;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. /**
  10. * Returns responses for config module routes.
  11. */
  12. class FeaturesController implements ContainerInjectionInterface {
  13. /**
  14. * The file download controller.
  15. *
  16. * @var \Drupal\system\FileDownloadController
  17. */
  18. protected $fileDownloadController;
  19. /**
  20. * The CSRF token generator.
  21. *
  22. * @var \Drupal\Core\Access\CsrfTokenGenerator
  23. */
  24. protected $csrfToken;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function create(ContainerInterface $container) {
  29. return new static(
  30. new FileDownloadController(),
  31. $container->get('csrf_token')
  32. );
  33. }
  34. /**
  35. * Constructs a FeaturesController object.
  36. *
  37. * @param \Drupal\system\FileDownloadController $file_download_controller
  38. * The file download controller.
  39. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
  40. * The CSRF token generator.
  41. */
  42. public function __construct(FileDownloadController $file_download_controller, CsrfTokenGenerator $csrf_token) {
  43. $this->fileDownloadController = $file_download_controller;
  44. $this->csrfToken = $csrf_token;
  45. }
  46. /**
  47. * Downloads a tarball of the site configuration.
  48. *
  49. * @param string $uri
  50. * The URI to download.
  51. * @param \Symfony\Component\HttpFoundation\Request $request
  52. * The request.
  53. *
  54. * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  55. * The downloaded file.
  56. */
  57. public function downloadExport($uri, Request $request) {
  58. if ($uri) {
  59. // @todo Simplify once https://www.drupal.org/node/2630920 is solved.
  60. if (!$this->csrfToken->validate($request->query->get('token'), $uri)) {
  61. throw new AccessDeniedHttpException();
  62. }
  63. $request = new Request(array('file' => $uri));
  64. return $this->fileDownloadController->download($request, 'temporary');
  65. }
  66. }
  67. }