Commit 693dfc8e authored by Bobík's avatar Bobík
Browse files

Fixes #1185300.

parent f1c980c0
...@@ -2,15 +2,82 @@ ...@@ -2,15 +2,82 @@
// $Id$ // $Id$
/** /**
* Inplementation of hook_install. * Implementation of hook_schema().
*/
function node_authlink_schema() {
$schema['node_authlink_nodes'] = array(
'description' => 'Table for store authorization keys.',
'fields' => array(
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
'authkey' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE),
'created' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
),
'primary key' => array('nid'),
);
return $schema;
}
/**
* Inplementation of hook_install().
*/ */
function node_authlink_install() { function node_authlink_install() {
drupal_set_message(t('To setup Node authorize link module go to Structure → Node types → edit → Node authorize link.')); drupal_set_message(t('To setup Node authorize link module go to Structure → Node types → edit → Node authorize link.'));
} }
/** /**
* Inplementation of hook_uninstall. * Inplementation of hook_update_N().
*/ */
function node_authlink_uninstall() { function node_authlink_update_7100(&$sandbox) {
// Migrate data
drupal_install_schema('node_authlink');
$keys = db_query('SELECT f.field_node_authlink_authkey_value, f.entity_id, n.created FROM
{field_data_field_node_authlink_authkey} AS f INNER JOIN {node} AS n ON f.entity_id =
n.nid WHERE f.language = \'und\'');
foreach ($keys as $key) {
db_insert('node_authlink_nodes')
->fields(array(
'nid' => $key->entity_id,
'authkey' => $key->field_node_authlink_authkey_value,
'created' => $key->created,
))
->execute();
}
unset($keys);
// Migrate settings
$field = field_info_field('field_node_authlink_authkey');
$instances = field_read_instances(array('field_id' => $field['id'], 'entity_type' => 'node'));
$node_types = array();
foreach ($instances as $instance) {
$node_types[$instance['bundle']] = $instance['bundle'];
variable_set('node_authlink_enable_' . $instance['bundle'], TRUE);
variable_set('node_authlink_grants_' . $instance['bundle'], $instance['node_authlink_grants']);
}
variable_set('node_authlink_types', $node_types);
// Cleanup
field_delete_field('field_node_authlink_authkey'); field_delete_field('field_node_authlink_authkey');
} }
\ No newline at end of file
/**
* Inplementation of hook_uninstall().
*/
function node_authlink_uninstall() {
$node_types = variable_get('node_authlink_types', array());
variable_del('node_authlink_types');
foreach ($node_types as $node_type)
variable_del('node_authlink_type_' . $node_type . '_grants');
}
...@@ -13,24 +13,17 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) { ...@@ -13,24 +13,17 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) {
'#group' => 'additional_settings', '#group' => 'additional_settings',
); );
$instance = field_info_instance('node', 'field_node_authlink_authkey', $form['#node_type']->type);
$form['node_authlink']['node_authlink_enable'] = array( $form['node_authlink']['node_authlink_enable'] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => t('Enable'), '#title' => t('Enable'),
'#default_value' => (bool)$instance, '#default_value' => variable_get('node_authlink_enable_' . $form['#node_type']->type, FALSE),
'#description' => t('Disable of this feature will cost erase of authorization keys of all nodes in this node type.'), '#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_authlink_grants']))
$default_grants = array_keys($instance['node_authlink_grants'], TRUE);
else
$default_grants = array();
$form['node_authlink']['node_authlink_grants'] = array( $form['node_authlink']['node_authlink_grants'] = array(
'#type' => 'checkboxes', '#type' => 'checkboxes',
'#title' => t('Grants to give'), '#title' => t('Grants to give'),
'#default_value' => $default_grants, '#default_value' => variable_get('node_authlink_grants_' . $form['#node_type']->type, array()),
'#options' => array( '#options' => array(
'view' => t('View'), 'view' => t('View'),
'update' => t('Update'), 'update' => t('Update'),
...@@ -46,90 +39,66 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) { ...@@ -46,90 +39,66 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) {
* Submit for node_type_form. * Submit for node_type_form.
*/ */
function node_authlink_form_node_type_form_alter_submit(&$form, &$form_state) { function node_authlink_form_node_type_form_alter_submit(&$form, &$form_state) {
$instance = field_info_instance('node', 'field_node_authlink_authkey', $form_state['values']['type']); $node_types = variable_get('node_authlink_types', array());
// Instance does not exist and checkbox is enabled - create filed and instance // Enabled
if(!$instance && $form_state['values']['node_authlink_enable']) { if($form_state['values']['node_authlink_enable']) {
node_authlink_create_field_instance($form_state['values']['type'], $form_state['values']['node_authlink_grants']); $node_types[$form_state['values']['type']] = $form_state['values']['type'];
} }
// Instance exists and checkbox is enabled - update instance // Disabled
elseif($instance && $form_state['values']['node_authlink_enable']) { elseif(!$form_state['values']['node_authlink_enable']) {
$instance['node_authlink_grants'] = $form_state['values']['node_authlink_grants']; variable_del('node_authlink_enable_' . $form_state['values']['type']);
field_update_instance($instance); variable_del('node_authlink_grants_' . $form_state['values']['type']);
unset($node_types[$form_state['values']['type']]);
} }
// Instance exists and checkbox is disabled - delete filed and instance variable_set('node_authlink_types', $node_types);
elseif($instance && !$form_state['values']['node_authlink_enable']) {
field_delete_instance($instance);
}
} }
/** /**
* Create field instance in specified node type. * Implementation of hook_node_load().
*
* Appends authke to loaded node object.
*/ */
function node_authlink_create_field_instance($node_type, $grants) { function node_authlink_node_load($nodes, $types) {
if(!field_info_field('field_node_authlink_authkey')) { foreach ($nodes as $nid => $node) {
$field = array( // TODO: check node type (performance)
'translatable' => '1', if($authkey = node_authlink_load_authkey($nid))
'settings' => array( $nodes[$nid]->authkey = $authkey;
'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_authlink_authkey',
'type' => 'computed',
'active' => '1',
'locked' => '0',
'cardinality' => '1',
);
field_create_field($field);
} }
}
/**
* Loads key from NID.
*/
function node_authlink_load_authkey($nid) {
$result = db_query('SELECT authkey FROM {node_authlink_nodes} WHERE nid = :nid', array(':nid' => $nid));
return $result->fetchField();
}
/**
* Get edit URL of specified node.
* @param $node Node object or NID.
* @param $op Operation to do with node. view, edit (default) or delete.
*/
function node_authlink_get_url($node, $op = 'edit') {
if(is_numeric($node))
$node = node_load($node);
$instance = array( if(!isset($node->authkey))
'label' => 'Node authorize key', return FALSE;
'widget' => array(
'type' => 'computed', if($op == 'view')
'weight' => '1', $op = '';
'settings' => array(), else
), $op = '/' . $op;
'settings' => array(
'user_register_form' => FALSE, return url("node/$node->nid$op", array(
), 'absolute' => TRUE,
'display' => array( 'query' => array('authkey' => $node->authkey),
'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_authlink_authkey',
'entity_type' => 'node',
'bundle' => $node_type,
'node_authlink_grants' => $grants,
);
field_create_instance($instance);
} }
/** /**
...@@ -140,70 +109,51 @@ function node_authlink_node_access($node, $op, $account) { ...@@ -140,70 +109,51 @@ function node_authlink_node_access($node, $op, $account) {
if($op == 'create') if($op == 'create')
return NODE_ACCESS_IGNORE; return NODE_ACCESS_IGNORE;
// Ignore if node type is not enabled
if(!variable_get('node_authlink_enable_' . $node->type, FALSE))
return NODE_ACCESS_IGNORE;
// Check key if: // Check key if:
if(isset($_GET['authkey']) && // authkey param is set if(isset($_GET['authkey']) && // authkey param is set
isset($node->field_node_authlink_authkey) && // authkey in node is set isset($node->authkey)) { // authkey in node is set
!isset($_SESSION['node_authlink_auth_nodes'][$node->nid])) { // grants has not loaded yet
$authkeys = field_get_items('node', $node, 'field_node_authlink_authkey'); if($node->authkey == $_GET['authkey']) {
if($authkeys[0]['value'] == $_GET['authkey']) {
// Start session // Start session
if(!isset($_SESSION)) if(!isset($_SESSION))
drupal_session_initialize(); drupal_session_initialize();
// Load settings // Save allowed grants to session
$instance = field_info_instance('node', 'field_node_authlink_authkey', $node->type); $_SESSION['node_authlink_nodes'][$node->nid] = variable_get('node_authlink_grants_' . $node->type, array());
// Save authorization to session
$_SESSION['node_authlink_auth_nodes'][$node->nid] = $instance['node_authlink_grants'];
} }
} }
// Permit if checked // Permit if checked
if(isset($_SESSION['node_authlink_auth_nodes'][$node->nid]) && if(isset($_SESSION['node_authlink_nodes'][$node->nid]) &&
$_SESSION['node_authlink_auth_nodes'][$node->nid][$op]) in_array($op, $_SESSION['node_authlink_nodes'][$node->nid]))
return NODE_ACCESS_ALLOW; return NODE_ACCESS_ALLOW;
} }
/** /**
* Computed field callback which generate auth key. * Implementation of hook_node_insert().
*
* Generate auth key for the new node.
*/ */
function computed_field_field_node_authlink_authkey_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) { function node_authlink_node_insert($node) {
// Ignore if node type is disabled
// Break if set yet and entity is not node if(!variable_get('node_authlink_enable_' . $node->type, FALSE))
if(!empty($entity_field[0]['value']) || $entity_type != 'node')
return; return;
// Find key in other languages
foreach($entity->field_node_authlink_authkey as $values) {
if(!empty($values[0]['value'])) {
$actual = $values[0]['value'];
break;
}
}
// Generate new key // Generate new key
if(!isset($actual)) $authkey = hash('sha256', drupal_random_bytes(64));
$actual = hash('sha256', drupal_random_bytes(64));
// Save to DB
$entity_field[0]['value'] = $actual; db_insert('node_authlink_nodes')
->fields(array(
'nid' => $node->nid,
'authkey' => $authkey,
'created' => time(),
))
->execute();
} }
/**
* Get edit URL of specified node.
* @param $node Node object or NID.
*/
function node_authlink_get_url($node) {
if(is_numeric($node))
$node = node_load($node);
$items = field_get_items('node', $node, 'field_node_authlink_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