comparison core/modules/ban/src/Form/BanAdmin.php @ 0:4c8ae668cc8c

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