You are here

r4032login.module in Redirect 403 to User Login 5

Redirect denied pages to the user login form.

File

r4032login.module
View source
<?php

/**
 * @file
 * Redirect denied pages to the user login form.
 */

/**
 * Implementation of hook_menu().
 */
function r4032login_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'r4032login',
      'callback' => 'r4032login_redirect',
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/settings/r4032login',
      'title' => t('Redirect 403 to User Login'),
      'description' => t('Toggle display of denied message on login page.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'r4032login_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
    );
  }
  return $items;
}
function r4032login_admin_settings() {
  $form = array();
  $form['r4032login_display_denied_message'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display access denied message on login page'),
    '#default_value' => variable_get('r4032login_display_denied_message', TRUE),
  );
  $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);
}

/**
 * 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->uid == 0) {
    if (variable_get('r4032login_display_denied_message', TRUE)) {
      drupal_set_message(t('Access denied. You must login to view this page.'), 'error');
    }

    // using drupal_goto() with destination set causes a recursive redirect loop
    header('Location: ' . url('user/login', 'destination=' . drupal_urlencode($_REQUEST['q']), NULL, TRUE), TRUE, 301);
    exit;
  }
  elseif ($_REQUEST['q'] == 'user/register') {
    print theme('page', theme('r4032login_user_register'));
    exit;
  }
  else {
    print theme('page', theme('r4032login_denied'));
    exit;
  }
}
function theme_r4032login_denied() {
  drupal_set_title(t('Access denied'));
  return '<p>' . t('You are not authorized to access this page.') . '</p>';
}
function theme_r4032login_user_register() {
  drupal_set_title(t('Access denied'));
  return '<p>' . variable_get('r4032login_user_register_message', t('You are not authorized to access this page.')) . '</p>';
}