Last active
September 29, 2015 15:02
-
-
Save heyrocker/5130173f4f2bc0ae83f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/access_unpublished.module b/access_unpublished.module | |
index f2b7db5..cb42c03 100644 | |
--- a/access_unpublished.module | |
+++ b/access_unpublished.module | |
@@ -34,6 +34,12 @@ function access_unpublished_permission() { | |
'title' => t('View unpublished contents'), | |
'description' => t('Allow users to view unpublished contents with unique key in URL path'), | |
); | |
+ if (module_exists('workbench_moderation')) { | |
+ $permissions['access unpublished view workbench moderation drafts'] = array( | |
+ 'title' => t('View moderated drafts'), | |
+ 'description' => t('Allow users to view workbench moderation drafts with unique key in URL path'), | |
+ ); | |
+ } | |
$permissions['access unpublished view hashkey'] = array( | |
'title' => t('View hash key link to unpublished contents'), | |
'description' => t('Allow users to view link with hash key for view unpublished contents'), | |
@@ -49,37 +55,25 @@ function access_unpublished_permission() { | |
* Implements hook_node_view(). | |
*/ | |
function access_unpublished_node_view($node) { | |
- // Test publish and view node status. | |
- // Only unpublished nodes in full page view are affected. | |
- if ($node->status == NODE_NOT_PUBLISHED && node_is_page($node)) { | |
- // Test user permission. | |
- if (user_access('access unpublished view hashkey')) { | |
- // Construct URL link. | |
- $au_url_key = variable_get('access_unpublished_url_key', 'hash'); | |
- $au_link = l(t("hashed link"), 'node/' . $node->nid, | |
- array( | |
- 'query' => array( | |
- $au_url_key => access_unpublished_get_hash_from_nodeid($node->nid), | |
- ), | |
- 'attributes' => array( | |
- // We need a extra class for link with class="active" from l() | |
- 'class' => array('access_unpublished_link'), | |
- ), | |
- ) | |
- ); | |
- $au_link_text = t("You can use !link to view this unpublished node.", | |
- array('!link' => $au_link)); | |
- if (variable_get('access_unpublished_display_link_in_drupal_message', 1)) { | |
- // Show link in Drupal message. | |
- drupal_set_message($au_link_text, 'status', FALSE); | |
+ if (user_access('access unpublished view hashkey') && node_is_page($node)) { | |
+ // Only unpublished nodes in full page view are affected. | |
+ if ($node->status == NODE_NOT_PUBLISHED) { | |
+ access_unpublished_show_hashed_link('node/' . $node->nid, $node); | |
+ } | |
+ else if (module_exists('workbench_moderation')) { | |
+ // Check for Workbench moderation processes and revisions | |
+ $wb_unpublished = FALSE; | |
+ $wb_revision = FALSE; | |
+ | |
+ if (!isset($node->workbench_moderation['published'])) { | |
+ $wb_unpublished = TRUE; | |
} | |
- if (variable_get('access_unpublished_display_link_in_node_content', 1)) { | |
- // Show link in extra renderable entry in node contents. | |
- $node->content['access_unpublished_link'] = array( | |
- '#access_unpublished_link' => $au_link_text, | |
- '#weight' => 100, | |
- '#theme' => 'access_unpublished_link', | |
- ); | |
+ if ($node->workbench_moderation['current']->current == 1 && $node->workbench_moderation['current']->published == '0') { | |
+ $wb_revision = TRUE; | |
+ } | |
+ | |
+ if ($wb_revision && !$wb_unpublished && strpos(current_path(), 'draft') !== FALSE) { | |
+ access_unpublished_show_hashed_link('node/' . $node->nid . '/draft', $node); | |
} | |
} | |
} | |
@@ -93,28 +87,17 @@ function access_unpublished_node_view($node) { | |
* or the node_access table to control access. | |
*/ | |
function access_unpublished_node_access($node, $op) { | |
- if ($op == 'view' && isset($node->nid) | |
- && $node->status == NODE_NOT_PUBLISHED) { | |
+ if ($op == 'view' && isset($node->nid) && $node->status == NODE_NOT_PUBLISHED) { | |
// Check user permission. | |
if (user_access('access unpublished view unpublished node')) { | |
- $au_url_key = variable_get('access_unpublished_url_key', 'hash'); | |
- // Check hash key in url. | |
- $url_query = drupal_get_query_parameters(); | |
- if (isset($url_query[$au_url_key])) { | |
- $url_hash = $url_query[$au_url_key]; | |
- // Generate reference hash. | |
- $hash = access_unpublished_get_hash_from_nodeid($node->nid); | |
- // Check hash match and return access state. | |
- if ($url_hash == $hash) { | |
- return NODE_ACCESS_ALLOW; | |
- } | |
+ if (access_unpublished_check_hash($node->nid)) { | |
+ return NODE_ACCESS_ALLOW; | |
} | |
} | |
} | |
return NODE_ACCESS_IGNORE; | |
} | |
- | |
/** | |
* Generate hash from node ID. | |
* | |
@@ -128,7 +111,6 @@ function access_unpublished_get_hash_from_nodeid($nid) { | |
return drupal_hash_base64(drupal_get_private_key() . $nid); | |
} | |
- | |
/** | |
* Implements hook_theme(). | |
*/ | |
@@ -140,3 +122,97 @@ function access_unpublished_theme() { | |
), | |
); | |
} | |
+ | |
+/** | |
+ * Implements hook_workbench_moderation_access_alter(). | |
+ * | |
+ * Return $access = TRUE if a node has an active revision and url hash matches reference hash. | |
+ */ | |
+function access_unpublished_workbench_moderation_access_alter(&$access, $op, $node) { | |
+ // Check user permission | |
+ if (user_access('access unpublished view workbench moderation drafts')) { | |
+ // Check that we are viewing a draft | |
+ if ($node->workbench_moderation['current']->current == 1 && $node->workbench_moderation['current']->published == '0') { | |
+ if (access_unpublished_check_hash($node->nid)) { | |
+ $access = TRUE; | |
+ } | |
+ } | |
+ } | |
+} | |
+ | |
+/** | |
+ * Implements menu_local_tasks_alter(). | |
+ */ | |
+function access_unpublished_menu_local_tasks_alter(&$data, $router_item, $root_path) { | |
+ // Do we need to bother doing anything? | |
+ if (empty($data['tabs'][0]['output'])) { | |
+ return; | |
+ } | |
+ | |
+ if(user_is_anonymous()) { | |
+ unset($data['tabs'][0]['output']); | |
+ } | |
+} | |
+ | |
+/** | |
+ * Check whether a hash is present in the current URL, and if so, verify | |
+ * that the hash is correct. | |
+ * | |
+ * @param int | |
+ * The node id to check. | |
+ * | |
+ * @return boolean | |
+ * TRUE if the hash is present and correct, otherwise FALSE. | |
+ */ | |
+function access_unpublished_check_hash($nid) { | |
+ $au_url_key = variable_get('access_unpublished_url_key', 'hash'); | |
+ | |
+ // Check hash key in url. | |
+ $url_query = drupal_get_query_parameters(); | |
+ if (isset($url_query[$au_url_key])) { | |
+ $url_hash = $url_query[$au_url_key]; | |
+ | |
+ // Generate reference hash. | |
+ $hash = access_unpublished_get_hash_from_nodeid($nid); | |
+ | |
+ // Check hash match and return access state. | |
+ if ($url_hash == $hash) { | |
+ return TRUE; | |
+ } | |
+ } | |
+} | |
+ | |
+/** | |
+ * Display the hashed link to the end user. | |
+ * | |
+ * @param string | |
+ * The original URL | |
+ * @param $node | |
+ * The node being displayed, passed by reference | |
+ */ | |
+function access_unpublished_show_hashed_link($url, &$node) { | |
+ // Construct URL link. | |
+ $au_url_key = variable_get('access_unpublished_url_key', 'hash'); | |
+ $au_link = l(t("hashed link"), $url, | |
+ array( | |
+ 'query' => array($au_url_key => access_unpublished_get_hash_from_nodeid($node->nid)), | |
+ 'attributes' => array('class' => array('access_unpublished_link')), | |
+ ) | |
+ ); | |
+ $au_link_text = t("You can use !link to view this unpublished content.", array('!link' => $au_link)); | |
+ | |
+ if (variable_get('access_unpublished_display_link_in_drupal_message', 1)) { | |
+ // Show link in Drupal message. | |
+ drupal_set_message($au_link_text, 'status', FALSE); | |
+ } | |
+ | |
+ if (variable_get('access_unpublished_display_link_in_node_content', 1)) { | |
+ // Show link in extra renderable entry in node contents. | |
+ $node->content['access_unpublished_link'] = array( | |
+ '#access_unpublished_link' => $au_link_text, | |
+ '#weight' => 100, | |
+ '#theme' => 'access_unpublished_link', | |
+ ); | |
+ } | |
+ | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment