You are here

function tokenauth_init in Token authentication 6

Same name and namespace in other branches
  1. 5 tokenauth.module \tokenauth_init()
  2. 6.2 tokenauth.module \tokenauth_init()

Implementation of hook_init().

File

./tokenauth.module, line 82

Code

function tokenauth_init() {
  global $user;

  // Process any provided token and log in user
  $key = tokenauth_get_token_key();
  if (user_is_anonymous() && isset($_REQUEST[$key]) && tokenauth_allowed_pages($_GET['q'])) {
    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;
        if (function_exists('session_save_session')) {
          session_save_session(FALSE);
        }
        elseif (function_exists('drupal_save_session')) {
          drupal_save_session(FALSE);
        }
        watchdog('user', 'Page @page loaded for %name via token authentication.', array(
          '@page' => $_GET['q'],
          '%name' => $account->name,
        ));
      }
    }

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

  // 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']));
  }
}