'r4032login_redirect', 'access callback' => 'r4032login_access', 'type' => MENU_CALLBACK ); $items['admin/settings/r4032login'] = array( 'title' => 'Redirect 403 to User Login', 'description' => 'Toggle display of denied message on login page.', 'page callback' => 'drupal_get_form', 'page arguments' => array('r4032login_admin_settings'), 'access callback' => 'user_access', 'access arguments' => array('administer site configuration') ); return $items; } /** * Without an access callback on the menu item for r4032login_redirect, * the redirect will be 403 and just have the default access denied page anyway. */ function r4032login_access() { return TRUE; } function r4032login_admin_settings() { $form = array(); $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.') ); $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.')) ); return system_settings_form($form); } /** * Implementation of hook_theme(). */ function r4032login_theme() { return array( 'r4032login_denied' => array( 'arguments' => array() ), 'r4032login_user_register' => array( 'arguments' => array() ) ); } /** * MENU_CALLBACK for /4032login * * Redirect anonymous users from 403 Access Denied pages to the /user/login page * with a message explaining that they must login to view the requested page * and a query string parameter appended to the url to return after login. */ function r4032login_redirect() { global $user; if (user_is_anonymous()) { if (variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.')) { drupal_set_message(variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.'), 'error'); } // A special case for the homepage. if (empty($_REQUEST['q'])) { $_REQUEST['q'] = ''; } // Check for path prefix and strip it out if its found. $path = _r4032login_remove_language(drupal_urlencode($_REQUEST['q'])); // using drupal_goto() with destination set causes a recursive redirect loop header('Location: '. url('user/login', array('query' => 'destination='. $path, 'absolute' => TRUE)), TRUE, 302); exit; } // checking arg() returns r4032login elseif ($_REQUEST['q'] == 'user/register') { print theme('page', theme('r4032login_user_register')); exit; } else { print theme('page', theme('r4032login_denied')); exit; } } /** * Special handling for sites that use localization. * * @see http://drupal.org/node/339120 */ function _r4032login_remove_language($destination) { $prefix = arg(0, $destination); $languages = language_list(); while (list(, $lang) = each($languages)) { if (strcasecmp($prefix, $lang->language) == 0) { return substr($destination, strlen($prefix) + 1); // remove prefix and slash } } return $destination; } function theme_r4032login_denied() { drupal_set_title(t('Access denied')); return '

'. t('You are not authorized to access this page.') .'

'; } function theme_r4032login_user_register() { drupal_set_title(t('Access denied')); return '

'. variable_get('r4032login_user_register_message', t('You are not authorized to access this page.')) .'

'; }