When trying to create a node using drupal_execute and providing the creating user's uid in the $form_values array, if the user has the "administer nodes" permission, uid is being reset if the username is not provided in the $form_values array.

This is the problem (line 1994 of node.module):

  if (user_access('administer nodes')) {
    // Populate the "authored by" field.
    if ($account = user_load(array('name' => $node->name))) {
      $node->uid = $account->uid;
    }
    else {
      $node->uid = 0;
    }
  }

This is applicable for 5.x, 6.x and 7.x.

I've attached a proposed patch. If it causes trouble with editing existing nodes, an additional condition (if node is new) may be required.

Shai

CommentFileSizeAuthor
node_module_node_submit.patch698 byteselectricmonk

Comments

electricmonk’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch failed testing.

advseb’s picture

subscribe, I think I'm also experiencing this problem while creating nodes through drupal_execute. Here is my forum post about this issue:

http://drupal.org/node/440598

I hope this could be fixed, because it is an annoying bug.

Sebastian

Dave Cohen’s picture

Regarding patch in original issue... I find it does not work in D6. In node_submit, $node->uid == 0 even when you attempt to populate it in drupal_execute(). However $node->name might be set, so I've patched my D6 to honor $node->name.


Index: htdocs/modules/node/node.module
===================================================================
--- htdocs/modules/node/node.module     (revision 3708)
+++ htdocs/modules/node/node.module     (working copy)
@@ -848,7 +848,9 @@
     }
   }

-  if (user_access('administer nodes')) {
+  // Patched by Dave to honor $node->uid, when called via drupal_execute().
+  //if (user_access('administer nodes')) {
+  if (user_access('administer nodes') || (isset($node->name) && $node->name)) {
     // Populate the "authored by" field.
     if ($account = user_load(array('name' => $node->name))) {
       $node->uid = $account->uid;

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.