You are here

function node_authlink_node_insert in Node authorize link 7

Implements hook_node_insert().

Generate and save auth key for the new node.

2 calls to node_authlink_node_insert()
node_authlink_batch_generate in ./node_authlink.module
Callback: Generate authkeys for all nodes in node type.
node_authlink_cron in ./node_authlink.module
Implements hook_cron().

File

./node_authlink.module, line 257
Provides functionality for the node_authlink module.

Code

function node_authlink_node_insert($node) {

  // Allow key generate without load node object.
  if (is_numeric($node)) {
    $nid = $node;
  }
  else {
    $nid = $node->nid;

    // Ignore if node type is disabled.
    if (!variable_get('node_authlink_enable_' . $node->type, FALSE)) {
      return;
    }
  }

  // Generate key if not yet.
  $authkey = isset($node->authkey) ? $node->authkey : hash('sha256', drupal_random_bytes(64));

  // Save to DB.
  db_insert('node_authlink_nodes')
    ->fields(array(
    'nid' => $nid,
    'authkey' => $authkey,
    'created' => time(),
  ))
    ->execute();
}