You are here

function xmlsitemap_switch_user in XML sitemap 7.2

Same name and namespace in other branches
  1. 6.2 xmlsitemap.module \xmlsitemap_switch_user()

Set the current user stored in $GLOBALS['user'].

@todo Remove when https://www.drupal.org/node/287292 is fixed.

4 calls to xmlsitemap_switch_user()
XMLSitemapUnitTest::testSwitchUser in ./xmlsitemap.test
Tests for xmlsitemap_switch_user and xmlsitemap_restore_user().
xmlsitemap_custom_edit_link_form_validate in xmlsitemap_custom/xmlsitemap_custom.admin.inc
Edit Link Form Validate.
xmlsitemap_menu_xmlsitemap_process_menu_links in xmlsitemap_menu/xmlsitemap_menu.module
Process menu sitemap links.
xmlsitemap_restore_user in ./xmlsitemap.module
Restore the user that was originally loaded.

File

./xmlsitemap.module, line 1416
xmlsitemap XML sitemap

Code

function xmlsitemap_switch_user($new_user = NULL) {
  global $user;
  $user_original =& drupal_static(__FUNCTION__);
  if (!isset($new_user)) {
    if (isset($user_original)) {

      // Restore the original user.
      $user = $user_original;
      $user_original = NULL;
      drupal_save_session(TRUE);
    }
    else {
      return FALSE;
    }
  }
  elseif (is_numeric($new_user) && $user->uid != $new_user) {

    // Get the full user object.
    if (!$new_user) {
      $new_user = drupal_anonymous_user();
    }
    elseif (!($new_user = user_load($new_user))) {
      return FALSE;
    }

    // Backup the original user object.
    if (!isset($user_original)) {
      $user_original = $user;
      drupal_save_session(FALSE);
    }
    $user = $new_user;
  }
  elseif (is_object($new_user) && $user->uid != $new_user->uid) {

    // Backup the original user object.
    if (!isset($user_original)) {
      $user_original = $user;
      drupal_save_session(FALSE);
    }
    $user = $new_user;
  }
  else {
    return FALSE;
  }
  return $user;
}