Commit 9f044e07 authored by akalam's avatar akalam

Started porting to d8

parent 92ff514d
{
"name": "drupal/node_authlink",
"type": "drupal-module",
"description": "Integrate Media with Keepeek DAM",
"keywords": ["Drupal"],
"license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/node_authlink",
"minimum-stability": "dev",
"support": {
"issues": "https://www.drupal.org/project/issues/node_authlink",
"source": "http://cgit.drupalcode.org/node_authlink"
},
"repositories": [
{
"type": "vcs",
"url": "git@bitbucket.org:metadrop/node_authlink.git"
}
]
}
name = Node authorize link
description = Generates link for view, edit, or delete node without login.
version = 7.x-1.0
core = 7.x
files[] = node_authlink.module
\ No newline at end of file
name: Node authorize link
description: Generates link for view, edit, or delete node without login.
version: 8.x-1.0
core: 8.x
<?php
// $Id$
/**
* Implementation of hook_schema().
*/
function node_authlink_schema() {
$schema['node_authlink_nodes'] = array(
$schema['node_authlink_nodes'] = [
'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'),
);
'fields' => [
'nid' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE
],
'authkey' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0
],
],
'primary key' => ['nid'],
];
return $schema;
}
......@@ -22,67 +33,8 @@ function node_authlink_schema() {
* Inplementation of hook_install().
*/
function node_authlink_install() {
drupal_set_message(t('To setup Node authorize link module go to Structure → Content types → edit → Node authorize link.'));
}
/**
* Inplementation of hook_update_N().
*/
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');
}
function node_authlink_update_7101(&$sandbox) {
variable_del('node_authlink_types');
}
/**
* Inplementation of hook_uninstall().
*/
function node_authlink_uninstall() {
$node_types = node_type_get_types();
foreach ($node_types as $node_type) {
variable_del('node_authlink_enable_' . $node_type->type);
variable_del('node_authlink_grants_' . $node_type->type);
variable_del('node_authlink_expire_' . $node_type->type);
$messenger = \Drupal::messenger();
if (isset($message)) {
$messenger->addStatus(t('To setup Node authorize link module go to Structure → Content types → edit → Node authorize link.'));
}
}
<?php
// $Id$
use Drupal\Core\Access\AccessResult;
/**
* Alter of node_type_form.
......@@ -12,37 +13,37 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) {
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$form['node_authlink']['node_authlink_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Enable'),
'#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.'),
);
$form['node_authlink']['node_authlink_grants'] = array(
'#type' => 'checkboxes',
'#title' => t('Grants to give'),
'#type' => 'checkboxes',
'#title' => t('Grants to give'),
'#default_value' => variable_get('node_authlink_grants_' . $form['#node_type']->type, array()),
'#options' => array(
'view' => t('View'),
'update' => t('Update'),
'delete' => t('Delete'),
'update' => t('Update'),
'delete' => t('Delete'),
),
'#description' => t('What operations will be temporarily given to authorised user for the node. This not affect users who is authorised yet.'),
);
// Time periods: none, 1 day, 1 week, 4 weeks
$period = drupal_map_assoc(array(0, 86400, 604800, 2419200), 'format_interval');
$period[0] = '<' . t('disabled') . '>';
$form['node_authlink']['node_authlink_expire'] = array(
'#type' => 'select',
'#title' => t('Regenerate authkeys after'),
'#type' => 'select',
'#title' => t('Regenerate authkeys after'),
'#default_value' => variable_get('node_authlink_expire_' . $form['#node_type']->type, 0),
'#options' => $period,
'#description' => t('Keys older than selected time will be regenerated by cron run.'),
);
$form['node_authlink']['node_authlink_batch'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
......@@ -60,7 +61,7 @@ function node_authlink_form_node_type_form_alter(&$form, &$form_state) {
'#value' => t('Delete all authkeys'),
'#submit' => array('node_authlink_batch_delete'),
);
$form['#submit'][] = 'node_authlink_form_node_type_form_alter_submit';
}
......@@ -79,18 +80,18 @@ function node_authlink_form_node_type_form_alter_submit(&$form, &$form_state) {
* Generate authkeys for all nodes in node type.
*/
function node_authlink_batch_generate(&$form, &$form_state) {
// Load NIDs that are not in the authkeys table
// Load NIDs that are not in the authkeys table
$query = db_select('node', 'n');
$query->leftJoin('node_authlink_nodes', 'a', 'n.nid = a.nid');
$query->fields('n', array('nid'))
->condition('type', $form_state['values']['type'])
->isNull('authkey');
$nids = $query->execute()->fetchCol();
// Create keys
foreach ($nids as $nid)
node_authlink_node_insert($nid);
drupal_set_message(t('%num authkeys has been generated.', array('%num' => count($nids))));
}
......@@ -105,12 +106,12 @@ function node_authlink_batch_delete(&$form, &$form_state) {
->condition('type', $form_state['values']['type'])
->isNotNull('authkey');
$nids = $query->execute()->fetchCol();
// Delete keys
$count = db_delete('node_authlink_nodes')
->condition('nid', $nids, 'IN')
->execute();
drupal_set_message(t('%num authkeys has been deleted.', array('%num' => $count)));
}
......@@ -140,20 +141,20 @@ function node_authlink_load_authkey($nid) {
/**
* Get edit URL of specified node.
* @param $node Node object or NID.
* @param $op Operation to do with node. view, edit (default) or delete.
* @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);
if(!isset($node->authkey))
return FALSE;
if($op == 'view')
$op = '';
else
$op = '/' . $op;
return url("node/$node->nid$op", array(
'absolute' => TRUE,
'query' => array('authkey' => $node->authkey),
......@@ -161,35 +162,37 @@ function node_authlink_get_url($node, $op = 'edit') {
}
/**
* Implementation of hook_node_access().
* Implements hook_node_access().
*/
function node_authlink_node_access($node, $op, $account) {
function node_authlink_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
// Ignore if just creating node
if($op == 'create')
return NODE_ACCESS_IGNORE;
return AccessResult::neutral();
// Ignore if node type is not enabled
if(!variable_get('node_authlink_enable_' . $node->type, FALSE))
return NODE_ACCESS_IGNORE;
if(!variable_get('node_authlink_enable_' . $node->bundle(), FALSE))
return AccessResult::neutral();
// Check key if:
if(isset($_GET['authkey']) && // authkey param is set
isset($node->authkey)) { // authkey in node is set
isset($node->authkey)) { // authkey in node is set
if($node->authkey == $_GET['authkey']) {
// Start session
if(!isset($_SESSION))
drupal_session_initialize();
/** @var \Drupal\Core\Session\SessionManager $session_manager */
$session_manager = \Drupal::service('session_manager');
$session_manager->start();
// Save allowed grants to session
$_SESSION['node_authlink_nodes'][$node->nid] = variable_get('node_authlink_grants_' . $node->type, array());
$_SESSION['node_authlink_nodes'][$node->nid] = variable_get('node_authlink_grants_' . $node->bundle(), array());
}
}
// Permit if checked
if(isset($_SESSION['node_authlink_nodes'][$node->nid]) &&
in_array($op, $_SESSION['node_authlink_nodes'][$node->nid]))
return NODE_ACCESS_ALLOW;
if(isset($_SESSION['node_authlink_nodes'][$node->id()]) &&
in_array($op, $_SESSION['node_authlink_nodes'][$node->id()]))
return AccessResult::allowed();
}
/**
......@@ -201,7 +204,7 @@ 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));
}
......@@ -217,19 +220,19 @@ function node_authlink_node_insert($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,
'nid' => $nid,
'authkey' => $authkey,
'created' => time(),
))
......@@ -241,12 +244,12 @@ function node_authlink_node_insert($node) {
*/
function node_authlink_cron() {
$node_types = node_type_get_types();
foreach ($node_types as $type) {
$expire = variable_get('node_authlink_expire_' . $type->type, 0);
if(!$expire)
continue;
// NIDs of expired keys
$query = db_select('node', 'n');
$query->leftJoin('node_authlink_nodes', 'a', 'n.nid = a.nid');
......@@ -254,7 +257,7 @@ function node_authlink_cron() {
->condition('n.type', $type->type)
->condition('a.created', time() - $expire, '<');
$nids = $query->execute()->fetchCol();
// Regenerate keys
foreach ($nids as $nid) {
db_delete('node_authlink_nodes')
......@@ -270,19 +273,19 @@ function node_authlink_cron() {
*/
function node_authlink_token_info() {
$node['authlink:authkey'] = array(
'name' => t("Authorization key"),
'name' => t("Authorization key"),
'description' => t("Key generated by Node authorize link module."),
);
$node['authlink:view-url'] = array(
'name' => t("View URL"),
'name' => t("View URL"),
'description' => t("URL with authorization key."),
);
$node['authlink:edit-url'] = array(
'name' => t("Edit URL"),
'name' => t("Edit URL"),
'description' => t("URL with authorization key."),
);
$node['authlink:delete-url'] = array(
'name' => t("Delete URL"),
'name' => t("Delete URL"),
'description' => t("URL with authorization key."),
);
......@@ -297,23 +300,23 @@ function node_authlink_token_info() {
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