Commit 1bf078e7 authored by Dries's avatar Dries

Issue #1944910 by tim.plunkett, barraponto, jibran: Convert ban.module's page...

Issue #1944910 by tim.plunkett, barraponto, jibran: Convert ban.module's page callbacks to Controllers.
parent ec9dc2c3
<?php
/**
* @file
* Administrative functionality for the Ban module.
*/
/**
* Page callback: Displays banned IP addresses.
*
* @param string $default_ip
* (optional) IP address to be passed on to drupal_get_form() for use as the
* default value of the IP address form field.
*
* @return array
* A render array.
*/
function ban_admin_page($default_ip = '') {
$rows = array();
$header = array(t('banned IP addresses'), t('Operations'));
$result = db_query('SELECT * FROM {ban_ip}');
foreach ($result as $ip) {
$row = array();
$row[] = $ip->ip;
$links = array();
$links['delete'] = array(
'title' => t('delete'),
'href' => "admin/config/people/ban/delete/$ip->iid",
);
$row[] = array(
'data' => array(
'#type' => 'operations',
'#links' => $links,
),
);
$rows[] = $row;
}
$build['ban_ip_form'] = drupal_get_form('ban_ip_form', $default_ip);
$build['ban_ip_banning_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No blocked IP addresses available.'),
);
return $build;
}
/**
* Form constructor for banning an IP address.
*
* @param string $default_ip
* An IP address to ban, used as default value.
*
* @see ban_ip_form_validate()
* @see ban_ip_form_submit()
* @ingroup forms
*/
function ban_ip_form($form, &$form_state, $default_ip) {
$form['ip'] = array(
'#title' => t('IP address'),
'#type' => 'textfield',
'#size' => 48,
'#maxlength' => 40,
'#default_value' => $default_ip,
'#description' => t('Enter a valid IP address.'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* Form validation handler for ban_ip_form().
*
* @see ban_ip_form_submit()
*/
function ban_ip_form_validate($form, &$form_state) {
$ip = trim($form_state['values']['ip']);
if (db_query("SELECT * FROM {ban_ip} WHERE ip = :ip", array(':ip' => $ip))->fetchField()) {
form_set_error('ip', t('This IP address is already banned.'));
}
elseif ($ip == Drupal::request()->getClientIP()) {
form_set_error('ip', 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 submission handler for ban_ip_form().
*
* @see ban_ip_form_validate()
*/
function ban_ip_form_submit($form, &$form_state) {
$ip = trim($form_state['values']['ip']);
db_insert('ban_ip')
->fields(array('ip' => $ip))
->execute();
drupal_set_message(t('The IP address %ip has been banned.', array('%ip' => $ip)));
$form_state['redirect'] = 'admin/config/people/ban';
}
/**
* Form constructor to unban an IP address.
*
* @param array $ban_ip
* The IP address record to unban, as provided by ban_ip_load().
*
* @see ban_ip_delete_submit()
*/
function ban_ip_delete_form($form, &$form_state, array $ban_ip) {
$form['ban_ip'] = array(
'#type' => 'value',
'#value' => $ban_ip,
);
return confirm_form($form,
t('Are you sure you want to unblock %ip?', array('%ip' => $ban_ip['ip'])),
'admin/config/people/ban',
NULL,
t('Delete')
);
}
/**
* Form submission handler for ban_ip_delete_form().
*/
function ban_ip_delete_form_submit($form, &$form_state) {
$banned_ip = $form_state['values']['ban_ip'];
db_delete('ban_ip')
->condition('iid', $banned_ip['iid'])
->execute();
watchdog('user', 'Deleted %ip', array('%ip' => $banned_ip['ip']));
drupal_set_message(t('The IP address %ip was deleted.', array('%ip' => $banned_ip['ip'])));
$form_state['redirect'] = 'admin/config/people/ban';
}
......@@ -44,30 +44,12 @@ function ban_menu() {
$items['admin/config/people/ban'] = array(
'title' => 'IP address bans',
'description' => 'Manage banned IP addresses.',
'page callback' => 'ban_admin_page',
'access arguments' => array('ban IP addresses'),
'file' => 'ban.admin.inc',
'route_name' => 'ban_admin_page',
'weight' => 10,
);
$items['admin/config/people/ban/delete/%ban_ip'] = array(
$items['admin/config/people/ban/delete/%'] = array(
'title' => 'Delete IP address',
'page callback' => 'drupal_get_form',
'page arguments' => array('ban_ip_delete_form', 5),
'access arguments' => array('ban IP addresses'),
'file' => 'ban.admin.inc',
'route_name' => 'ban_delete',
);
return $items;
}
/**
* Loads a banned IP address record from the database.
*
* @param int $iid
* The ID of the banned IP address to retrieve.
*
* @return array
* The banned IP address record from the database as an array.
*/
function ban_ip_load($iid) {
return db_query("SELECT * FROM {ban_ip} WHERE iid = :iid", array(':iid' => $iid))->fetchAssoc();
}
ban_admin_page:
pattern: '/admin/config/people/ban/{default_ip}'
defaults:
_form: '\Drupal\ban\Form\BanAdmin'
default_ip: ''
requirements:
_permission: 'ban IP addresses'
ban_delete:
pattern: '/admin/config/people/ban/delete/{ban_id}'
defaults:
_form: '\Drupal\ban\Form\BanDelete'
requirements:
_permission: 'ban IP addresses'
......@@ -17,14 +17,14 @@ class BanIpManager {
/**
* The database connection used to check the IP against.
*
* @var Drupal\Core\Database\Connection
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Construct the BanSubscriber.
*
* @param Drupal\Core\Database\Connection $connection
* @param \Drupal\Core\Database\Connection $connection
* The database connection which will be used to check the IP against.
*/
public function __construct(Connection $connection) {
......@@ -46,4 +46,65 @@ public function isDenied($ip) {
->fetchField();
return (bool) $denied;
}
/**
* Returns if this IP address is banned.
*
* @param string $ip
* The IP address to check.
*
* @return bool
* TRUE if the IP address is banned, FALSE otherwise.
*/
public function isBanned($ip) {
return (bool) $this->connection->query("SELECT * FROM {ban_ip} WHERE ip = :ip", array(':ip' => $ip))->fetchField();
}
/**
* Finds all banned IP addresses.
*
* @return \Drupal\Core\Database\StatementInterface
* The result of the database query.
*/
public function findAll() {
return $this->connection->query('SELECT * FROM {ban_ip}');
}
/**
* Bans an IP address.
*
* @param string $ip
* The IP address to ban.
*/
public function banIp($ip) {
$this->connection->insert('ban_ip')
->fields(array('ip' => $ip))
->execute();
}
/**
* Unbans an IP address.
*
* @param string $id
* The IP address to unban.
*/
public function unbanIp($id) {
$this->connection->delete('ban_ip')
->condition('ip', $id)
->execute();
}
/**
* Finds a banned IP address by its ID.
*
* @param int $ban_id
* The ID for a banned IP address.
*
* @return string|false
* Either the banned IP address or FALSE if none exist with that ID.
*/
public function findById($ban_id) {
return $this->connection->query("SELECT ip FROM {ban_ip} WHERE iid = :iid", array(':iid' => $ban_id))->fetchField();
}
}
<?php
/**
* @file
* Contains \Drupal\ban\Form\BanAdmin.
*/
namespace Drupal\ban\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\ban\BanIpManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays banned IP addresses.
*/
class BanAdmin implements FormInterface, ControllerInterface {
/**
* @var \Drupal\ban\BanIpManager
*/
protected $ipManager;
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs a new BanAdmin object.
*
* @param \Drupal\ban\BanIpManager $ip_manager
*/
public function __construct(BanIpManager $ip_manager) {
$this->ipManager = $ip_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('ban.ip_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'ban_ip_form';
}
/**
* {@inheritdoc}
*
* @param string $default_ip
* (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;
$rows = array();
$header = array(t('banned IP addresses'), t('Operations'));
$result = $this->ipManager->findAll();
foreach ($result as $ip) {
$row = array();
$row[] = $ip->ip;
$links = array();
$links['delete'] = array(
'title' => t('delete'),
'href' => "admin/config/people/ban/delete/$ip->iid",
);
$row[] = array(
'data' => array(
'#type' => 'operations',
'#links' => $links,
),
);
$rows[] = $row;
}
$form['ip'] = array(
'#title' => t('IP address'),
'#type' => 'textfield',
'#size' => 48,
'#maxlength' => 40,
'#default_value' => $default_ip,
'#description' => t('Enter a valid IP address.'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
$form['ban_ip_banning_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No blocked IP addresses available.'),
'#weight' => 120,
);
return $form;
}
/**
* {@inheritdoc}
*/
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.'));
}
elseif ($ip == $this->request->getClientIP()) {
form_set_error('ip', 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.'));
}
}
/**
* {@inheritdoc}
*/
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)));
$form_state['redirect'] = 'admin/config/people/ban';
}
}
<?php
/**
* @file
* Contains \Drupal\ban\Form\BanDelete.
*/
namespace Drupal\ban\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\ban\BanIpManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provides a form to unban IP addresses.
*/
class BanDelete extends ConfirmFormBase implements ControllerInterface {
/**
* The banned IP address.
*
* @var string
*/
protected $banIp;
/**
* Constructs a new BanDelete object.
*
* @param \Drupal\ban\BanIpManager $ip_manager
*/
public function __construct(BanIpManager $ip_manager) {
$this->ipManager = $ip_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('ban.ip_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'ban_ip_delete_form';
}
/**
* {@inheritdoc}
*/
protected function getQuestion() {
return t('Are you sure you want to unblock %ip?', array('%ip' => $this->banIp));
}
/**
* {@inheritdoc}
*/
protected function getConfirmText() {
return t('Delete');
}
/**
* {@inheritdoc}
*/
protected function getCancelPath() {
return 'admin/config/people/ban';
}
/**
* {@inheritdoc}
*
* @param string $ban_id
* The IP address record ID to unban.
*/
public function buildForm(array $form, array &$form_state, $ban_id = '') {
if (!$this->banIp = $this->ipManager->findById($ban_id)) {
throw new NotFoundHttpException();
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
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)));
$form_state['redirect'] = 'admin/config/people/ban';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment