From afa2e0844ff79b59d529343cecd5a61328e1c386 Mon Sep 17 00:00:00 2001
From: Jonathan Jordan <jojonaloha@1579186.no-reply.drupal.org>
Date: Tue, 17 Feb 2015 18:19:35 -0800
Subject: [PATCH] Issue #1784130 by cweagans, grndlvl: Add some basic
 integration with organic groups

---
 view_unpublished.module | 77 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/view_unpublished.module b/view_unpublished.module
index 6f2e399..b496f5f 100644
--- a/view_unpublished.module
+++ b/view_unpublished.module
@@ -26,6 +26,27 @@ function view_unpublished_permission() {
 }
 
 /**
+ * Implements hook_og_permission().
+ */
+function view_unpublished_og_permission() {
+  $perms = array(
+    'view any unpublished content' => array(
+      'title' => t('View any unpublished group content'),
+    ),
+  );
+  // Generate standard node permissions for all applicable node types.
+  $og_bundles = og_get_all_group_content_bundle();
+  if (isset($og_bundles['node'])) {
+    foreach ($og_bundles['node'] as $type => $label) {
+      $perms["view any unpublished $type content"] = array(
+        'title' => t('%type_name: View any unpublished group content', array('%type_name' => $label)),
+      );
+    }
+  }
+  return $perms;
+}
+
+/**
  * Implements hook_node_access_records().
  */
 function view_unpublished_node_access_records($node) {
@@ -58,6 +79,38 @@ function view_unpublished_node_access_records($node) {
       'grant_delete' => 0,
       'priority' => 0,
     );
+
+    // When the og module is enabled, add grants if the node is group content.
+    if (module_exists('og')) {
+      $og_info = system_get_info('module', 'og');
+      // OG 7.x-1.x is not supported.
+      if (version_compare($og_info['version'], '7.x-2.x', '>=')) {
+        $is_group_content = og_is_group_content_type('node', $node->type);
+        if ($is_group_content && $groups = og_get_entity_groups('node', $node)) {
+          foreach ($groups as $group_type => $gids) {
+            foreach ($gids as $gid) {
+              $grants[] = array(
+                'realm' => $group_type . ':view_unpublished_group_content',
+                'gid' => $gid,
+                'grant_view' => 1,
+                'grant_update' => 0,
+                'grant_delete' => 0,
+                'priority' => 0,
+              );
+              $grants[] = array(
+                'realm' => $group_type . ':view_unpublished_' . $node->type . '_group_content',
+                'gid' => $gid,
+                'grant_view' => 1,
+                'grant_update' => 0,
+                'grant_delete' => 0,
+                'priority' => 0,
+              );
+            }
+          }
+        }
+      }
+    }
+
     return $grants;
   }
 }
@@ -80,6 +133,30 @@ function view_unpublished_node_grants($account, $op) {
         $grants['view_unpublished_' . $type . '_content'] = array(1);
       }
     }
+
+    // When the og module is enabled, add grants if the user is in any groups.
+    if (module_exists('og')) {
+      $og_info = system_get_info('module', 'og');
+      // OG 7.x-1.x is not supported.
+      if (version_compare($og_info['version'], '7.x-2.x', '>=')) {
+        $groups = og_get_groups_by_user($account);
+        $og_bundles = og_get_all_group_content_bundle();
+        if (!empty($og_bundles['node']) && !empty($groups)) {
+          foreach ($groups as $group_type => $gids) {
+            foreach ($gids as $gid) {
+              if (og_user_access($group_type, $gid, 'view any unpublished content', $account)) {
+                $grants[$group_type . ':view_unpublished_group_content'][] = $gid;
+              }
+              foreach (array_keys($og_bundles['node']) as $type) {
+                if (og_user_access($group_type, $gid, "view any unpublished $type content", $account)) {
+                  $grants[$group_type . ':view_unpublished_' . $type . '_group_content'][] = $gid;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
   }
   return $grants;
 }
-- 
1.9.3 (Apple Git-50)

