d71be8d40259d3910744b2ba30ca474164071ae2
[yaffs-website] / src / Form / BanAdmin.php
1 <?php
2
3 namespace Drupal\ban\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\ban\BanIpManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Displays banned IP addresses.
13  *
14  * @internal
15  */
16 class BanAdmin extends FormBase {
17
18   /**
19    * @var \Drupal\ban\BanIpManagerInterface
20    */
21   protected $ipManager;
22
23   /**
24    * Constructs a new BanAdmin object.
25    *
26    * @param \Drupal\ban\BanIpManagerInterface $ip_manager
27    */
28   public function __construct(BanIpManagerInterface $ip_manager) {
29     $this->ipManager = $ip_manager;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('ban.ip_manager')
38     );
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getFormId() {
45     return 'ban_ip_form';
46   }
47
48   /**
49    * {@inheritdoc}
50    *
51    * @param string $default_ip
52    *   (optional) IP address to be passed on to
53    *   \Drupal::formBuilder()->getForm() for use as the default value of the IP
54    *   address form field.
55    */
56   public function buildForm(array $form, FormStateInterface $form_state, $default_ip = '') {
57     $rows = [];
58     $header = [$this->t('banned IP addresses'), $this->t('Operations')];
59     $result = $this->ipManager->findAll();
60     foreach ($result as $ip) {
61       $row = [];
62       $row[] = $ip->ip;
63       $links = [];
64       $links['delete'] = [
65         'title' => $this->t('Delete'),
66         'url' => Url::fromRoute('ban.delete', ['ban_id' => $ip->iid]),
67       ];
68       $row[] = [
69         'data' => [
70           '#type' => 'operations',
71           '#links' => $links,
72         ],
73       ];
74       $rows[] = $row;
75     }
76
77     $form['ip'] = [
78       '#title' => $this->t('IP address'),
79       '#type' => 'textfield',
80       '#size' => 48,
81       '#maxlength' => 40,
82       '#default_value' => $default_ip,
83       '#description' => $this->t('Enter a valid IP address.'),
84     ];
85     $form['actions'] = ['#type' => 'actions'];
86     $form['actions']['submit'] = [
87       '#type' => 'submit',
88       '#value' => $this->t('Add'),
89     ];
90
91     $form['ban_ip_banning_table'] = [
92       '#type' => 'table',
93       '#header' => $header,
94       '#rows' => $rows,
95       '#empty' => $this->t('No blocked IP addresses available.'),
96       '#weight' => 120,
97     ];
98     return $form;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function validateForm(array &$form, FormStateInterface $form_state) {
105     $ip = trim($form_state->getValue('ip'));
106     if ($this->ipManager->isBanned($ip)) {
107       $form_state->setErrorByName('ip', $this->t('This IP address is already banned.'));
108     }
109     elseif ($ip == $this->getRequest()->getClientIP()) {
110       $form_state->setErrorByName('ip', $this->t('You may not ban your own IP address.'));
111     }
112     elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
113       $form_state->setErrorByName('ip', $this->t('Enter a valid IP address.'));
114     }
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function submitForm(array &$form, FormStateInterface $form_state) {
121     $ip = trim($form_state->getValue('ip'));
122     $this->ipManager->banIp($ip);
123     $this->messenger()->addStatus($this->t('The IP address %ip has been banned.', ['%ip' => $ip]));
124     $form_state->setRedirect('ban.admin_page');
125   }
126
127 }