Index: auto_username.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/auto_username/auto_username.module,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 auto_username.module
--- auto_username.module	14 Apr 2008 15:32:59 -0000	1.1.2.1
+++ auto_username.module	22 Jul 2009 17:58:11 -0000
@@ -124,7 +124,12 @@ function auto_username_user($op, &$edit,
 
   switch ($op) {
     case 'validate':
-      $new_name = _auto_username_patternprocessor($account);
+      $new_name = _auto_username_patternprocessor((object)($edit + (array)$account));
+
+      if (arg(1) != 'register' && ($new_name == $account->name || empty($new_name))) {
+        $new_name == NULL;
+        return;
+      }
 
       // Borrowed from _user_edit_validate().
       if ($error = user_validate_name($new_name)) {
@@ -144,7 +149,9 @@ function auto_username_user($op, &$edit,
       break;
 
     case 'insert':
-      db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array($new_name, $account->uid));
+      if (!empty($new_name)) {
+        db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array($new_name, $account->uid));
+      }
       break;
 
     case 'after_update':
@@ -152,7 +159,7 @@ function auto_username_user($op, &$edit,
       // We have to use after_update here instead of the 'update' op because
       // update happens before user module does its own saving.  Anything we do
       // to the users table in op update would be overwritten.
-      if (variable_get('aun_update_on_edit', 1)) {
+      if (variable_get('aun_update_on_edit', 1) && !empty($new_name)) {
         db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array($new_name, $account->uid));
       }
       break;
