diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000000000000000000000000000000000..c78f0131e221378f455a6e8af6cf5d24867cf60d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,22 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# Drupal Coding Standards +# https://www.drupal.org/docs/develop/standards/coding-standards + +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +tab_width = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.txt] +trim_trailing_whitespace = false + +[*.{md,json,yml}] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..4a8834151d4265f6b5d3859b067a69d5afbb18c9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Set the default behavior, in case people don't have `core.autocrlf` set. +* text=auto + +# Declare files that will always have LF line endings on checkout. +*.php text eol=lf + +# Remove files for archives generated using `git archive`. +/.* export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..3b82dd8bb6921f5b81d818a090c1cc4f99c26e8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Composer +/vendor/ +composer.lock + +# Editors +project.xml +project.properties +/nbproject/private/ +.buildpath +.project +.settings* +.idea +.vscode +*.sublime-project +*.sublime-workspace +.sublimelinterrc + +# OS X metadata +.DS_Store + +# Windows junk +Thumbs.db \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d928cfdffac609c849666a41918c8494b0cd5a71 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# HTTP Auth + +## Description + +This module allows you to set up HTTP authentication on your site. You can +easily set username and password for HTTP Authentication. No server site +configuration needed. + +## Features + + * Restricting crawler to crawl on your site while development. + * Restrict admin pages after being your site Live. + * Easy to activate or deactivate HTTP Auth without deactivating module. + +## INSTALLATION INSTRUCTIONS + +Install as you would normally install a contributed Drupal module. Visit: +https://www.drupal.org/docs/8/extending-drupal-8/installing-drupal-8-modules +for further information. + +## CONFIGURATION + +* Configure user permissions in Administration » People » Permissions + + * HTTP Auth + + Users in roles with "HTTP Auth" permission can change HTTP Auth configurations. + +* Make settings and activate the functionality from the settings page. Setting + page found under: Development -> HTTP Auth Page. + +## MAINTAINERS + +Current maintainers: + * Sami Ahmed Siddiqui (sasiddiqui) - https://www.drupal.org/user/3100375 + +## NOTES + +This module add HTTP Auth on your complete site or on admin and login pages. You +can change the settings at any time from the settings page. diff --git a/README.txt b/README.txt deleted file mode 100644 index 07ba2c89be173acd72d199ca534aba587c51282d..0000000000000000000000000000000000000000 --- a/README.txt +++ /dev/null @@ -1,27 +0,0 @@ -HTTP Auth ---------- -This module allows you to set up HTTP authentication on your site. -You can easily set username and password for HTTP Authentication. -No server site configuration needed. - -Features --------- - * Restricting crawler to crawl on your site while development. - * Restrict admin pages after being your site Live. - * Easy to activate or deactivate HTTP Auth without deactivating module. - -INSTALLATION INSTRUCTIONS -------------------------- -1. Copy the files included in the tarball into a directory named "http_auth" - in your Drupal sites/all/modules/ directory. -2. Login as site administrator. -3. Enable the HTTP Auth module on the Administer -> Modules page. -4. Make settings and activate the functionaity from the settings page. - Setting page found under: Administer -> Configuration -> Development - -> HTTP Auth Settings Page. -5. Enjoy. - -NOTES ------ -This module add HTTP Auth on your complete site or on admin and login pages. -You can change the settings at any time from the settings page. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000000000000000000000000000000000000..52e7dae19de002224db331b4db29f23f2d3667c2 --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "drupal/http_auth", + "description": "Allows you to set up HTTP authentication on your site without server site configurations.", + "type": "drupal-module", + "license": "GPL-2.0-or-later", + "keywords": [ + "Drupal", + "http-auth" + ], + "homepage": "https://www.drupal.org/project/http_auth", + "authors": [ + { + "name": "See contributors", + "homepage": "https://www.drupal.org/node/2833082/committers", + "role": "Developer" + } + ], + "support": { + "email": "sami.siddiqui@yasglobal.com", + "issues": "https://www.drupal.org/project/issues/http_auth", + "source": "https://git.drupalcode.org/project/http_auth" + }, + "require": { + "php": "^7.4 || ^8.0", + "drupal/core": "^8 || ^9" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1", + "drupal/coder": "^8.3" + } +} diff --git a/http_auth.info b/http_auth.info deleted file mode 100644 index 8a3d943cbc396d87becf67e56e4bec7680850d22..0000000000000000000000000000000000000000 --- a/http_auth.info +++ /dev/null @@ -1,5 +0,0 @@ -name = HTTP Auth -description = This module allows you to set up HTTP authentication on your site. You can easily set username and password for HTTP Authentication. No server site configuration needed. -core = 7.x -package = HTTP Auth -configure = admin/config/development/http-auth diff --git a/http_auth.info.yml b/http_auth.info.yml new file mode 100644 index 0000000000000000000000000000000000000000..712b3e087d2f6830e09cd33c280a7070849fc746 --- /dev/null +++ b/http_auth.info.yml @@ -0,0 +1,9 @@ +name: HTTP Auth +description: This module allows you to set up HTTP authentication on your site. You can easily set username and password for HTTP Authentication. No server site configuration needed. +package: HTTP Auth + +type: module +core: 8.x +core_version_requirement: ^8 || ^9 + +configure: http_auth.admin_settings diff --git a/http_auth.install b/http_auth.install index 1a70251e41042319ad4571bd5118a3a50ab33678..15a42e6780abfb499975fafcf9f67dbc63565c7a 100644 --- a/http_auth.install +++ b/http_auth.install @@ -9,5 +9,7 @@ * Implements hook_uninstall(). */ function http_auth_uninstall() { - variable_del("http_auth"); + \Drupal::service('config.factory') + ->getEditable('http_auth.settings') + ->delete(); } diff --git a/http_auth.links.menu.yml b/http_auth.links.menu.yml new file mode 100644 index 0000000000000000000000000000000000000000..236d35694d7e4edbdd172266b5bc2d3763008a4e --- /dev/null +++ b/http_auth.links.menu.yml @@ -0,0 +1,5 @@ +http_auth.admin_settings: + title: 'HTTP Auth' + description: 'Allow to add HTTP Auth on your site or admin pages' + parent: system.admin_config_development + route_name: http_auth.admin_settings diff --git a/http_auth.links.task.yml b/http_auth.links.task.yml new file mode 100644 index 0000000000000000000000000000000000000000..d947b145996df4c4e03fd7d0a70c91142bf36e15 --- /dev/null +++ b/http_auth.links.task.yml @@ -0,0 +1,4 @@ +http_auth.admin_settings: + route_name: http_auth.admin_settings + title: 'HTTP Auth' + base_route: http_auth.admin_settings diff --git a/http_auth.module b/http_auth.module index d235fa0712b40e17e7d6c20ca0de5f1c6e8fed69..11c5bc6609f526d299e6d10c565fd2e018499956 100644 --- a/http_auth.module +++ b/http_auth.module @@ -6,141 +6,16 @@ */ /** - * Implements hook_menu(). + * Implements hook_page_top(). */ -function http_auth_menu() { - - $items = array(); - - $items['admin/config/development/http-auth'] = array( - 'title' => 'Http Auth settings', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('http_auth_settings_form'), - 'access arguments' => array('administer http auth module'), - 'description' => 'Allow to add HTTP Auth on your site or admin pages', - ); - - return $items; -} - -/** - * Implements hook_permission(). - */ -function http_auth_permission() { - - $permissions['administer http auth module'] = array( - 'title' => t('HTTP Auth'), - 'restrict access' => TRUE, - 'description' => t('Users who have this permission can change the HTTP Auth settings.'), - ); - - return $permissions; -} - -/** - * Implements hook_form(). - */ -function http_auth_settings_form($form, &$form_state) { - $http_auth_section = variable_get('http_auth'); - if (!is_array($http_auth_section)) { - $http_auth_section = unserialize($http_auth_section); - } - $applicable = array('complete' => t('Complete Site'), 'admin' => t('Admin and User Pages')); - - $form['http_auth'] = array( - '#type' => 'fieldset', - '#title' => t('Add HTTP Auth on your site'), - '#description' => t('By activating, your site or admin pages would be <strong>locked</strong> for unauthenticated users.'), - ); - - $form['http_auth']['username'] = array( - '#type' => 'textfield', - '#title' => t('HTTP Auth Username'), - '#description' => t('Add HTTP Auth username'), - '#default_value' => isset($http_auth_section['username']) ? $http_auth_section['username'] : '', - '#size' => 60, - '#maxlength' => 64, - '#required' => TRUE, - '#attributes' => array( - 'placeholder' => 'username', - ), - ); - - $form['http_auth']['password'] = array( - '#type' => 'password', - '#title' => t('HTTP Auth password'), - '#description' => t('Add HTTP Auth password'), - '#default_value' => isset($http_auth_section['password']) ? $http_auth_section['password'] : '', - '#size' => 60, - '#maxlength' => 64, - '#required' => TRUE, - '#attributes' => array( - 'placeholder' => 'password', - ), - ); - - $form['http_auth']['message'] = array( - '#type' => 'textarea', - '#title' => t('HTTP Auth Message'), - '#description' => t('Add HTTP Auth message which would be shown to the unauthenticated users.'), - '#default_value' => isset($http_auth_section['message']) ? $http_auth_section['message'] : '', - '#attributes' => array( - 'placeholder' => t('This page is Restricted. Please contact the administrator for access.'), - ), - ); - - $form['http_auth']['applicable'] = array( - '#type' => 'radios', - '#title' => t('Applicable on:'), - '#default_value' => isset($http_auth_section['applicable']) ? $http_auth_section['applicable'] : 'complete', - '#options' => $applicable, - ); - - $form['http_auth']['activate'] = array( - '#type' => 'checkbox', - '#title' => t('Activate HTTP Authentication'), - '#default_value' => isset($http_auth_section['activate']) ? $http_auth_section['activate'] : 0, - ); - - $form['http_auth']['note'] = array( - '#markup' => "<div><strong>Note:</strong> Please clear the cache if the settings wouldn't work!</div>", - ); - - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Save Settings', - ); - - return $form; -} - -/** - * Submit handler(). - */ -function http_auth_settings_form_submit($form, &$form_state) { - $http_auth['username'] = $form_state['values']['username']; - $http_auth['password'] = $form_state['values']['password']; - $http_auth['message'] = $form_state['values']['message']; - $http_auth['applicable'] = $form_state['values']['applicable']; - $http_auth['activate'] = $form_state['values']['activate']; - $http_auth_settings = serialize($http_auth); - variable_set('http_auth', $http_auth_settings); -} - -/** - * Implements hook_page_alter(). - */ -function http_auth_page_alter(&$page) { - global $user; - if (is_array($user->roles) && in_array('administrator', $user->roles)) { +function http_auth_page_top(array &$page_top) { + $user_roles = \Drupal::currentUser()->getRoles(); + if (is_array($user_roles) && in_array('administrator', $user_roles)) { return; } $realm = 'Restricted Page'; - $http_auth = variable_get('http_auth'); - if (!is_array($http_auth)) { - $http_auth = unserialize($http_auth); - } + $http_auth = \Drupal::config('http_auth.settings')->get(); if (isset($http_auth) && !empty($http_auth) && isset($http_auth['activate']) && $http_auth['activate'] == 1) { if ($http_auth['applicable'] == 'admin') { @@ -157,8 +32,9 @@ function http_auth_page_alter(&$page) { header('WWW-Authenticate: Basic realm="' . $realm . '"'); header('HTTP/1.0 401 Unauthorized'); if (empty($message)) { - $message = "This page is Restricted. Please contact the administrator for access."; + $message = 'This page is Restricted. Please contact the administrator for access.'; } + die(http_auth_cancel_page($message)); } } @@ -168,17 +44,17 @@ function http_auth_page_alter(&$page) { * Returns the page to the unauthenticated user. */ function http_auth_cancel_page($message = '') { - $sitename = variable_get('site_name'); - if ($sitename == '') { - $sitename = "Locked"; + $sitename = \Drupal::config('system.site')->get('name'); + if ($sitename === '') { + $sitename = 'Locked'; } return '<html> - <head> - <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> - <title>' . $sitename . ' | Restricted Site</title> - </head> - <body class="http-restricted"> - <p>' . $message . '</p> - </body> - </html>'; + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <title>' . $sitename . ' | Restricted Site</title> + </head> + <body class="http-restricted"> + <p>' . $message . '</p> + </body> + </html>'; } diff --git a/http_auth.permissions.yml b/http_auth.permissions.yml new file mode 100644 index 0000000000000000000000000000000000000000..2ab9e35ba4635b17283507cd8ad4aa230e260d9b --- /dev/null +++ b/http_auth.permissions.yml @@ -0,0 +1,4 @@ +http_auth_settings: + title: 'HTTP Auth' + description: 'Users who have this permission can change the HTTP Auth settings.' + restrict access: TRUE diff --git a/http_auth.routing.yml b/http_auth.routing.yml new file mode 100644 index 0000000000000000000000000000000000000000..745d11b6c44ea5e0670f3ca9ea58c58805167666 --- /dev/null +++ b/http_auth.routing.yml @@ -0,0 +1,7 @@ +http_auth.admin_settings: + path: '/admin/config/development/http-auth' + defaults: + _form: '\Drupal\http_auth\Form\HttpAuthSettings' + _title: 'HTTP Auth' + requirements: + _permission: 'http_auth_settings' diff --git a/src/Form/HttpAuthSettings.php b/src/Form/HttpAuthSettings.php new file mode 100644 index 0000000000000000000000000000000000000000..0f00507a4eb1af61ec5d1a27795febe9956a2b8e --- /dev/null +++ b/src/Form/HttpAuthSettings.php @@ -0,0 +1,119 @@ +<?php + +namespace Drupal\http_auth\Form; + +use Drupal\Core\Form\ConfigFormBase; +use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\Form\FormStateInterface; + +/** + * Provide settings page for applying Http Auth on your site. + */ +class HttpAuthSettings extends ConfigFormBase { + + /** + * Implements FormBuilder::getFormId. + */ + public function getFormId() { + return 'http_auth'; + } + + /** + * Implements ConfigFormBase::getEditableConfigNames. + */ + protected function getEditableConfigNames() { + return ['http_auth.settings']; + } + + /** + * Implements FormBuilder::buildForm. + */ + public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) { + $http_auth_section = $this->config('http_auth.settings')->get(); + $applicable = [ + 'complete' => $this->t('Complete Site'), + 'admin' => $this->t('Admin and User Pages'), + ]; + + $form['http_auth'] = [ + '#type' => 'fieldset', + '#title' => $this->t('Add HTTP Auth on your site'), + '#description' => $this->t('By activating, your site or admin pages would be <strong>locked</strong> for unauthenticated users.'), + ]; + + $form['http_auth']['username'] = [ + '#type' => 'textfield', + '#title' => $this->t('HTTP Auth Username'), + '#description' => $this->t('Add HTTP Auth username'), + '#default_value' => isset($http_auth_section['username']) ? $http_auth_section['username'] : '', + '#size' => 60, + '#maxlength' => 64, + '#required' => TRUE, + '#attributes' => [ + 'placeholder' => 'username', + ], + ]; + + $form['http_auth']['password'] = [ + '#type' => 'password', + '#title' => $this->t('HTTP Auth password'), + '#description' => $this->t('Add HTTP Auth password'), + '#size' => 60, + '#maxlength' => 64, + '#required' => TRUE, + '#attributes' => [ + 'placeholder' => 'password', + ], + ]; + + $form['http_auth']['message'] = [ + '#type' => 'textarea', + '#title' => $this->t('HTTP Auth Message'), + '#description' => $this->t('Add HTTP Auth message which would be shown to the unauthenticated users.'), + '#default_value' => isset($http_auth_section['message']) ? $http_auth_section['message'] : '', + '#attributes' => [ + 'placeholder' => $this->t('This page is Restricted. Please contact the administrator for access.'), + ], + ]; + + $form['http_auth']['applicable'] = [ + '#type' => 'radios', + '#title' => $this->t('Applicable on:'), + '#default_value' => isset($http_auth_section['applicable']) ? $http_auth_section['applicable'] : 'complete', + '#options' => $applicable, + ]; + + $form['http_auth']['activate'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Activate HTTP Authentication'), + '#default_value' => isset($http_auth_section['activate']) ? $http_auth_section['activate'] : 0, + ]; + + $form['http_auth']['note'] = [ + '#markup' => "<div><strong>Note:</strong> Please clear the cache if the settings wouldn't work!</div>", + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * Implements FormBuilder::submitForm(). + * + * Save the HTTP Auth Details to to the Drupal's config Table. + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValues(); + + $this->configFactory() + ->getEditable('http_auth.settings') + ->set('username', $values['username']) + ->set('password', $values['password']) + ->set('message', $values['message']) + ->set('applicable', $values['applicable']) + ->set('activate', $values['activate']) + ->save(); + + $this->messenger()->addStatus($this->t('Your Settings have been saved.')); + } + +}