Commit 9213ee7a authored by akalam's avatar akalam

node_authlink_batch_delete, node_authlink_batch_generate,...

node_authlink_batch_delete, node_authlink_batch_generate, node_authlink_form_node_type_form_alter_submit, node_authlink_form_node_type_form_alter ported
parent baa2fdfe
<?php
// $Id$
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
/**
* Alter of node_type_form.
*/
function node_authlink_form_node_type_form_alter(&$form, &$form_state) {
$form['node_authlink'] = array(
function node_authlink_form_node_type_form_alter(&$form, FormStateInterface &$form_state) {
/** @var \Drupal\node\Entity\NodeType $node_type */
$node_type = $form_state->getFormObject();
$type = $node_type->id();
$config = \Drupal::config('node_authlink.settings');
$form['node_authlink'] = [
'#type' => 'fieldset',
'#title' => t('Node authorize link'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
];
$form['node_authlink']['node_authlink_enable'] = array(
$form['node_authlink']['node_authlink_enable'] = [
'#type' => 'checkbox',
'#title' => t('Enable'),
'#default_value' => variable_get('node_authlink_enable_' . $form['#node_type']->type, FALSE),
'#default_value' => $config->get('enable.' . $type),
'#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(
$form['node_authlink']['node_authlink_grants'] = [
'#type' => 'checkboxes',
'#title' => t('Grants to give'),
'#default_value' => variable_get('node_authlink_grants_' . $form['#node_type']->type, array()),
'#options' => array(
'#default_value' => $config->get('grants.' . $type),
'#options' => [
'view' => t('View'),
'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 = drupal_map_assoc([0, 86400, 604800, 2419200], 'format_interval');
$period[0] = '<' . t('disabled') . '>';
$form['node_authlink']['node_authlink_expire'] = array(
$form['node_authlink']['node_authlink_expire'] = [
'#type' => 'select',
'#title' => t('Regenerate authkeys after'),
'#default_value' => variable_get('node_authlink_expire_' . $form['#node_type']->type, 0),
'#default_value' => $config->get('expire.' . $type),
'#options' => $period,
'#description' => t('Keys older than selected time will be regenerated by cron run.'),
);
];
$form['node_authlink']['node_authlink_batch'] = array(
$form['node_authlink']['node_authlink_batch'] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Batch operations'),
'#description' => t('Affects all nodes in this node type.'),
);
$form['node_authlink']['node_authlink_batch']['generate'] = array(
];
$form['node_authlink']['node_authlink_batch']['generate'] = [
'#type' => 'submit',
'#value' => t('Generate authkeys'),
'#submit' => array('node_authlink_batch_generate'),
);
$form['node_authlink']['node_authlink_batch']['delete'] = array(
'#submit' => ['node_authlink_batch_generate'],
];
$form['node_authlink']['node_authlink_batch']['delete'] = [
'#type' => 'submit',
'#value' => t('Delete all authkeys'),
'#submit' => array('node_authlink_batch_delete'),
);
'#submit' => ['node_authlink_batch_delete'],
];
$form['#submit'][] = 'node_authlink_form_node_type_form_alter_submit';
}
......@@ -70,11 +79,15 @@ 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) {
function node_authlink_form_node_type_form_alter_submit(&$form, FormStateInterface &$form_state) {
// Disabled
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']);
if (!$form_state->getValue('node_authlink_enable')) {
$type = $form_state->getValue('type');
$config_factory = \Drupal::configFactory();
$config_factory->getEditable('node_authlink.settings.enable.' . $type)
->delete();
$config_factory->getEditable('node_authlink.settings.grants.' . $type)
->delete();
}
}
......@@ -85,27 +98,31 @@ function node_authlink_batch_generate(&$form, &$form_state) {
// 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'))
$query->fields('n', ['nid'])
->condition('type', $form_state['values']['type'])
->isNull('authkey');
$nids = $query->execute()->fetchCol();
// Create keys
foreach ($nids as $nid)
foreach ($nids as $nid) {
node_authlink_node_insert($nid);
}
drupal_set_message(t('%num authkeys has been generated.', array('%num' => count($nids))));
drupal_set_message(t('%num authkeys has been generated.', ['%num' => count($nids)]));
}
/**
* Delete authkeys for all nodes in node type.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function node_authlink_batch_delete(&$form, &$form_state) {
function node_authlink_batch_delete(&$form, FormStateInterface &$form_state) {
// NIDs of nodes that are in this node type
$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'])
$query->fields('n', ['nid'])
->condition('type', $form_state->getValue('type'))
->isNotNull('authkey');
$nids = $query->execute()->fetchCol();
......@@ -114,7 +131,7 @@ function node_authlink_batch_delete(&$form, &$form_state) {
->condition('nid', $nids, 'IN')
->execute();
drupal_set_message(t('%num authkeys has been deleted.', array('%num' => $count)));
drupal_set_message(t('%num authkeys has been deleted.', ['%num' => $count]));
}
......@@ -126,8 +143,9 @@ function node_authlink_batch_delete(&$form, &$form_state) {
function node_authlink_node_load($nodes, $types) {
foreach ($nodes as $nid => $node) {
// TODO: check node type (performance)
if($authkey = node_authlink_load_authkey($nid))
if ($authkey = node_authlink_load_authkey($nid)) {
$nodes[$nid]->authkey = $authkey;
}
}
}
......@@ -135,22 +153,25 @@ function node_authlink_node_load($nodes, $types) {
* 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));
$result = db_query('SELECT authkey FROM {node_authlink_nodes} WHERE nid = :nid', [':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))
if (is_numeric($node)) {
$node = Node::load($node);
}
if(!isset($node->authkey))
if (!isset($node->authkey)) {
return FALSE;
}
switch ($op) {
case 'view':
......@@ -177,28 +198,30 @@ function node_authlink_get_url($node, $op = 'edit') {
/**
* Implements hook_node_access().
*/
function node_authlink_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
function node_authlink_node_access(NodeInterface $node, $op, AccountInterface $account) {
// Ignore if just creating node
if($op == 'create')
if ($op == 'create') {
return AccessResult::neutral();
}
$config = \Drupal::config('node_authlink.settings');
$config_enable = $config->get('enable');
// Ignore if node type is not enabled
if(!isset($config_enable[$node->bundle()]) || !$config_enable[$node->bundle()])
if (!isset($config_enable[$node->bundle()]) || !$config_enable[$node->bundle()]) {
return AccessResult::neutral();
}
// Check key if:
if(isset($_GET['authkey']) && // authkey param is set
if (isset($_GET['authkey']) && // authkey param is set
isset($node->authkey)) { // authkey in node is set
if($node->authkey == $_GET['authkey']) {
if ($node->authkey == $_GET['authkey']) {
// Start session
if(!isset($_SESSION))
/** @var \Drupal\Core\Session\SessionManager $session_manager */
if (!isset($_SESSION)) /** @var \Drupal\Core\Session\SessionManager $session_manager */ {
$session_manager = \Drupal::service('session_manager');
$session_manager->start();
}
$session_manager->start();
// Save allowed grants to session
$config_grants = $config->get('grants');
......@@ -207,9 +230,10 @@ function node_authlink_node_access(\Drupal\node\NodeInterface $node, $op, \Drupa
}
// Permit if checked
if(isset($_SESSION['node_authlink_nodes'][$node->id()]) &&
in_array($op, $_SESSION['node_authlink_nodes'][$node->id()]))
if (isset($_SESSION['node_authlink_nodes'][$node->id()]) &&
in_array($op, $_SESSION['node_authlink_nodes'][$node->id()])) {
return AccessResult::allowed();
}
}
/**
......@@ -219,8 +243,9 @@ function node_authlink_node_access(\Drupal\node\NodeInterface $node, $op, \Drupa
*/
function node_authlink_node_presave($node) {
// Ignore if node type is disabled
if(!variable_get('node_authlink_enable_' . $node->type, FALSE))
if (!variable_get('node_authlink_enable_' . $node->type, FALSE)) {
return;
}
// Generate key
$node->authkey = hash('sha256', drupal_random_bytes(64));
......@@ -233,14 +258,16 @@ function node_authlink_node_presave($node) {
*/
function node_authlink_node_insert($node) {
// Allow key generate without load node object
if(is_numeric($node))
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))
if (!variable_get('node_authlink_enable_' . $node->type, FALSE)) {
return;
}
}
// Generate key if not yet
......@@ -248,11 +275,11 @@ function node_authlink_node_insert($node) {
// Save to DB
db_insert('node_authlink_nodes')
->fields(array(
->fields([
'nid' => $nid,
'authkey' => $authkey,
'created' => time(),
))
])
->execute();
}
......@@ -264,13 +291,14 @@ function node_authlink_cron() {
foreach ($node_types as $type) {
$expire = variable_get('node_authlink_expire_' . $type->type, 0);
if(!$expire)
if (!$expire) {
continue;
}
// NIDs of expired keys
$query = db_select('node', 'n');
$query->leftJoin('node_authlink_nodes', 'a', 'n.nid = a.nid');
$query->fields('n', array('nid'))
$query->fields('n', ['nid'])
->condition('n.type', $type->type)
->condition('a.created', time() - $expire, '<');
$nids = $query->execute()->fetchCol();
......@@ -289,50 +317,55 @@ function node_authlink_cron() {
* Implementation of hook_token_info().
*/
function node_authlink_token_info() {
$node['authlink:authkey'] = array(
$node['authlink:authkey'] = [
'name' => t("Authorization key"),
'description' => t("Key generated by Node authorize link module."),
);
$node['authlink:view-url'] = array(
];
$node['authlink:view-url'] = [
'name' => t("View URL"),
'description' => t("URL with authorization key."),
);
$node['authlink:edit-url'] = array(
];
$node['authlink:edit-url'] = [
'name' => t("Edit URL"),
'description' => t("URL with authorization key."),
);
$node['authlink:delete-url'] = array(
];
$node['authlink:delete-url'] = [
'name' => t("Delete URL"),
'description' => t("URL with authorization key."),
);
];
return array(
'tokens' => array('node' => $node),
);
return [
'tokens' => ['node' => $node],
];
}
/**
* Implementation of hook_tokens().
*/
function node_authlink_tokens($type, $tokens, array $data = array(), array $options = array()) {
if($type != 'node')
function node_authlink_tokens($type, $tokens, array $data = [], array $options = []) {
if ($type != 'node') {
return;
}
//dpm(func_get_args());
$replacements = array();
$replacements = [];
if(isset($tokens['authlink:authkey']))
if (isset($tokens['authlink:authkey'])) {
$replacements['[node:authlink:authkey]'] = $data['node']->authkey;
}
if(isset($tokens['authlink:view-url']))
if (isset($tokens['authlink:view-url'])) {
$replacements['[node:authlink:view-url]'] = node_authlink_get_url($data['node'], 'view');
}
if(isset($tokens['authlink:edit-url']))
if (isset($tokens['authlink:edit-url'])) {
$replacements['[node:authlink:edit-url]'] = node_authlink_get_url($data['node'], 'edit');
}
if(isset($tokens['authlink:delete-url']))
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