Skip to content
Snippets Groups Projects
Commit 694b2130 authored by David López's avatar David López
Browse files

We don't create authlinks on node save, it should take a manual action....

We don't create authlinks on node save, it should take a manual action. Otherwise we delete them on node delete.
parent 9ff484ff
No related branches found
No related tags found
No related merge requests found
...@@ -143,7 +143,7 @@ function node_authlink_batch_generate(&$form, FormStateInterface &$form_state) { ...@@ -143,7 +143,7 @@ function node_authlink_batch_generate(&$form, FormStateInterface &$form_state) {
// Create keys // Create keys
foreach ($nids as $nid) { foreach ($nids as $nid) {
node_authlink_node_insert($nid); node_authlink_create($nid);
} }
drupal_set_message(t('%num authkeys has been generated.', ['%num' => count($nids)])); drupal_set_message(t('%num authkeys has been generated.', ['%num' => count($nids)]));
...@@ -164,10 +164,9 @@ function node_authlink_batch_delete(&$form, FormStateInterface &$form_state) { ...@@ -164,10 +164,9 @@ function node_authlink_batch_delete(&$form, FormStateInterface &$form_state) {
->isNotNull('authkey'); ->isNotNull('authkey');
$nids = $query->execute()->fetchCol(); $nids = $query->execute()->fetchCol();
// Delete keys foreach ($nids as $nid) {
$count = db_delete('node_authlink_nodes') node_authlink_delete($nid);
->condition('nid', $nids, 'IN') }
->execute();
drupal_set_message(t('%num authkeys has been deleted.', ['%num' => $count])); drupal_set_message(t('%num authkeys has been deleted.', ['%num' => $count]));
} }
...@@ -277,27 +276,21 @@ function node_authlink_node_access(NodeInterface $node, $op, AccountInterface $a ...@@ -277,27 +276,21 @@ function node_authlink_node_access(NodeInterface $node, $op, AccountInterface $a
} }
/** /**
* Implementation of hook_node_presave(). * Implements hook_ENTITY_TYPE_delete().
*
* Pre-generate auth key for the new node (e.g. for use in Rules).
*/ */
function node_authlink_node_presave(Node $node) { function node_authlink_node_delete(Drupal\Core\Entity\EntityInterface $entity) {
// Ignore if node type is disabled
$config = \Drupal::config('node_authlink.settings'); $config = \Drupal::config('node_authlink.settings');
if (!$config->get('enable.' . $node->bundle())) { $enable = $config->get('enable');
return; // Ignore if node type is disabled
if (isset($enable[$entity->bundle()]) && $enable[$entity->bundle()]) {
node_authlink_delete($entity->id());
} }
// Generate key
$node->authkey = hash('sha256', Crypt::randomBytes(64));
} }
/** /**
* Implementation of hook_node_insert().
*
* Generate and save auth key for the new node. * Generate and save auth key for the new node.
*/ */
function node_authlink_node_insert($node) { function node_authlink_create($node) {
// Allow key generate without load node object // Allow key generate without load node object
if (is_numeric($node)) { if (is_numeric($node)) {
$nid = $node; $nid = $node;
...@@ -325,6 +318,25 @@ function node_authlink_node_insert($node) { ...@@ -325,6 +318,25 @@ function node_authlink_node_insert($node) {
->execute(); ->execute();
} }
/**
* Deletes the node_authlink.
*
* @param $node
*/
function node_authlink_delete($node) {
if (is_numeric($node)) {
$nid = $node;
}
else {
$nid = $node->id();
}
// Delete keys
$count = db_delete('node_authlink_nodes')
->condition('nid', $nid)
->execute();
}
/** /**
* Implementation of hook_cron(). * Implementation of hook_cron().
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment