Issue #2072351 by tim.plunkett: Modernize ban.module forms.
parent
f557439503
commit
af00da698e
|
@ -7,29 +7,20 @@
|
|||
|
||||
namespace Drupal\ban\Form;
|
||||
|
||||
use Drupal\Core\Controller\ControllerInterface;
|
||||
use Drupal\Core\Form\FormInterface;
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\ban\BanIpManager;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Displays banned IP addresses.
|
||||
*/
|
||||
class BanAdmin implements FormInterface, ControllerInterface {
|
||||
class BanAdmin extends FormBase {
|
||||
|
||||
/**
|
||||
* @var \Drupal\ban\BanIpManager
|
||||
*/
|
||||
protected $ipManager;
|
||||
|
||||
/**
|
||||
* The request object.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Constructs a new BanAdmin object.
|
||||
*
|
||||
|
@ -62,17 +53,16 @@ class BanAdmin implements FormInterface, ControllerInterface {
|
|||
* (optional) IP address to be passed on to drupal_get_form() for use as the
|
||||
* default value of the IP address form field.
|
||||
*/
|
||||
public function buildForm(array $form, array &$form_state, Request $request = NULL, $default_ip = '') {
|
||||
$this->request = $request;
|
||||
public function buildForm(array $form, array &$form_state, $default_ip = '') {
|
||||
$rows = array();
|
||||
$header = array(t('banned IP addresses'), t('Operations'));
|
||||
$header = array($this->t('banned IP addresses'), $this->t('Operations'));
|
||||
$result = $this->ipManager->findAll();
|
||||
foreach ($result as $ip) {
|
||||
$row = array();
|
||||
$row[] = $ip->ip;
|
||||
$links = array();
|
||||
$links['delete'] = array(
|
||||
'title' => t('delete'),
|
||||
'title' => $this->t('delete'),
|
||||
'href' => "admin/config/people/ban/delete/$ip->iid",
|
||||
);
|
||||
$row[] = array(
|
||||
|
@ -85,24 +75,24 @@ class BanAdmin implements FormInterface, ControllerInterface {
|
|||
}
|
||||
|
||||
$form['ip'] = array(
|
||||
'#title' => t('IP address'),
|
||||
'#title' => $this->t('IP address'),
|
||||
'#type' => 'textfield',
|
||||
'#size' => 48,
|
||||
'#maxlength' => 40,
|
||||
'#default_value' => $default_ip,
|
||||
'#description' => t('Enter a valid IP address.'),
|
||||
'#description' => $this->t('Enter a valid IP address.'),
|
||||
);
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Add'),
|
||||
'#value' => $this->t('Add'),
|
||||
);
|
||||
|
||||
$form['ban_ip_banning_table'] = array(
|
||||
'#theme' => 'table',
|
||||
'#header' => $header,
|
||||
'#rows' => $rows,
|
||||
'#empty' => t('No blocked IP addresses available.'),
|
||||
'#empty' => $this->t('No blocked IP addresses available.'),
|
||||
'#weight' => 120,
|
||||
);
|
||||
return $form;
|
||||
|
@ -114,13 +104,13 @@ class BanAdmin implements FormInterface, ControllerInterface {
|
|||
public function validateForm(array &$form, array &$form_state) {
|
||||
$ip = trim($form_state['values']['ip']);
|
||||
if ($this->ipManager->isBanned($ip)) {
|
||||
form_set_error('ip', t('This IP address is already banned.'));
|
||||
form_set_error('ip', $this->t('This IP address is already banned.'));
|
||||
}
|
||||
elseif ($ip == $this->request->getClientIP()) {
|
||||
form_set_error('ip', t('You may not ban your own IP address.'));
|
||||
elseif ($ip == $this->getRequest()->getClientIP()) {
|
||||
form_set_error('ip', $this->t('You may not ban your own IP address.'));
|
||||
}
|
||||
elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
|
||||
form_set_error('ip', t('Enter a valid IP address.'));
|
||||
form_set_error('ip', $this->t('Enter a valid IP address.'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,7 +120,7 @@ class BanAdmin implements FormInterface, ControllerInterface {
|
|||
public function submitForm(array &$form, array &$form_state) {
|
||||
$ip = trim($form_state['values']['ip']);
|
||||
$this->ipManager->banIp($ip);
|
||||
drupal_set_message(t('The IP address %ip has been banned.', array('%ip' => $ip)));
|
||||
drupal_set_message($this->t('The IP address %ip has been banned.', array('%ip' => $ip)));
|
||||
$form_state['redirect'] = 'admin/config/people/ban';
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
namespace Drupal\ban\Form;
|
||||
|
||||
use Drupal\Core\Controller\ControllerInterface;
|
||||
use Drupal\Core\Form\ConfirmFormBase;
|
||||
use Drupal\ban\BanIpManager;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
@ -16,7 +15,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|||
/**
|
||||
* Provides a form to unban IP addresses.
|
||||
*/
|
||||
class BanDelete extends ConfirmFormBase implements ControllerInterface {
|
||||
class BanDelete extends ConfirmFormBase {
|
||||
|
||||
/**
|
||||
* The banned IP address.
|
||||
|
@ -91,7 +90,7 @@ class BanDelete extends ConfirmFormBase implements ControllerInterface {
|
|||
public function submitForm(array &$form, array &$form_state) {
|
||||
$this->ipManager->unbanIp($this->banIp);
|
||||
watchdog('user', 'Deleted %ip', array('%ip' => $this->banIp));
|
||||
drupal_set_message(t('The IP address %ip was deleted.', array('%ip' => $this->banIp)));
|
||||
drupal_set_message($this->t('The IP address %ip was deleted.', array('%ip' => $this->banIp)));
|
||||
$form_state['redirect'] = 'admin/config/people/ban';
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue