? modules/masquerade/masquerade.module0
Index: modules/masquerade/masquerade.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.module,v
retrieving revision 1.13
diff -u -r1.13 masquerade.module
--- modules/masquerade/masquerade.module	9 Jul 2006 16:55:17 -0000	1.13
+++ modules/masquerade/masquerade.module	26 Oct 2006 04:08:48 -0000
@@ -24,9 +24,8 @@
  */
 function masquerade_menu($may_cache) {
   $items = array();
+  $default_test_user = user_load(array('name' => variable_get('masquerade_test_user', '')));
   if ($may_cache) {
-    $default_test_user = user_load(array('name' => variable_get('masquerade_test_user', 0)));
-
     $items[] = array('path' => 'masquerade/switch', 
       'title' => t('switch user'),
       'callback' => 'masquerade_switch_user',
@@ -45,6 +44,12 @@
       'callback' => 'masquerade_switch_back',
       'access' => $GLOBALS['masquerading'],
       'type' => MENU_NORMAL_ITEM);
+
+    $items[] = array('path' => 'masquerade/autocomplete',
+	  'title' => t('masquerade autocomplete'),
+      'callback' => 'masquerade_autocomplete',
+	  'access' => $GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin')),
+      'type' => MENU_CALLBACK); 
   }
 
   return $items;
@@ -111,6 +116,95 @@
 }
 
 /**
+ * Implementation of hook_block()
+ */
+function masquerade_block($op = 'list', $delta = 0, $edit = array()) {
+  if ($op == 'list') {
+    $blocks[0] = array('info' => t('Masquerade'),
+      'weight' => 0, 'enabled' => 1, 'region' => 'left');
+    return $blocks;
+  }
+  else if ($op == 'configure' && $delta == 0) {
+    return;
+  }
+  else if ($op == 'view') {
+    if ($GLOBALS['masquerading'] || user_access('masquerade as user') || user_access('masquerade as admin')) {
+      switch($delta) {
+        case 0:
+          $block = array('subject' => t('Masquerade'),
+            'content' => _masquerade_block_1());
+          break;
+      }
+    return $block;
+	}
+  }
+} 
+
+/**
+ * masquerade block form
+ */
+function _masquerade_block_1() {
+  $attributes = $GLOBALS['masquerading'] ? array('disabled' => 'disabled') : array();
+  
+  if ($GLOBALS['masquerading']) {
+    global $user;
+    $markup_value = t('You are masquerading as:<br/>%masq_user.', array('%masq_user' => $user->name));
+    }else{$markup_value = t('Enter username to masquerade.<br/>Leave blank for %anonymous.', array('%anonymous' => theme('placeholder', variable_get('anonymous', 'Anonymous'))));
+}
+    $form['masquerade_user_field'] = array(
+      '#prefix' => '<div class="container-inline">',
+      '#type' => 'textfield',
+	  '#size' => '20',
+      '#default_value' => $GLOBALS['masquerading'] ? 'switch back to use' : variable_get('masquerade_user_field', ''),
+	  '#attributes' => $attributes,
+	  '#autocomplete_path' => 'masquerade/autocomplete',
+    );
+	$form['submit'] = array(
+	  '#type' => 'submit',
+	  '#value' => t('Go'),
+	  '#attributes' => $attributes,
+      '#suffix' => '</div>',
+	);
+	$form['masquerade_desc'] = array(
+	  '#prefix' => '<div class="form-item"><div class="description">',
+	  '#type' => 'markup',
+	  '#value' => $markup_value,
+      '#suffix' => '</div></div>',
+	);
+  $output = drupal_get_form('_masquerade_block_1', $form);
+
+  $links[] = $GLOBALS['masquerading'] ? l('switch back', 'masquerade/unswitch', array()) : l('quick switch', 'masquerade/quickswitch', array());
+    if ($links) {
+      $output .= theme('item_list', $links);
+    }
+
+  return $output;
+}
+
+/**
+ * masquerade block form submit
+ */
+function _masquerade_block_1_submit($form_id, $form_values) {
+  $masq_user = user_load(array('name' => $form_values['masquerade_user_field']));
+  masquerade_switch_user($masq_user->uid);
+  drupal_goto();
+}
+
+/**
+ * masquerade block form validation
+ */
+function _masquerade_block_1_validate($form_id, $form_value) {
+  global $user;
+  $masq_user = user_load(array('name' => $form_value['masquerade_user_field']));
+  if (!$masq_user) {
+    form_set_error('masquerade_user_field', t('User: %masquser does not exist. You must enter username of an existing account. Leave blank for %anonymous.', array('%anonymous' => theme('placeholder', variable_get('anonymous', 'Anonymous')), '%masquser' => $form_value['masquerade_user_field'])));
+  }
+  if ($masq_user->uid == $user->uid) {
+    form_set_error('masquerade_user_field', t('You cannot masquerade on yourself!'));
+  }
+}
+
+/**
  * Implementation of hook_settings()
  */
 function masquerade_settings() {
@@ -138,9 +232,10 @@
 
   $form['masquerade_test_user'] = array(
     '#type' => 'textfield',
-    '#title' => 'Default test user',
+    '#title' => t('Default test user'),
     '#default_value' => check_plain($test_user->name),
-    '#description' => 'Enter the username of an account you wish to switch easily between. The name must be an exisiting user.',
+	'#autocomplete_path' => 'masquerade/autocomplete',
+    '#description' => t('Enter the username of an account you wish to switch easily between. The name must be an exisiting user. Leave blank for %anonymous.', array('%anonymous' => theme('placeholder', variable_get('anonymous', 'Anonymous'))))
   );
   
   $form['#validate'] = array(
@@ -172,7 +267,7 @@
     'masquerade as admin' :
     'masquerade as user';
   // check to see if we need admin permission
-  if (!user_access($perm)) {
+  if (!user_access($perm) && !$GLOBALS['masquerading']) {
     return drupal_access_denied();
   }
 
@@ -187,8 +282,9 @@
     $user->uid, $new_user->uid, session_id());
   // switch user
 
-  watchdog('masquerade', "User '$user->name' now masquerading as '$new_user->name'");
-  drupal_set_message('Viewing site as user ' . $new_user->name . '.');
+  $masqas = $new_user->name ? $new_user->name : variable_get('anonymous', 'Anonymous');
+  watchdog('masquerade', "User '$user->name' now masquerading as '$masqas'.");
+  drupal_set_message('Browsing site as user: ' . $masqas . '.');
   $user->masquerading = $new_user->uid;
   $user = $new_user;
   drupal_goto(referer_uri());
@@ -205,10 +301,20 @@
   $uid = db_result(db_query("SELECT uid_from FROM {masquerade} WHERE sid = '%s' AND uid_as = %d ", session_id(), $user->uid));
   // erase record
   db_query("DELETE FROM {masquerade} WHERE sid = '%s' AND uid_as = %d ", session_id(), $user->uid);
-  $oldname = $user->name;
+  $oldname = $user->name ? $user->name : variable_get('anonymous', 'Anonymous');
   $user = user_load(array('uid' => $uid));
-  watchdog('masquerade', "User '$user->name' no longer masquerading as '$oldname'");
-  drupal_set_message('Viewing site as user ' . $user->name . '.');
+  watchdog('masquerade', "User '$user->name' no longer masquerading as '$oldname'.");
+  drupal_set_message('Browsing site as user ' . $user->name . '.');
   drupal_goto(referer_uri());
 }
 
+function masquerade_autocomplete($string) {
+  $matches = array();
+  $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10);
+  while ($user = db_fetch_object($result)) {
+    $matches[$user->name] = check_plain($user->name);
+  }
+  print drupal_to_js($matches);
+  exit();
+}
+
