View source
<?php
namespace Drupal\externalauth\Plugin\migrate\destination;
use Drupal\externalauth\AuthmapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
class Authmap extends DestinationBase implements ContainerFactoryPluginInterface {
protected $authmap;
protected $userStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, AuthmapInterface $authmap, UserStorageInterface $user_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->authmap = $authmap;
$this->userStorage = $user_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('externalauth.authmap'), $container
->get('entity_type.manager')
->getStorage('user'));
}
public function getIds() {
return [
'uid' => [
'type' => 'integer',
],
];
}
public function fields(MigrationInterface $migration = NULL) {
return [
'uid' => 'Primary key: users.uid for user.',
'provider' => 'The name of the authentication provider providing the authname',
'authname' => 'Unique authentication name provided by authentication provider',
];
}
public function import(Row $row, array $old_destination_id_values = []) {
$account = $this->userStorage
->load($row
->getDestinationProperty('uid'));
$provider = $row
->getDestinationProperty('provider');
$authname = $row
->getDestinationProperty('authname');
$this->authmap
->save($account, $provider, $authname);
return [
$account
->id(),
];
}
}