cvs diff: Diffing .
? .project
Index: masquerade.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.module,v
retrieving revision 1.16.2.29
diff -u -p -r1.16.2.29 masquerade.module
--- masquerade.module	2 Oct 2009 15:56:53 -0000	1.16.2.29
+++ masquerade.module	27 Oct 2009 20:50:07 -0000
@@ -62,11 +62,11 @@ function masquerade_cron() {
 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 && ($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,13 +173,13 @@ function masquerade_admin_settings() {
     '#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',
     '#title' => t('Menu <em>Quick Switch</em> user'),
     '#autocomplete_path' => 'masquerade/autocomplete',
-    '#default_value' => check_plain($test_name->name),
+    '#default_value' => isset($test_name->name) ? check_plain($test_name->name) : '',
     '#description' => t('Enter the username of an account you wish to switch easily between via a menu item.'),
     '#maxlength' => NULL,
   );
@@ -187,6 +187,9 @@ function masquerade_admin_settings() {
   $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,15 +210,17 @@ function masquerade_admin_settings() {
 
 function masquerade_admin_settings_validate($form, &$form_state) {
   unset($form);
-  $test_user = user_load(array('name' => $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'])));
+  if (!empty($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'])));
+    }
   }
 
   // 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 +232,7 @@ function masquerade_admin_settings_submi
   $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 +245,31 @@ function masquerade_admin_settings_submi
   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;
+/**
+ * Wrapper around user_load() to allow the loading of anonymous users.
+ * 
+ * @param $username
+ *   The username of the user you wish to load (i.e. $user->name). To load the
+ *   anonymous user, pass the value of the 'anonymous' variable.
+ * 
+ * @return
+ *   A fully-loaded $user object upon successful user load or FALSE if user
+ *   cannot be loaded.
+ */
+function _masquerade_user_load($username) {
+  if (!empty($username)) {
+    $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;
+  }
+  return FALSE;
 }
 
 /**
@@ -391,11 +414,15 @@ function masquerade_block_1($record) {
     }
 
     foreach ($masquerade_switches as $switch_user) {
-      if ($switch_user != $_SESSION['user']->uid) {
+      if (!isset($_SESSION['user']->uid) || $switch_user != $_SESSION['user']->uid) {
         $user_name = user_load(array('uid' => $switch_user));
         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 +535,10 @@ function masquerade_autocomplete_multipl
   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)) {
