Issue #2072351 by tim.plunkett: Modernize ban.module forms.

8.0.x
webchick 2013-08-28 00:04:55 -07:00
parent f557439503
commit af00da698e
2 changed files with 16 additions and 27 deletions

View File

@ -7,29 +7,20 @@
namespace Drupal\ban\Form; namespace Drupal\ban\Form;
use Drupal\Core\Controller\ControllerInterface; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormInterface;
use Drupal\ban\BanIpManager; use Drupal\ban\BanIpManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Displays banned IP addresses. * Displays banned IP addresses.
*/ */
class BanAdmin implements FormInterface, ControllerInterface { class BanAdmin extends FormBase {
/** /**
* @var \Drupal\ban\BanIpManager * @var \Drupal\ban\BanIpManager
*/ */
protected $ipManager; protected $ipManager;
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/** /**
* Constructs a new BanAdmin object. * 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 * (optional) IP address to be passed on to drupal_get_form() for use as the
* default value of the IP address form field. * default value of the IP address form field.
*/ */
public function buildForm(array $form, array &$form_state, Request $request = NULL, $default_ip = '') { public function buildForm(array $form, array &$form_state, $default_ip = '') {
$this->request = $request;
$rows = array(); $rows = array();
$header = array(t('banned IP addresses'), t('Operations')); $header = array($this->t('banned IP addresses'), $this->t('Operations'));
$result = $this->ipManager->findAll(); $result = $this->ipManager->findAll();
foreach ($result as $ip) { foreach ($result as $ip) {
$row = array(); $row = array();
$row[] = $ip->ip; $row[] = $ip->ip;
$links = array(); $links = array();
$links['delete'] = array( $links['delete'] = array(
'title' => t('delete'), 'title' => $this->t('delete'),
'href' => "admin/config/people/ban/delete/$ip->iid", 'href' => "admin/config/people/ban/delete/$ip->iid",
); );
$row[] = array( $row[] = array(
@ -85,24 +75,24 @@ class BanAdmin implements FormInterface, ControllerInterface {
} }
$form['ip'] = array( $form['ip'] = array(
'#title' => t('IP address'), '#title' => $this->t('IP address'),
'#type' => 'textfield', '#type' => 'textfield',
'#size' => 48, '#size' => 48,
'#maxlength' => 40, '#maxlength' => 40,
'#default_value' => $default_ip, '#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'] = array('#type' => 'actions');
$form['actions']['submit'] = array( $form['actions']['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Add'), '#value' => $this->t('Add'),
); );
$form['ban_ip_banning_table'] = array( $form['ban_ip_banning_table'] = array(
'#theme' => 'table', '#theme' => 'table',
'#header' => $header, '#header' => $header,
'#rows' => $rows, '#rows' => $rows,
'#empty' => t('No blocked IP addresses available.'), '#empty' => $this->t('No blocked IP addresses available.'),
'#weight' => 120, '#weight' => 120,
); );
return $form; return $form;
@ -114,13 +104,13 @@ class BanAdmin implements FormInterface, ControllerInterface {
public function validateForm(array &$form, array &$form_state) { public function validateForm(array &$form, array &$form_state) {
$ip = trim($form_state['values']['ip']); $ip = trim($form_state['values']['ip']);
if ($this->ipManager->isBanned($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()) { elseif ($ip == $this->getRequest()->getClientIP()) {
form_set_error('ip', t('You may not ban your own IP address.')); 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) { 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) { public function submitForm(array &$form, array &$form_state) {
$ip = trim($form_state['values']['ip']); $ip = trim($form_state['values']['ip']);
$this->ipManager->banIp($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'; $form_state['redirect'] = 'admin/config/people/ban';
} }

View File

@ -7,7 +7,6 @@
namespace Drupal\ban\Form; namespace Drupal\ban\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\ConfirmFormBase;
use Drupal\ban\BanIpManager; use Drupal\ban\BanIpManager;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
@ -16,7 +15,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Provides a form to unban IP addresses. * Provides a form to unban IP addresses.
*/ */
class BanDelete extends ConfirmFormBase implements ControllerInterface { class BanDelete extends ConfirmFormBase {
/** /**
* The banned IP address. * The banned IP address.
@ -91,7 +90,7 @@ class BanDelete extends ConfirmFormBase implements ControllerInterface {
public function submitForm(array &$form, array &$form_state) { public function submitForm(array &$form, array &$form_state) {
$this->ipManager->unbanIp($this->banIp); $this->ipManager->unbanIp($this->banIp);
watchdog('user', 'Deleted %ip', array('%ip' => $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'; $form_state['redirect'] = 'admin/config/people/ban';
} }