-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add drush migrate:import userid option
Switches the user running the migration to the one indicated, necessary for JWT authentication to work for generating derivatives. Resolves #4
- Loading branch information
1 parent
24fc5b5
commit b25a34a
Showing
5 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
migrate_apollo.commands: | ||
class: \Drupal\migrate_apollo\Commands\MigrateApolloCommands | ||
tags: | ||
- { name: drush.command } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Drupal\migrate_apollo\Commands; | ||
|
||
use Consolidation\AnnotatedCommand\CommandData; | ||
use Drupal\Core\Session\UserSession; | ||
use Drupal\Core\Session\AccountSwitcherInterface; | ||
use Drupal\migrate_tools\Commands\MigrateToolsCommands; | ||
use Drupal\user\Entity\User; | ||
use Drush\Commands\DrushCommands; | ||
|
||
/** | ||
* Adds a userid option to migrate:import | ||
* | ||
* ... because the --user option was removed from drush 9. | ||
*/ | ||
class MigrateApolloCommands extends DrushCommands { | ||
|
||
/** | ||
* @hook option migrate:import | ||
* @option userid User ID to run the migration. | ||
*/ | ||
public function optionsetImportUser($options = ['userid' => self::REQ]) | ||
{ | ||
} | ||
|
||
/** | ||
* @hook validate migrate:import | ||
*/ | ||
public function validateUser(CommandData $commandData) | ||
{ | ||
$userid = $commandData->input()->getOption('userid'); | ||
if($userid) | ||
{ | ||
$account = \Drupal\user\Entity\User::load($userid); | ||
if (!$account) { | ||
throw new \Exception("User ID does not match an existing user."); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @hook pre-command migrate:import | ||
*/ | ||
public function preImport(CommandData $commandData) | ||
{ | ||
// | ||
$userid = $commandData->input()->getOption('userid'); | ||
if ($userid) | ||
{ | ||
$account = \Drupal\user\Entity\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()] | ||
) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @hook post-command migrate:import | ||
*/ | ||
public function postImport($result, CommandData $commandData) | ||
{ | ||
if ($commandData->input()->getOption('userid')) | ||
{ | ||
$accountSwitcher = \Drupal::service('account_switcher'); | ||
$this->logger()->notice(dt( | ||
'Switching back from user @uid.', | ||
['@uid'=>\Drupal::currentUser()->id()] | ||
)); | ||
$accountSwitcher->switchBack(); | ||
} | ||
} | ||
} |