? usernode_assign_uid_cleanly.patch
Index: usernode.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/usernode/usernode.module,v
retrieving revision 1.12.2.10
diff -u -r1.12.2.10 usernode.module
--- usernode.module	2 Feb 2007 18:30:15 -0000	1.12.2.10
+++ usernode.module	5 Feb 2007 12:29:03 -0000
@@ -33,21 +33,31 @@
 function usernode_form(&$node) {
   $form['title'] = array(
     '#type' => 'hidden',
-    '#value' => $node->title
+    '#value' => $node->title,
   );
   $form['log'] = array(
     '#type' => 'fieldset',
     '#title' => t('Log message'),
     '#collapsible' => TRUE,
-    '#collapsed' => TRUE
+    '#collapsed' => TRUE,
   );
   $form['log']['message'] = array(
     '#type' => 'textarea',
     '#default_value' => $node->log,
     '#description' => t('An explanation of the additions or updates being made '.
-                        'to help other authors understand your motivations.')
+                        'to help other authors understand your motivations.'),
   );
 
+  // on node creation, temporarily store a newly registering user's actual uid,
+  // instead of the one for the currently logged in user.
+  // (see usernode_create_node() and usernode_submit() for details.)
+  if ($node->realuid) {
+    $form['realuid'] = array(
+      '#type' => 'value',
+      '#value' => $node->realuid,
+    );
+  }
+
   return $form;
 }
 
@@ -214,31 +224,36 @@
 /**
  * Create an associated node. Called by hook_user().
  */
-function usernode_create_node($account) {
+function usernode_create_node($user) {
 
   $node = array(
     'type' => USERNODE_CONTENT_TYPE,
-    'title' => check_plain($account->name),
+    'title' => check_plain($user->name),
+    'realuid' => $user->uid,
   );
 
-  $values = array(
-    'name' => $account->name,
-    'title' => check_plain($account->name),
-  );
+  $values = array();
 
   // workaround to disable drupal message "Your usernode has been created"
   $messages = drupal_get_messages();
-  // workaround to get permissions for creating the usernode in the first place
-  global $user;
-  $current_user = $user;
-  $user = $account;
 
   // create the usernode
   drupal_execute(USERNODE_CONTENT_TYPE .'_node_form', $values, $node);
 
-  // write back the old messages and user object
+  // write back the old messages
   $_SESSION['messages'] = $messages;
-  $user = $current_user;
+}
+
+/**
+ * Implementation of usernode_submit():
+ * Use the temporarily stored 'realuid' of a newly registering user,
+ * so that the usernode is owned by the actual user instead of Anonymous.
+ */
+function usernode_submit(&$node) {
+  if ($node->realuid) {
+    $node->uid = $node->realuid;
+    unset($node->realuid);
+  }
 }
 
 /**
