Commit c5e18126 authored by Eelke Blok's avatar Eelke Blok Committed by Steven Jones
Browse files

[#3536585] fix: Account switching doesn't work correctly for drush command

By: eelkeblok
By: steven jones
parent 77f79791
Loading
Loading
Loading
Loading
Loading
+49 −2
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Session\UserSession;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Url;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
@@ -147,6 +148,9 @@ class DataExport extends RestExport {
            $total_rows,
            $query_parameters,
            $redirect_url,
            // Add the current user ID, mostly for Drush, which looses the user
            // context when running a batch.
            \Drupal::currentUser()->id(),
          ],
        ],
      ],
@@ -702,6 +706,50 @@ class DataExport extends RestExport {
  /**
   * Implements callback_batch_operation() - perform processing on each batch.
   *
   * Provides a wrapper that switches the current user to the user that is
   * should be running the batch process.
   *
   * @param string $view_id
   *   ID of the view.
   * @param string $display_id
   *   ID of the view display.
   * @param array $args
   *   Views arguments.
   * @param array $exposed_input
   *   Exposed input.
   * @param int $total_rows
   *   Total rows.
   * @param array $query_parameters
   *   Query string parameters.
   * @param string $redirect_url
   *   Redirect URL.
   * @param int $uid
   *   User ID. This is used to be able to pass the user ID to the batch
   *   process, which is relevant in the context of the Drush command.
   * @param mixed $context
   *   Batch context information.
   *
   * @throws \PhpOffice\PhpSpreadsheet\Exception
   * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
   * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
   */
  public static function processBatch($view_id, $display_id, array $args, array $exposed_input, $total_rows, array $query_parameters, $redirect_url, int $uid, &$context = []) {
    $switchAccount = ((int) \Drupal::currentUser()->id() !== $uid);
    if ($switchAccount) {
      $accountSwitcher = \Drupal::service('account_switcher');
      $accountSwitcher->switchTo(new UserSession(['uid' => $uid]));
    }

    static::doProcessBatch($view_id, $display_id, $args, $exposed_input, $total_rows, $query_parameters, $redirect_url, $context);

    if ($switchAccount) {
      $accountSwitcher->switchBack();
    }
  }

  /**
   * Implements the heavy lifting for the callback_batch_operation().
   *
   * Writes rendered data export View rows to an output file that will be
   * returned by callback_batch_finished() (i.e. finishBatch) when we're done.
   *
@@ -726,8 +774,7 @@ class DataExport extends RestExport {
   * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
   * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
   */
  public static function processBatch($view_id, $display_id, array $args, array $exposed_input, $total_rows, array $query_parameters, $redirect_url, &$context) {

  protected static function doProcessBatch($view_id, $display_id, array $args, array $exposed_input, $total_rows, array $query_parameters, $redirect_url, &$context = []) {
    // Add query string back to the URL for processing.
    if ($query_parameters) {
      \Drupal::request()->query->add($query_parameters);