public function preImport(CommandData $commandData) {
    $userid = $commandData->input()->getOption('userid');
    if ($userid) {
      $account = User::load($userid);
      $accountSwitcher = \Drupal::service('account_switcher');
      $userSession = new UserSession([
        'uid'   => $account->id(),
        'name'  => $account->getUsername(),
        'roles' => $account->getRoles(),
      ]);
      $accountSwitcher->switchTo($userSession);
      $this->logger()->notice(
          dt(
              'Now acting as user ID @id',
              ['@id' => \Drupal::currentUser()->id()]
            )
      );
    }
  }
public function preImport(CommandData $commandData) {
    $userid = $commandData->input()->getOption('userid');
    if ($userid) {
      $account = User::load($userid);
      $accountSwitcher = \Drupal::service('account_switcher');
      $userSession = new UserSession([
        'uid'   => $account->id(),
        'name'  => $account->getUsername(),
        'roles' => $account->getRoles(),
      ]);
      $accountSwitcher->switchTo($userSession);
      $this->logger()->notice(
          dt(
              'Now acting as user ID @id',
              ['@id' => \Drupal::currentUser()->id()]
            )
      );
    }
  }
public function preImport ( CommandData $ commandData ) { $ userid = $ commandData -> input ( ) -> getOption ( 'userid' ) ; if ( $ userid ) { $ account = User :: load ( $ userid ) ; $ accountSwitcher = \ Drupal :: service ( 'account_switcher' ) ; $ userSession = new UserSession ( [ 'uid' => $ account -> id ( ) , 'name' => $ account -> getUsername ( ) , 'roles' => $ account -> getRoles ( ) , ] ) ; $ accountSwitcher -> switchTo ( $ userSession ) ; $ this -> logger ( ) -> notice ( dt ( 'Now acting as user ID @id' , [ '@id' => \ Drupal :: currentUser ( ) -> id ( ) ] ) ) ; } }
Switch the active user account to perform the import.

@hook pre-command migrate:import
Switch the active user account to perform the import .