Skip to content

Commit

Permalink
add drush migrate:import userid option
Browse files Browse the repository at this point in the history
Switches the user running the migration to the one indicated, necessary for JWT authentication to work for generating derivatives.

Resolves #4
  • Loading branch information
seth-shaw-unlv committed May 15, 2018
1 parent 24fc5b5 commit b25a34a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ Note: using drush with migrate_tools is optional, but the instructions assume it
0. Copy the data directory to your drupal web root (e.g. in my tests the drupal web root is `/var/www/drupalvm/drupal/web` and the data directory is `/var/www/drupalvm/drupal/web/data`).
0. Copy the migrate_cdm and unlv_image directories to your modules directory.
0. Enable the modules. E.g. `drush en -y migrate_tools migrate_apollo`.
0. Run the migration. E.g. `drush -l http://localhost:8000 mim --all`. *Note: drush must be run by the webserver user because the claw_file migration copies files to the "public://directory". E.g.* `sudo -u www-data drush -l http://localhost:8000 mim --all` *if you are using vagrant.*
0. Generate service images. *Still addressing issues with service file generation. Until we figure out that problem you will need to do it yourself by going to the content page, selecting all the items you migrated, and then use the "Generate a service file from image preservation master". This will trigger both the service image and, as a chain reaction, the thumbnail generation.*
0. Run the migration. E.g. `drush -l http://localhost:8000 mim --userid=1 --all`. *Note: drush must be run by the webserver user because the claw_file migration copies files to the "public://directory". E.g.* `sudo -u www-data drush -l http://localhost:8000 mim --userid=1 --all` *if you are using vagrant. Also, the userid flag is specific to the migrate:import command, it provides the necessary user information to the JWT authentication to enable derivatives.*
0. See a wonderful list of the newly migrated images on your Drupal site's front page!

# Combining People and Subject entities in a single column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source:
constants:
collection_alias: 'apollo'
image: 'Image'
uid: 1 # UID of Admin user, may be changed to uid of someone with permission to create items
column_names:
0:
digital_id: 'Digital ID'
Expand All @@ -41,6 +42,7 @@ process:
default_value: unlv_image

# One-to-One mappings
uid: constants/uid
field_digital_id: digital_id
title: title
field_description: description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ source:
- digital_id
constants:
preservation_master: 'Preservation Master'
uid: 1 # UID of Admin user, may be changed to uid of someone with permission to create items
column_names:
0:
digital_id: 'Digital ID' # identifier key
Expand All @@ -27,6 +28,8 @@ process:
source: digital_id
no_stub: true

uid: constants/uid

# Lookup the Tiff we just migrated
field_media_file/target_id:
plugin: migration_lookup
Expand Down
5 changes: 5 additions & 0 deletions migrate_apollo/drush.services.yml
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 }
83 changes: 83 additions & 0 deletions migrate_apollo/src/Commands/MigrateApolloCommands.php
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();
}
}
}

0 comments on commit b25a34a

Please sign in to comment.