BanDelete.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\ban\Form;
  3. use Drupal\Core\Form\ConfirmFormBase;
  4. use Drupal\ban\BanIpManagerInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Url;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. /**
  10. * Provides a form to unban IP addresses.
  11. *
  12. * @internal
  13. */
  14. class BanDelete extends ConfirmFormBase {
  15. /**
  16. * The banned IP address.
  17. *
  18. * @var string
  19. */
  20. protected $banIp;
  21. /**
  22. * The IP manager.
  23. *
  24. * @var \Drupal\ban\BanIpManagerInterface
  25. */
  26. protected $ipManager;
  27. /**
  28. * Constructs a new BanDelete object.
  29. *
  30. * @param \Drupal\ban\BanIpManagerInterface $ip_manager
  31. * The IP manager.
  32. */
  33. public function __construct(BanIpManagerInterface $ip_manager) {
  34. $this->ipManager = $ip_manager;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function create(ContainerInterface $container) {
  40. return new static(
  41. $container->get('ban.ip_manager')
  42. );
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getFormId() {
  48. return 'ban_ip_delete_form';
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getQuestion() {
  54. return $this->t('Are you sure you want to unblock %ip?', ['%ip' => $this->banIp]);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getConfirmText() {
  60. return $this->t('Delete');
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getCancelUrl() {
  66. return new Url('ban.admin_page');
  67. }
  68. /**
  69. * {@inheritdoc}
  70. *
  71. * @param string $ban_id
  72. * The IP address record ID to unban.
  73. */
  74. public function buildForm(array $form, FormStateInterface $form_state, $ban_id = '') {
  75. if (!$this->banIp = $this->ipManager->findById($ban_id)) {
  76. throw new NotFoundHttpException();
  77. }
  78. return parent::buildForm($form, $form_state);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function submitForm(array &$form, FormStateInterface $form_state) {
  84. $this->ipManager->unbanIp($this->banIp);
  85. $this->logger('user')->notice('Deleted %ip', ['%ip' => $this->banIp]);
  86. $this->messenger()->addStatus($this->t('The IP address %ip was deleted.', ['%ip' => $this->banIp]));
  87. $form_state->setRedirectUrl($this->getCancelUrl());
  88. }
  89. }