From ac92551d28855412e2f354e160f234054ae85642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l=20Pittet?= Date: Sat, 17 Oct 2020 23:04:04 -0700 Subject: [PATCH] Coding standards cleanup --- node_authlink.install | 96 ++++++++++-------- node_authlink.module | 215 +++++++++++++++++++++++----------------- node_authlink.views.inc | 8 +- 3 files changed, 187 insertions(+), 132 deletions(-) diff --git a/node_authlink.install b/node_authlink.install index db5d046..6ee6d15 100644 --- a/node_authlink.install +++ b/node_authlink.install @@ -1,40 +1,69 @@ '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), + '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(). + * Implements 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(). + * Implements 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); + } +} + +/** + * Implements hook_update_N(). */ function node_authlink_update_7100(&$sandbox) { - // Migrate data + // 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\''); + + $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') @@ -45,44 +74,33 @@ function node_authlink_update_7100(&$sandbox) { )) ->execute(); } - + unset($keys); - - // Migrate settings + + // Migrate settings. $field = field_info_field('field_node_authlink_authkey'); - $instances = field_read_instances(array('field_id' => $field['id'], 'entity_type' => 'node')); - + $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 + + // Cleanup. field_delete_field('field_node_authlink_authkey'); } -function node_authlink_update_7101(&$sandbox) { - variable_del('node_authlink_types'); -} /** - * Inplementation of hook_uninstall(). + * Remove node_authlink_types variable. */ -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); - } +function node_authlink_update_7101(&$sandbox) { + variable_del('node_authlink_types'); } - - - - - diff --git a/node_authlink.module b/node_authlink.module index 5b763c0..01044a6 100644 --- a/node_authlink.module +++ b/node_authlink.module @@ -1,8 +1,12 @@ 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, 3 months, 6 months, 1 year - $period = drupal_map_assoc(array(0, 86400, 604800, 2419200, 7776000, 15552000, 31536000), 'format_interval'); + + // Time periods: none, 1 day, 1 week, 4 weeks, 3 months, 6 months, 1 year. + $period = drupal_map_assoc(array( + 0, + 86400, + 604800, + 2419200, + 7776000, + 15552000, + 31536000, + ), '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,57 +72,57 @@ 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'; } /** - * Submit for node_type_form. + * Callback: Submit for node_type_form. */ function node_authlink_form_node_type_form_alter_submit(&$form, &$form_state) { - // Disabled - if(!$form_state['values']['node_authlink_enable']) { + // 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']); } } /** - * Generate authkeys for all nodes in node type. + * Callback: 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 + + // Create keys. foreach ($nids as $nid) { node_authlink_node_insert($nid); if (module_exists('entitycache')) { cache_clear_all($nid, 'cache_entity_node'); } } - + drupal_set_message(t('%num authkeys has been generated.', array('%num' => count($nids)))); } /** - * Delete authkeys for all nodes in node type. + * Callback: Delete authkeys for all nodes in node type. */ function node_authlink_batch_delete(&$form, &$form_state) { - // NIDs of nodes that are in this node type + // 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']) ->isNotNull('authkey'); $nids = $query->execute()->fetchCol(); - - // Delete keys + + // Delete keys. $count = db_delete('node_authlink_nodes') ->condition('nid', $nids, 'IN') ->execute(); @@ -120,50 +132,65 @@ function node_authlink_batch_delete(&$form, &$form_state) { cache_clear_all($nid, 'cache_entity_node'); } } - + drupal_set_message(t('%num authkeys has been deleted.', array('%num' => $count))); } - /** - * Implementation of hook_node_load(). + * Implements hook_node_load(). * - * Appends authke to loaded node object. + * Appends authkey to loaded node object. */ 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; + } } } /** * Loads key from NID. + * + * @param string $nid + * The node id. + * + * @return string + * The authkey. */ 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. + * + * @param int|object $node + * Node object or NID. + * @param string $op + * Operation to do with node. view, edit (default) or delete. + * + * @return string + * The node operation's URL with authkey query appended. */ 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; - - if($op == 'view') + } + + if ($op == 'view') { $op = ''; - else + } + else { $op = '/' . $op; - + } + return url("node/$node->nid$op", array( 'absolute' => TRUE, 'query' => array('authkey' => $node->authkey), @@ -171,77 +198,83 @@ 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) { - // Ignore if just creating node - if($op == 'create') + // Ignore if just creating node. + if ($op == 'create') { return NODE_ACCESS_IGNORE; - - // Ignore if node type is not enabled - if(!variable_get('node_authlink_enable_' . $node->type, FALSE)) + } + + // Ignore if node type is not enabled. + if (!variable_get('node_authlink_enable_' . $node->type, FALSE)) { return NODE_ACCESS_IGNORE; - + } + // Check key if: - if(isset($_GET['authkey']) && // authkey param is set - isset($node->authkey)) { // authkey in node is set - - if($node->authkey == $_GET['authkey']) { - // Start session - if(!isset($_SESSION)) + // authkey param is set and in node is set. + if (isset($_GET['authkey']) && isset($node->authkey)) { + + if ($node->authkey == $_GET['authkey']) { + // Start session. + if (!isset($_SESSION)) { drupal_session_initialize(); - - // Save allowed grants to session + } + + // Save allowed grants to session. $_SESSION['node_authlink_nodes'][$node->nid] = variable_get('node_authlink_grants_' . $node->type, array()); } } - - // Permit if checked - if(isset($_SESSION['node_authlink_nodes'][$node->nid]) && - in_array($op, $_SESSION['node_authlink_nodes'][$node->nid])) + + // Permit if checked. + if (isset($_SESSION['node_authlink_nodes'][$node->nid]) && in_array($op, $_SESSION['node_authlink_nodes'][$node->nid])) { return NODE_ACCESS_ALLOW; + } } /** - * Implementation of hook_node_presave(). + * Implements hook_node_presave(). * * Pre-generate auth key for the new node (e.g. for use in Rules). */ function node_authlink_node_presave($node) { - // Ignore if node type is disabled - if(!variable_get('node_authlink_enable_' . $node->type, FALSE)) + // Ignore if node type is disabled. + if (!variable_get('node_authlink_enable_' . $node->type, FALSE)) { return; + } - // Generate key + // Generate key. if (!isset($node->authkey)) { $node->authkey = hash('sha256', drupal_random_bytes(64)); } } /** - * Implementation of hook_node_insert(). + * Implements hook_node_insert(). * * Generate and save auth key for the new node. */ function node_authlink_node_insert($node) { - // Allow key generate without load node object - if(is_numeric($node)) + // Allow key generate without load node object. + 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)) + + // Ignore if node type is disabled. + if (!variable_get('node_authlink_enable_' . $node->type, FALSE)) { return; + } } - - // Generate key if not yet + + // Generate key if not yet. $authkey = isset($node->authkey) ? $node->authkey : hash('sha256', drupal_random_bytes(64)); - - // Save to DB + + // Save to DB. db_insert('node_authlink_nodes') ->fields(array( - 'nid' => $nid, + 'nid' => $nid, 'authkey' => $authkey, 'created' => time(), )) @@ -249,25 +282,26 @@ function node_authlink_node_insert($node) { } /** - * Implementation of hook_cron(). + * Implements hook_cron(). */ 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) + if (!$expire) { continue; - - // NIDs of expired keys + } + + // NIDs of expired keys. $query = db_select('node', 'n'); $query->leftJoin('node_authlink_nodes', 'a', 'n.nid = a.nid'); $query->fields('n', array('nid')) ->condition('n.type', $type->type) ->condition('a.created', time() - $expire, '<'); $nids = $query->execute()->fetchCol(); - - // Regenerate keys + + // Regenerate keys. foreach ($nids as $nid) { db_delete('node_authlink_nodes') ->condition('nid', $nid) @@ -281,23 +315,23 @@ function node_authlink_cron() { } /** - * Implementation of hook_token_info(). + * Implements hook_token_info(). */ 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."), ); @@ -307,7 +341,7 @@ function node_authlink_token_info() { } /** - * Implementation of hook_tokens(). + * Implements hook_tokens(). */ function node_authlink_tokens($type, $tokens, array $data = array(), array $options = array()) { $replacements = array(); @@ -320,12 +354,15 @@ function node_authlink_tokens($type, $tokens, array $data = array(), array $opti case 'authlink:authkey': $replacements[$original] = $node->authkey; break; + case 'authlink:view-url': $replacements[$original] = node_authlink_get_url($node, 'view'); break; + case 'authlink:edit-url': $replacements[$original] = node_authlink_get_url($node, 'edit'); break; + case 'authlink:delete-url': $replacements[$original] = node_authlink_get_url($node, 'delete'); break; diff --git a/node_authlink.views.inc b/node_authlink.views.inc index ff4483e..aa3c95d 100644 --- a/node_authlink.views.inc +++ b/node_authlink.views.inc @@ -12,11 +12,11 @@ */ function node_authlink_views_data() { - // Table properties + // Table properties. $data['node_authlink_nodes']['table']['group'] = t('Node Auth link'); $data['node_authlink_nodes']['table']['base'] = array( - 'field' => 'nid', // This is the identifier field for the view. + 'field' => 'nid', 'title' => t('Node auth link nodes table'), 'help' => t('Table for store authorization keys.'), 'weight' => -10, @@ -29,7 +29,7 @@ function node_authlink_views_data() { ), ); - // Table fields + // Table fields. $data['node_authlink_nodes']['nid'] = array( 'title' => t('Nid'), 'help' => t('The node ID.'), @@ -76,7 +76,7 @@ function node_authlink_views_data() { ), ); - // Node integration + // Node integration. $data['node']['node_authlink_nodes'] = array( 'title' => t('Nid'), 'help' => t('The node ID.'), -- GitLab