BlockedIp.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Drupal\ban\Plugin\migrate\destination;
  3. use Drupal\ban\BanIpManagerInterface;
  4. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  5. use Drupal\migrate\Plugin\MigrationInterface;
  6. use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
  7. use Drupal\migrate\Row;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Destination for blocked IP addresses.
  11. *
  12. * @MigrateDestination(
  13. * id = "blocked_ip"
  14. * )
  15. */
  16. class BlockedIP extends DestinationBase implements ContainerFactoryPluginInterface {
  17. /**
  18. * The IP ban manager.
  19. *
  20. * @var \Drupal\ban\BanIpManagerInterface
  21. */
  22. protected $banManager;
  23. /**
  24. * Constructs a BlockedIP object.
  25. *
  26. * @param array $configuration
  27. * Plugin configuration.
  28. * @param string $plugin_id
  29. * The plugin ID.
  30. * @param mixed $plugin_definition
  31. * The plugin definition.
  32. * @param \Drupal\migrate\Plugin\MigrationInterface $migration
  33. * The current migration.
  34. * @param \Drupal\ban\BanIpManagerInterface $ban_manager
  35. * The IP manager service.
  36. */
  37. public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, BanIpManagerInterface $ban_manager) {
  38. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
  39. $this->banManager = $ban_manager;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  45. return new static(
  46. $configuration,
  47. $plugin_id,
  48. $plugin_definition,
  49. $migration,
  50. $container->get('ban.ip_manager')
  51. );
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getIds() {
  57. return ['ip' => ['type' => 'string']];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function fields(MigrationInterface $migration = NULL) {
  63. return [
  64. 'ip' => $this->t('The blocked IP address.'),
  65. ];
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function import(Row $row, array $old_destination_id_values = []) {
  71. $this->banManager->banIp($row->getDestinationProperty('ip'));
  72. }
  73. }