? usernode_assign_uid_cleanly_try2.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 14:16:52 -0000
@@ -33,21 +33,30 @@
 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.'),
   );
 
+  // temporarily store a newly registering user's user object,
+  // so that usernode_submit() can set the user's actual uid on node creation.
+  if ($node->user) {
+    $form['user'] = array(
+      '#type' => 'value',
+      '#value' => $node->user,
+    );
+  }
+
   return $form;
 }
 
@@ -214,31 +223,35 @@
 /**
  * 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),
+    'user' => $user,
   );
 
-  $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 user object of a newly registering user,
+ * so that the usernode is owned by the actual user instead of Anonymous.
+ */
+function usernode_submit(&$node) {
+  if ($node->type == USERNODE_CONTENT_TYPE && $node->user) {
+    $node->uid = $node->user->uid;
+  }
 }
 
 /**
