diff --git a/core/modules/ban/ban.admin.inc b/core/modules/ban/ban.admin.inc deleted file mode 100644 index 9f20a45..0000000 --- a/core/modules/ban/ban.admin.inc +++ /dev/null @@ -1,142 +0,0 @@ -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'; -} diff --git a/core/modules/ban/ban.module b/core/modules/ban/ban.module index 9e9ed0f..29becf5 100644 --- a/core/modules/ban/ban.module +++ b/core/modules/ban/ban.module @@ -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( '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(); -} diff --git a/core/modules/ban/ban.routing.yml b/core/modules/ban/ban.routing.yml new file mode 100644 index 0000000..366bd2e --- /dev/null +++ b/core/modules/ban/ban.routing.yml @@ -0,0 +1,14 @@ +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_ip}' + defaults: + _form: '\Drupal\ban\Form\BanDelete' + requirements: + _permission: 'ban IP addresses' diff --git a/core/modules/ban/lib/Drupal/ban/Form/BanAdmin.php b/core/modules/ban/lib/Drupal/ban/Form/BanAdmin.php new file mode 100644 index 0000000..ca15bf1 --- /dev/null +++ b/core/modules/ban/lib/Drupal/ban/Form/BanAdmin.php @@ -0,0 +1,145 @@ +request = $request; + $this->database = $database; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('request'), + $container->get('database') + ); + } + + /** + * {@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, $default_ip = '') { + $rows = array(); + $header = array(t('banned IP addresses'), t('Operations')); + $result = $this->database->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; + } + + $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->database->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 == $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->database->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'; + } + +} diff --git a/core/modules/ban/lib/Drupal/ban/Form/BanDelete.php b/core/modules/ban/lib/Drupal/ban/Form/BanDelete.php new file mode 100644 index 0000000..5c4c45a --- /dev/null +++ b/core/modules/ban/lib/Drupal/ban/Form/BanDelete.php @@ -0,0 +1,104 @@ +database = $database; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('database') + ); + } + + /** + * {@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['ip'])); + } + + /** + * {@inheritdoc} + */ + protected function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + protected function getCancelPath() { + return 'admin/config/people/ban'; + } + + /** + * {@inheritdoc} + * + * @param int $ban_ip_id + * The IP address record ID to unban. + */ + public function buildForm(array $form, array &$form_state, $ban_ip = '') { + $this->banIP = $this->database->query("SELECT * FROM {ban_ip} WHERE iid = :iid", array(':iid' => $ban_ip))->fetchAssoc(); + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $this->database->delete('ban_ip') + ->condition('iid', $this->banIP['iid']) + ->execute(); + watchdog('user', 'Deleted %ip', array('%ip' => $this->banIP['ip'])); + drupal_set_message(t('The IP address %ip was deleted.', array('%ip' => $this->banIP['ip']))); + $form_state['redirect'] = 'admin/config/people/ban'; + } + +}