Updated r4032login module from 6.x-1.4 to 6.x-1.5

This commit is contained in:
Oliver Bock 2013-10-29 16:59:26 +00:00
parent 2ac408ad76
commit 542f62e291
3 changed files with 100 additions and 20 deletions

View File

@ -1,9 +1,9 @@
name = Redirect 403 to User Login
description = Redirect anonymous users from 403 Access Denied pages to the /user/login page.
core = 6.x
; Information added by drupal.org packaging script on 2011-09-28
version = "6.x-1.4"
; Information added by drupal.org packaging script on 2013-10-24
version = "6.x-1.5"
core = "6.x"
project = "r4032login"
datestamp = "1317225408"
datestamp = "1382632830"

View File

@ -21,4 +21,7 @@ function r4032login_uninstall() {
variable_del('r4032login_display_denied_message');
variable_del('r4032login_user_register_message');
variable_del('r4032login_redirect_authenticated_users_to');
variable_del('r4032login_user_login_path');
variable_del('r4032login_default_redirect_code');
variable_del('r4032login_match_noredirect_pages');
}

View File

@ -39,20 +39,45 @@ function r4032login_admin_settings() {
$form['r4032login_display_denied_message'] = array(
'#type' => 'textarea',
'#title' => t('Display access denied message on login page'),
'#default_value' => variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.')
'#default_value' => variable_get('r4032login_display_denied_message', t('Access denied. You must login to view this page.')),
);
$form['r4032login_user_register_message'] = array(
'#type' => 'textfield',
'#title' => t('User register message'),
'#description' => t('The message to display when a logged-in user tries to register another account through the !new_account. Drupal does not allow it, so regular users must log out first. Administrators should create new users in !link.', array('!new_account' => l(t('new account registration form'), 'user/register'), '!link' => l(t('User management'), 'admin/user/user/create'))),
'#default_value' => variable_get('r4032login_user_register_message', t('You are not authorized to access this page.'))
'#default_value' => variable_get('r4032login_user_register_message', t('You are not authorized to access this page.')),
);
$form['r4032login_redirect_authenticated_users_to'] = array(
'#type' => 'textfield',
'#title' => t('Rediect authenticated users to'),
'#title' => t('Redirect authenticated users to'),
'#description' => t('If an authenticated user tries to access a page they can not, redirect them to the given page. Use <front> for the front page, leave blank for the default access denied page.'),
'#default_value' => variable_get('r4032login_redirect_authenticated_users_to', ''),
);
$form['r4032login_user_login_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to user login form'),
'#description' => t('The path to the user login form. Omit the beginning slash, ie: user/login'),
'#default_value' => variable_get('r4032login_user_login_path', 'user/login'),
);
$options = array('301' => '301 Moved Permanently', '302' => '302 Found');
$form['r4032login_default_redirect_code'] = array(
'#type' => 'select',
'#title' => t("HTTP redirect code"),
'#description' => t('The redirect code to send. 301 responses may be cached by browsers and proxies, so 302 is normally the correct choice.'),
'#options' => $options,
'#default_value' => variable_get('r4032login_default_redirect_code', 302),
);
$form['matching_paths'] = array(
'#type' => 'fieldset',
'#title' => t('Skip redirect for matching pages'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['matching_paths']['r4032login_match_noredirect_pages'] = array(
'#type' => 'textarea',
'#default_value' => variable_get('r4032login_match_noredirect_pages', ''),
'#description' => t('Instead of redirecting, the user will get an access deined response and see the standard login form. This may be useful when the response code is important - such as for removing outdated content from search engines. Use the path node/* for all content.') . ' ' . t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
);
return system_settings_form($form);
}
@ -82,36 +107,88 @@ function r4032login_redirect() {
$redirect = variable_get('r4032login_redirect_authenticated_users_to', '');
if (user_is_anonymous()) {
// Only display the message if there is one.
$message = variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.');
if (!empty($message)) {
drupal_set_message(t($message), 'error');
$message = variable_get('r4032login_display_denied_message', t('Access denied. You must login to view this page.'));
if (!empty($message) && empty($_POST)) {
drupal_set_message($message, 'error');
}
$page_match = FALSE;
$pages = variable_get('r4032login_match_redirect_pages', '');
if ($pages) {
// When on an access denied page, Drupal stores the original path in
// $_REQUEST['destination'] in drupal_access_denied().
// Convert the Drupal path to lowercase
$path = drupal_strtolower(drupal_get_path_alias($_REQUEST['destination']));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_REQUEST['destination']) {
$page_match = $page_match || drupal_match_path($_REQUEST['destination'], $pages);
}
}
if ($page_match) {
// Display the default login page.
return drupal_get_form('user_login');
}
// Handle redirection to the login form.
// using drupal_goto() with destination set causes a recursive redirect loop
header('Location: '. url('user/login', array('query' => drupal_get_destination(), 'absolute' => TRUE)), TRUE, 302);
exit;
$login_path = variable_get('r4032login_user_login_path', 'user/login');
$code = variable_get('r4032login_default_redirect_code', 302);
header('Location: ' . url($login_path, array('query' => _r4032login_destination(), 'absolute' => TRUE)), TRUE, $code);
_r4032login_exit();
}
elseif (!empty($redirect)) {
header('Location: '. url($redirect));
exit;
header('Location: ' . url($redirect));
_r4032login_exit();
}
// checking arg() returns r4032login
elseif ($_REQUEST['q'] == 'user/register') {
print theme('page', theme('r4032login_user_register'));
exit;
return theme('r4032login_user_register');
}
else {
print theme('page', theme('r4032login_denied'));
exit;
return theme('r4032login_denied');
}
}
/**
* Version of drupal_get_destination which works inside of a 403 callback
*
*/
function _r4032login_destination() {
// When on an access denied page, Drupal stores the original path in
// $_REQUEST['destination'] in drupal_access_denied().
$path = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : '';
$query = drupal_query_string_encode($_GET, array('q'));
if ($query != '') {
$path .= '?' . $query;
}
return 'destination=' . urlencode($path);
}
function theme_r4032login_denied() {
drupal_set_title(t('Access denied'));
return '<p>'. t('You are not authorized to access this page.') .'</p>';
return '<p>' . t('You are not authorized to access this page.') . '</p>';
}
function theme_r4032login_user_register() {
drupal_set_title(t('Access denied'));
$message = variable_get('r4032login_user_register_message', 'You are not authorized to access this page.');
return '<p>'. t($message) .'</p>';
$message = variable_get('r4032login_user_register_message', t('You are not authorized to access this page.'));
return '<p>' . t($message) . '</p>';
}
/**
* Performs end-of-request tasks.
*
* Code borrowed from drupal_goto() because drupal_exit() doesn't exist in 6.x.
*/
function _r4032login_exit($url = NULL) {
// Allow modules to react to the end of the page request before redirecting.
// We do not want this while running update.php.
if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
module_invoke_all('exit', $url);
}
// Even though session_write_close() is registered as a shutdown function, we
// need all session data written to the database before redirecting.
session_write_close();
}