Index: masquerade.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.module,v
retrieving revision 1.16.2.29
diff -u -r1.16.2.29 masquerade.module
--- masquerade.module   2 Oct 2009 15:56:53 -0000   1.16.2.29
+++ masquerade.module   23 Oct 2009 08:47:26 -0000
@@ -62,11 +62,11 @@
 function masquerade_menu() {
   $items = array();
 
-  $default_test_user = _masquerade_test_user();
-  if ($default_test_user->uid) {
+  $default_test_user = _masquerade_user_load(variable_get('masquerade_test_user', ''));
+  if ($default_test_user->uid || $default_test_user->name == variable_get('anonymous', t('Anonymous'))) {
     $items['masquerade/switch/' . $default_test_user->uid] = array(
       'title' => 'Masquerade as @testuser',
-     'title arguments' => array('@testuser' => $default_test_user->name),
+      'title arguments' => array('@testuser' => $default_test_user->name),
       'page callback' => 'masquerade_switch_user',
       'page arguments' => array(2),
        'access callback' => 'masquerade_access',
@@ -173,7 +173,7 @@
     '#default_value' => variable_get('masquerade_admin_roles', array()),
   );
 
-  $test_name = _masquerade_test_user();
+  $test_name = _masquerade_user_load(variable_get('masquerade_test_user', ''));
 
   $form['masquerade_test_user'] = array(
     '#type' => 'textfield',
@@ -187,6 +187,9 @@
   $quick_switch_users = array();
   foreach ((variable_get('masquerade_quick_switches', array())) as $uid) {
     $u = user_load(array('uid' => $uid));
+    if( $uid == 0 ) {
+      $u->name = variable_get('anonymous', t('Anonymous'));
+    }
     $quick_switch_users[] = $u->name;
   }
   $form['masquerade_quick_switches'] = array(
@@ -207,7 +210,7 @@
 
 function masquerade_admin_settings_validate($form, &$form_state) {
   unset($form);
-  $test_user = user_load(array('name' => $form_state['values']['masquerade_test_user']));
+  $test_user = _masquerade_user_load($form_state['values']['masquerade_test_user']);
   if (!$test_user) {
     form_set_error('masquerade_test_user', t('%user does not exist. Please enter a valid username.', array('%user' => $form_state['values']['masquerade_test_user'])));
   }
@@ -215,7 +218,7 @@
   // A comma-separated list of users.
   $masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']);
   foreach ($masquerade_switches as $switch_user) {
-    $test_user = user_load(array('name' => $switch_user));
+    $test_user = _masquerade_user_load($switch_user);
     if (!$test_user) {
       form_set_error('masquerade_quick_switches', t('%user does not exist. Please enter a valid username.', array('%user' => $switch_user)));
     }
@@ -227,7 +230,7 @@
   $masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']);
   $masquerade_uids = array();
   foreach ($masquerade_switches as $masquerade_name) {
-    $u = user_load(array('name' => $masquerade_name));
+    $u = _masquerade_user_load($masquerade_name);
     $masquerade_uids[] = $u->uid;
   }
   variable_set('masquerade_quick_switches', $masquerade_uids);
@@ -240,13 +243,32 @@
   menu_rebuild();
 }
 
-function _masquerade_test_user() {
-  $test_user->uid = 0;
-  $test_user->name = '';
-
-  $test_user = user_load(array('name' => variable_get('masquerade_test_user', $test_user->name)));
-
-  return $test_user;
+/**
+ * Load users to accomodate for the anonymous 'user'.
+ * 
+ * @param $username
+ *   The username of the user you wish to load (i.e. $user->name).
+ * 
+ * @return
+ *   The loaded user (or an anonymous user: name='', uid=0) or true if $username is empty
+ */
+function _masquerade_user_load($username) {
+  if ($username == '') {
+    // return true here so that masquerade_admin_settings_validate() will not set a form error if $username is empty
+    return true;
+  }
+  else {
+    $user = '';
+    $anon = variable_get('anonymous', t('Anonymous'));
+    if ($username == $anon) {
+      $user = user_load(array('name' => ''));
+      $user->name = $anon;
+    }
+    else {
+      $user = user_load(array('name' => $username));
+    }
+    return $user;
+  }
 }
 
 /**
@@ -396,6 +418,10 @@
         if ($user_name->uid) {
           $quick_switch_link[] = l($user_name->name, 'masquerade/switch/'. $user_name->uid);
         }
+        if( $switch_user == 0 ) {
+          $user_name->name = variable_get('anonymous', t('Anonymous'));
+        }
+        $quick_switch_link[] = l($user_name->name, 'masquerade/switch/'. $user_name->uid);
       }
     }
 
@@ -508,6 +534,10 @@
   while ($user = db_fetch_object($result)) {
     $matches[$prefix . $user->name] = check_plain($user->name);
   }
+  // This will add anonymous to the list, but not sorted
+  if (stripos(variable_get('anonymous', t('Anonymous')), $last_string) === 0) {
+    $matches[$prefix . variable_get('anonymous', t('Anonymous'))] = variable_get('anonymous', t('Anonymous'));
+  }
   if (module_exists('alt_login')) {
     $result = db_query_range("SELECT alt_login FROM {alt_login} u WHERE LOWER(alt_login) LIKE LOWER('%s%%')", $string, 0, 10);
     while ($user = db_fetch_object($result)) {

Success, CVS operation completed
