Commit 9c1eb22a authored by Bobík's avatar Bobík

Removed variable node_authlink_types, authkeys are generated in...

Removed variable node_authlink_types, authkeys are generated in hook_node_presave, support for Token API
parent fb2e1f8c
......@@ -68,21 +68,11 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) {
* Submit for node_type_form.
*/
function node_authlink_form_node_type_form_alter_submit(&$form, &$form_state) {
$node_types = variable_get('node_authlink_types', array());
// Enabled
if($form_state['values']['node_authlink_enable']) {
$node_types[$form_state['values']['type']] = $form_state['values']['type'];
}
// Disabled
elseif(!$form_state['values']['node_authlink_enable']) {
if(!$form_state['values']['node_authlink_enable']) {
variable_del('node_authlink_enable_' . $form_state['values']['type']);
variable_del('node_authlink_grants_' . $form_state['values']['type']);
unset($node_types[$form_state['values']['type']]);
}
variable_set('node_authlink_types', $node_types);
}
/**
......@@ -202,10 +192,24 @@ function node_authlink_node_access($node, $op, $account) {
return NODE_ACCESS_ALLOW;
}
/**
* Implementation of hook_node_presave().
*
* Pre-generate auth key for the new node (e.g. for use in Rules).
*/
function node_authlink_node_presave($node) {
// Ignore if node type is disabled
if(!variable_get('node_authlink_enable_' . $node->type, FALSE))
return;
// Generate key
$node->authkey = hash('sha256', drupal_random_bytes(64));
}
/**
* Implementation of hook_node_insert().
*
* Generate auth key for the new node.
* Generate and save auth key for the new node.
*/
function node_authlink_node_insert($node) {
// Allow key generate without load node object
......@@ -219,8 +223,8 @@ function node_authlink_node_insert($node) {
return;
}
// Generate new key
$authkey = hash('sha256', drupal_random_bytes(64));
// 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')
......@@ -261,4 +265,56 @@ function node_authlink_cron() {
}
}
/**
* Implementation of hook_token_info().
*/
function node_authlink_token_info() {
$node['authlink:authkey'] = array(
'name' => t("Authorization key"),
'description' => t("Key generated by Node authorize link module."),
);
$node['authlink:view-url'] = array(
'name' => t("View URL"),
'description' => t("URL with authorization key."),
);
$node['authlink:edit-url'] = array(
'name' => t("Edit URL"),
'description' => t("URL with authorization key."),
);
$node['authlink:delete-url'] = array(
'name' => t("Delete URL"),
'description' => t("URL with authorization key."),
);
return array(
'tokens' => array('node' => $node),
);
}
/**
* Implementation of hook_tokens().
*/
function node_authlink_tokens($type, $tokens, array $data = array(), array $options = array()) {
if($type != 'node')
return;
//dpm(func_get_args());
$replacements = array();
if(isset($tokens['authlink:authkey']))
$replacements['[node:authlink:authkey]'] = $data['node']->authkey;
if(isset($tokens['authlink:view-url']))
$replacements['[node:authlink:view-url]'] = node_authlink_get_url($data['node'], 'view');
if(isset($tokens['authlink:edit-url']))
$replacements['[node:authlink:edit-url]'] = node_authlink_get_url($data['node'], 'edit');
if(isset($tokens['authlink:delete-url']))
$replacements['[node:authlink:delete-url]'] = node_authlink_get_url($data['node'], 'delete');
return $replacements;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment