I discovered an error while using some other modules (advanced profile, node profile, etc), which modify the user edit form, in part due to the other recent change adding 'update' to the hook_user call.

If the mail field isn't defined/set as part of the submitted form, $namenew ends up (incorrectly) empty, causing the username to be set to "_1" (or similar)
The fix is pretty clear: add a check for $edit['mail'] being nonempty, otherwise, don't do anything.

function email_registration_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      if (!empty($edit['mail'])){
        $namenew = variable_get('email_registration_eq_email',FALSE) ? $edit['mail'] : preg_replace('/@.*$/', '', $edit['mail']);
        // if username generated from email record already exists, append underscore and number eg:(chris_123)
        if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
          // find the next number available to append to the name
          $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '^%s_[0-9]+$' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
          $nameidx = db_result(db_query($sql, $namenew));
          $namenew .= '_'. ($nameidx + 1);
        }
        // replace with generated username
        db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid);
      }
     break;
  }
  return;
}

Comments

sbandyopadhyay’s picture

I have folded this patch into a master patch to the 5.x-1.x-dev version for easy updating on several issues: #366504: Master patch combining several recent issues.

Christopher Herberte’s picture

Status: Needs review » Closed (fixed)

patch applied to head