[1;33mdiff --git a/view_unpublished.install b/view_unpublished.install[m
[1;33mindex 90331a0..7324da0 100644[m
[1;33m--- a/view_unpublished.install[m
[1;33m+++ b/view_unpublished.install[m
[1;35m@@ -16,4 +16,12 @@[m [mfunction view_unpublished_enable() {[m
  */[m
 function view_unpublished_disable() {[m
   node_access_needs_rebuild(TRUE);[m
[1;31m-}[m
\ No newline at end of file[m
[1;32m+[m[1;32m}[m
[1;32m+[m
[1;32m+[m[1;32m/**[m
[1;32m+[m[1;32m * Rebuild the node access table and flush caches.[m
[1;32m+[m[1;32m */[m
[1;32m+[m[1;32mfunction view_unpublished_update_7001() {[m
[1;32m+[m[1;32m  node_access_needs_rebuild(TRUE);[m
[1;32m+[m[1;32m  drupal_flush_all_caches();[m
[1;32m+[m[1;32m}[m
[1;33mdiff --git a/view_unpublished.module b/view_unpublished.module[m
[1;33mindex fafc85e..7fdbd07 100644[m
[1;33m--- a/view_unpublished.module[m
[1;33m+++ b/view_unpublished.module[m
[1;35m@@ -6,9 +6,9 @@[m
  */[m
 [m
 /**[m
[1;31m- * Implements hook_permission().[m
[1;32m+[m[1;32m * Helper function to retrieve list of permissions.[m
  */[m
[1;31m-function view_unpublished_permission() {[m
[1;32m+[m[1;32mfunction view_unpublished_get_permissions_list() {[m
   $perms = array([m
     'view any unpublished content' => array([m
       'title' => t('View any unpublished content'),[m
[1;35m@@ -26,51 +26,71 @@[m [mfunction view_unpublished_permission() {[m
 }[m
 [m
 /**[m
[1;31m- * Implements hook_node_access_records().[m
[1;32m+[m[1;32m * Implements hook_permission().[m
  */[m
[1;31m-function view_unpublished_node_access_records($node) {[m
[1;31m-  // We only care about the node if is unpublished. If not, it is[m
[1;31m-  // treated just like any other node and we completely ignore it.[m
[1;31m-  if ($node->status == 0) {[m
[1;31m-    $grants = array();[m
[1;31m-    // Unpublished nodes should be viewable to all editors.[m
[1;31m-    $grants[] = array([m
[1;31m-      'realm' => 'view_unpublished_content',[m
[1;31m-      'gid' => 1,[m
[1;31m-      'grant_view' => 1,[m
[1;31m-      'grant_update' => 0,[m
[1;31m-      'grant_delete' => 0,[m
[1;31m-      'priority' => 0,[m
[1;31m-    );[m
[1;31m-    $grants[] = array([m
[1;31m-      'realm' => 'view_unpublished_' . $node->type . '_content',[m
[1;31m-      'gid' => 1,[m
[1;31m-      'grant_view' => 1,[m
[1;31m-      'grant_update' => 0,[m
[1;31m-      'grant_delete' => 0,[m
[1;31m-      'priority' => 0,[m
[1;31m-    );[m
[1;31m-    return $grants;[m
[1;32m+[m[1;32mfunction view_unpublished_permission() {[m
[1;32m+[m[1;32m  return view_unpublished_get_permissions_list();[m
[1;32m+[m[1;32m}[m
[1;32m+[m
[1;32m+[m[1;32m/**[m
[1;32m+[m[1;32m * Implements hook_og_permission().[m
[1;32m+[m[1;32m */[m
[1;32m+[m[1;32mfunction view_unpublished_og_permission() {[m
[1;32m+[m[1;32m  if (module_exists('og_context')) {[m
[1;32m+[m[1;32m    return view_unpublished_get_permissions_list();[m
   }[m
 }[m
 [m
 /**[m
[1;31m- * Implements hook_node_grants().[m
[1;32m+[m[1;32m * Implements hook_node_access().[m
  */[m
[1;31m-function view_unpublished_node_grants($account, $op) {[m
[1;31m-  $grants = array();[m
[1;32m+[m[1;32mfunction view_unpublished_node_access($node, $op, $account) {[m
   if ($op == 'view') {[m
[1;32m+[m[1;32m    // Set a good default response[m
[1;32m+[m[1;32m    $access = NODE_ACCESS_IGNORE;[m
[1;32m+[m
[1;32m+[m[1;32m    // Check to see if we're going to check for OG permissions as well.[m
[1;32m+[m[1;32m    $check_og = FALSE;[m
[1;32m+[m[1;32m    if (module_exists('og_context')) {[m
[1;32m+[m[1;32m      // When checking content within an access hook we have to use this method[m
[1;32m+[m[1;32m      // to get the group context. Otherwise, we will enter an infinite loop[m
[1;32m+[m[1;32m      // because og_context() does a menu_get_item() which in turns calls the[m
[1;32m+[m[1;32m      // access callbacks. http://drupal.org/node/1781218[m
[1;32m+[m[1;32m      $menu_item = array('path' => $_GET['q']);[m
[1;32m+[m[1;32m      if ($og = og_context_determine_context($menu_item)) {[m
[1;32m+[m[1;32m        $check_og = TRUE;[m
[1;32m+[m[1;32m        $gid = $og->gid;[m
[1;32m+[m[1;32m      }[m
[1;32m+[m[1;32m    }[m
[1;32m+[m
[1;32m+[m[1;32m    // If a user has the appropriate permission, then allow. If the user doesn't[m
[1;32m+[m[1;32m    // have the appropriate permission, but we're checking OG permissions and[m
[1;32m+[m[1;32m    // we're inside an organic group, use the group permission instead.[m
     if (user_access('view any unpublished content', $account)) {[m
[1;31m-      $grants['view_unpublished_content'] = array(1);[m
[1;31m-      return $grants;[m
[1;32m+[m[1;32m      $access = NODE_ACCESS_ALLOW;[m
     }[m
[1;31m-    foreach(node_permissions_get_configured_types() as $type) {[m
[1;31m-      if (user_access("view any unpublished $type content", $account)) {[m
[1;31m-        $grants['view_unpublished_' . $type . '_content'] = array(1);[m
[1;31m-      }[m
[1;32m+[m[1;32m    if ($check_og && isset($gid) && og_user_access($gid, 'view any unpublished content', $account)) {[m
[1;32m+[m[1;32m      $access = NODE_ACCESS_ALLOW;[m
[1;32m+[m[1;32m    }[m
[1;32m+[m
[1;32m+[m[1;32m    // If it's allowed at this point, then we can bail out - we know that the user[m
[1;32m+[m[1;32m    // will be allowed.[m
[1;32m+[m[1;32m    if ($access == NODE_ACCESS_ALLOW) {[m
[1;32m+[m[1;32m      return $access;[m
[1;32m+[m[1;32m    }[m
[1;32m+[m
[1;32m+[m[1;32m    // If a user has the appropriate permission, then allow. If the user doesn't[m
[1;32m+[m[1;32m    // have the appropriate permission, but we're checking OG permissions and[m
[1;32m+[m[1;32m    // we're inside an organic group, use the group permission instead.[m
[1;32m+[m[1;32m    if (user_access('view any unpublished ' . $node->type . ' content', $account)) {[m
[1;32m+[m[1;32m      $access = NODE_ACCESS_ALLOW;[m
     }[m
[1;32m+[m[1;32m    if ($check_og && isset($gid) && og_user_access($gid, 'view any unpublished ' . $node->type . ' content', $account)) {[m
[1;32m+[m[1;32m      $access = NODE_ACCESS_ALLOW;[m
[1;32m+[m[1;32m    }[m
[1;32m+[m
[1;32m+[m[1;32m    return $access;[m
   }[m
[1;31m-  return $grants;[m
 }[m
 [m
 /**[m
