You are here

function tokenauth_menu_get_item_alter in Token authentication 7

Implements hook_menu_get_item_alter().

File

./tokenauth.module, line 92

Code

function tokenauth_menu_get_item_alter(&$router_item, $path, $original_map) {
  global $user;

  // Only set user for current page, and only if tokenauth was not already processed.
  // The latter is to prevent redundant token authentication if menu_get_item cache is reset later.
  if ($path != current_path() && !isset($_SESSION['tokenauth_auth'])) {
    return;
  }

  // Process any provided token and log in user
  $key = tokenauth_get_token_key();
  if (user_is_anonymous() && isset($_REQUEST[$key]) && tokenauth_allowed_pages(current_path())) {
    if ($uid = tokenauth_get_user($_REQUEST[$key])) {
      $account = user_load($uid);
      if (user_access('access tokenauth', $account)) {
        $user = $account;

        // Store the fact that this user authenticated via token. Needed for logout.
        $_SESSION['tokenauth_auth'] = TRUE;
        drupal_save_session(FALSE);
        watchdog('user', 'Page @page loaded for %name via token authentication.', array(
          '@page' => current_path(),
          '%name' => $account->name,
        ));
      }
    }

    // Supplied an invalid token
    if (empty($_SESSION['tokenauth_auth'])) {

      // Setting access denied inside the menu router system creates recursions.
      // Real-time equivalent of hook_menu_alter().
      $router_item['access_callback'] = 0;
    }
  }

  // Trigger tokenauth context condition.
  if (module_exists('context') && function_exists('context_get_plugin') && ($plugin = context_get_plugin('condition', 'tokenauth_auth'))) {
    $plugin
      ->execute((int) isset($_SESSION['tokenauth_auth']));
  }
}