Any tips? In my module http://www.drupal.org/project/clickbank_ipn the "After saving new content" event does not work because I am saving new content with "node_save"

Comments

giorgio79’s picture

Component: Rules Core » Rules Engine

Here is my rule as well.

The goal is to create a new user when a new Clickbank IPN node is saved. :)

array (
  'rules' => 
  array (
    'rules_1' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_node_insert',
      '#label' => 'Clickbank: Create New User',
      '#active' => 1,
      '#weight' => '0',
      '#categories' => 
      array (
        0 => 'clickbank',
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        0 => 
        array (
          '#type' => 'condition',
          '#settings' => 
          array (
            'type' => 
            array (
              'clickbank_ipn' => 'clickbank_ipn',
            ),
            '#argument map' => 
            array (
              'node' => 'node',
            ),
          ),
          '#name' => 'rules_condition_content_is_type',
          '#info' => 
          array (
            'label' => 'Created content is Clickbank IPN',
            'arguments' => 
            array (
              'node' => 
              array (
                'type' => 'node',
                'label' => 'Content',
              ),
            ),
            'module' => 'Node',
          ),
          '#weight' => 0,
        ),
      ),
      '#actions' => 
      array (
        0 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Create a user',
            'arguments' => 
            array (
              'username' => 
              array (
                'type' => 'string',
                'label' => 'User name',
              ),
              'email' => 
              array (
                'type' => 'string',
                'label' => 'User\'s E-mail',
              ),
            ),
            'new variables' => 
            array (
              'user_added' => 
              array (
                'type' => 'user',
                'label' => 'New user',
              ),
              'user_added_pw' => 
              array (
                'type' => 'string',
                'label' => 'New user\'s password',
              ),
            ),
            'module' => 'User',
          ),
          '#name' => 'rules_action_user_create',
          '#settings' => 
          array (
            'username' => '[node:field_ccustname-formatted]',
            'email' => '[node:field_ccustemail-raw]',
            '#argument map' => 
            array (
              'user_added' => 'user_added',
              'user_added_pw' => 'user_added_pw',
            ),
            '#eval input' => 
            array (
              'token_rules_input_evaluator' => 
              array (
                'username' => 
                array (
                  0 => 'node',
                ),
                'email' => 
                array (
                  0 => 'node',
                ),
              ),
            ),
          ),
          '#type' => 'action',
        ),
      ),
      '#version' => 6003,
    ),
  ),
)
giorgio79’s picture

I just tested the trigger and it does work fine when I create the node for the content type manually, yet for some reason it does not work when it is done programmatically with node_save. Bit baffled :)

giorgio79’s picture

Status: Active » Needs review

Ok, in my tests I succeeded with getting this to work by loading the admin user before node save like this
http://drupal.org/node/178506#comment-778935

Can someone comment on how secure this is and if this is the way to solve it?


global $user;
$user = user_load(1);
node_save_action($node);

giorgio79’s picture

In my code I assign uid 1 when creating the content, and tried changing that to 0 (anonymous) , perhaps it was some kind of permission issue but no luck.

Browsing around the issue queue I also found the node delete patch for Rules, which had permission issues as well, so implemented direct SQL queries. Seems a bit hackish :)

#400334: Action: delete node

 /**
+ * Action "Delete a node".
+ *
+ * We cannot use node_delete() because it does access checks and output.
+ * @see node_delete()
+ */
+function rules_action_delete_node($node) {
+  db_query('DELETE FROM {node} WHERE nid = %d', $node->nid);
+  db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid);
+
+  // Call the node-specific callback (if any):
+  node_invoke($node, 'delete');
+  node_invoke_nodeapi($node, 'delete');
+
+  // Clear the page and block caches.
+  cache_clear_all();

giorgio79’s picture

I guess I will implement it like this:

$user = user_load(1);
node_save_action($node);
$user = user_load(0);

In an attempt to make this somewhat secure :)

ionmedia’s picture

#4 fix issue with triggers after saving new node via node_save

giorgio79’s picture

Status: Needs review » Closed (fixed)

Thanks ionmedia, kind of forgot about this issue.

I also managed to solve it by assigning proper permissions, namely node creation for anonymous user :)