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