diff --git a/drush.services.yml b/drush.services.yml new file mode 100644 index 00000000..fb36bda7 --- /dev/null +++ b/drush.services.yml @@ -0,0 +1,5 @@ +services: + islandora.commands: + class: \Drupal\islandora\Commands\IslandoraCommands + tags: + - { name: drush.command } diff --git a/src/Commands/IslandoraCommands.php b/src/Commands/IslandoraCommands.php new file mode 100644 index 00000000..19df5869 --- /dev/null +++ b/src/Commands/IslandoraCommands.php @@ -0,0 +1,82 @@ + self::REQ]) { + } + + /** + * Validate the provided userid. + * + * @hook validate migrate:import + */ + public function validateUser(CommandData $commandData) { + $userid = $commandData->input()->getOption('userid'); + if ($userid) { + $account = User::load($userid); + if (!$account) { + throw new \Exception("User ID does not match an existing user."); + } + } + } + + /** + * Switch the active user account to perform the import. + * + * @hook pre-command migrate:import + */ + 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 user back once the migration is complete. + * + * @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(); + } + } + +}