| 1 |
<?php
|
| 2 |
// $Id: r4032login.module,v 1.8 2009/07/14 20:35:43 deekayen Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Redirect denied pages to the user login form.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu().
|
| 11 |
*/
|
| 12 |
function r4032login_menu() {
|
| 13 |
$items = array();
|
| 14 |
$items['r4032login'] = array(
|
| 15 |
'page callback' => 'r4032login_redirect',
|
| 16 |
'access callback' => 'r4032login_access',
|
| 17 |
'type' => MENU_CALLBACK
|
| 18 |
);
|
| 19 |
$items['admin/settings/r4032login'] = array(
|
| 20 |
'title' => 'Redirect 403 to User Login',
|
| 21 |
'description' => 'Toggle display of denied message on login page.',
|
| 22 |
'page callback' => 'drupal_get_form',
|
| 23 |
'page arguments' => array('r4032login_admin_settings'),
|
| 24 |
'access callback' => 'user_access',
|
| 25 |
'access arguments' => array('administer site configuration')
|
| 26 |
);
|
| 27 |
return $items;
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Without an access callback on the menu item for r4032login_redirect,
|
| 32 |
* the redirect will be 403 and just have the default access denied page anyway.
|
| 33 |
*/
|
| 34 |
function r4032login_access() {
|
| 35 |
return TRUE;
|
| 36 |
}
|
| 37 |
|
| 38 |
function r4032login_admin_settings() {
|
| 39 |
$form = array();
|
| 40 |
$form['r4032login_display_denied_message'] = array(
|
| 41 |
'#type' => 'textarea',
|
| 42 |
'#title' => t('Display access denied message on login page'),
|
| 43 |
'#default_value' => variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.')
|
| 44 |
);
|
| 45 |
$form['r4032login_user_register_message'] = array(
|
| 46 |
'#type' => 'textfield',
|
| 47 |
'#title' => t('User register message'),
|
| 48 |
'#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('new account registration form', 'user/register'), '!link' => l(t('User management'), 'admin/user/user/create'))),
|
| 49 |
'#default_value' => variable_get('r4032login_user_register_message', t('You are not authorized to access this page.'))
|
| 50 |
);
|
| 51 |
return system_settings_form($form);
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Implementation of hook_theme().
|
| 56 |
*/
|
| 57 |
function r4032login_theme() {
|
| 58 |
return array(
|
| 59 |
'r4032login_denied' => array(
|
| 60 |
'arguments' => array()
|
| 61 |
),
|
| 62 |
'r4032login_user_register' => array(
|
| 63 |
'arguments' => array()
|
| 64 |
)
|
| 65 |
);
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* MENU_CALLBACK for /4032login
|
| 70 |
*
|
| 71 |
* Redirect anonymous users from 403 Access Denied pages to the /user/login page
|
| 72 |
* with a message explaining that they must login to view the requested page
|
| 73 |
* and a query string parameter appended to the url to return after login.
|
| 74 |
*/
|
| 75 |
function r4032login_redirect() {
|
| 76 |
global $user;
|
| 77 |
if ($user->uid == 0) {
|
| 78 |
if (variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.')) {
|
| 79 |
drupal_set_message(variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.'), 'error');
|
| 80 |
}
|
| 81 |
// using drupal_goto() with destination set causes a recursive redirect loop
|
| 82 |
header('Location: '. url('user/login', array('query' => 'destination='. drupal_urlencode($_REQUEST['q']), 'absolute' => TRUE)), TRUE, 302);
|
| 83 |
exit;
|
| 84 |
}
|
| 85 |
// checking arg() returns r4032login
|
| 86 |
elseif ($_REQUEST['q'] == 'user/register') {
|
| 87 |
print theme('page', theme('r4032login_user_register'));
|
| 88 |
exit;
|
| 89 |
}
|
| 90 |
else {
|
| 91 |
print theme('page', theme('r4032login_denied'));
|
| 92 |
exit;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
function theme_r4032login_denied() {
|
| 97 |
drupal_set_title(t('Access denied'));
|
| 98 |
return '<p>'. t('You are not authorized to access this page.') .'</p>';
|
| 99 |
}
|
| 100 |
|
| 101 |
function theme_r4032login_user_register() {
|
| 102 |
drupal_set_title(t('Access denied'));
|
| 103 |
return '<p>'. variable_get('r4032login_user_register_message', t('You are not authorized to access this page.')) .'</p>';
|
| 104 |
}
|