Commit 064e34bd authored by Bobík's avatar Bobík

Works giving all grants.

parents
name = Node access link
description = Generates link for view, edit, or delete node without login.
dependencies[] = computed_field
core = 7.x
files[] = node_access_link.module
\ No newline at end of file
<?php
// $Id$
/**
* @file
* TODO: Enter file description here.
*/
/**
* Alter of node_type_form.
*/
function node_access_link_form_node_type_form_alter(&$form, &$form_state) {
$form['node_access_link'] = array(
'#type' => 'fieldset',
'#title' => t('Node Auth Key'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$instance = field_info_instance('node', 'field_node_access_link_authkey', $form['#node_type']->type);
$form['node_access_link']['node_access_link_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#default_value' => (bool)$instance,
'#description' => t('Disable of this feature will cost erase of authorization keys of all nodes in this node type.'),
);
if(is_array($instance) && isset($instance['node_access_link_grants']))
$default_grants = array_keys($instance['node_access_link_grants'], TRUE);
else
$default_grants = array();
$form['node_access_link']['node_access_link_grants'] = array(
'#type' => 'checkboxes',
'#title' => t('Grants'),
'#default_value' => $default_grants,
'#options' => array(
'view' => t('View'),
'update' => t('Update'),
'delete' => t('Delete'),
),
'#description' => t('What operations will be temporarily given to authorised user for the node.'),
);
$form['#submit'][] = 'node_access_link_form_node_type_form_alter_submit';
}
/**
* Submit for node_type_form.
*/
function node_access_link_form_node_type_form_alter_submit(&$form, &$form_state) {
$instance = field_info_instance('node', 'field_node_access_link_authkey', $form_state['values']['type']);
if(!$instance && $form_state['values']['node_access_link_enable']) { // Instance does not exist and checkbox is enabled
node_access_link_create_field_instance($form_state['values']['type']);
}
elseif($instance && !$form_state['values']['node_access_link_enable']) { // Instance exists and checkbox is disabled
field_delete_instance($instance);
}
}
/**
* Create field instance in specified node type.
*/
function node_access_link_create_field_instance($node_type) {
if(!field_info_field('field_node_access_link_authkey')) {
$field = array(
'translatable' => '1',
'settings' => array(
'code' => '',
'display_format' => '$display_output = $entity_field_item[\'value\'];',
'store' => 1,
'database' => array(
'data_type' => 'varchar',
'data_length' => '64',
'data_size' => 'normal',
'data_precision' => '10',
'data_scale' => '2',
'data_default' => '',
'data_not_NULL' => 1,
'data_index' => 0,
),
),
'field_name' => 'field_node_access_link_authkey',
'type' => 'computed',
'active' => '1',
'locked' => '0',
'cardinality' => '1',
);
field_create_field($field);
}
$instance = array(
'label' => 'Node access link',
'widget' => array(
'type' => 'computed',
'weight' => '1',
'settings' => array(),
),
'settings' => array(
'user_register_form' => FALSE,
),
'display' => array(
'default' => array(
'label' => 'above',
'type' => 'hidden',
'weight' => '12',
'settings' => array(),
),
'teaser' => array(
'type' => 'hidden',
'label' => 'above',
'settings' => array(),
'weight' => 0,
),
),
'required' => FALSE,
'description' => '',
'field_name' => 'field_node_access_link_authkey',
'entity_type' => 'node',
'bundle' => $node_type,
);
field_create_instance($instance);
}
/**
* Implementation of hook_node_access().
*/
function node_access_link_node_access($node, $op, $account) {
// Ignore if just creating node
if($op == 'create')
return NODE_ACCESS_IGNORE;
// Check key
if(isset($_GET['authkey']) && isset($node->field_node_access_link_authkey)) {
$authkeys = field_get_items('node', $node, 'field_node_access_link_authkey');
if($authkeys[0]['value'] == $_GET['authkey']) {
// Start session
if(!isset($_SESSION))
drupal_session_initialize();
// Save authorization to session
$_SESSION['node_access_link_auth_nodes'][$node->nid] = TRUE;
}
}
// Permit if checked
if(isset($_SESSION['node_access_link_auth_nodes'][$node->nid]))
return NODE_ACCESS_ALLOW;
}
/**
* Computed field callback which generate auth key.
*/
function computed_field_field_node_access_link_authkey_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
// Break if set yet and entity is not node
if(!empty($entity_field[0]['value']) || $entity_type != 'node')
return;
// Find key in other languages
foreach($entity->field_node_access_link_authkey as $values) {
if(!empty($values[0]['value'])) {
$actual = $values[0]['value'];
break;
}
}
// Generate new key
if(!isset($actual))
$actual = hash('sha256', drupal_random_bytes(64));
$entity_field[0]['value'] = $actual;
}
/**
* Get edit URL of specified node.
* @param $node Node object or NID.
*/
function node_access_link_get_url($node) {
if(is_numeric($node))
$node = node_load($node);
$items = field_get_items('node', $node, 'field_node_access_link_authkey');
if(!$items)
return FALSE;
return url("node/$node->nid/edit", array(
'absolute' => TRUE,
'query' => array('authkey' => $items[0]['value']),
));
}
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