? view_unpublished.install ? view_unpublished_view_own_node_access.patch Index: view_unpublished.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/view_unpublished/view_unpublished.module,v retrieving revision 1.2.4.1 diff -u -p -r1.2.4.1 view_unpublished.module --- view_unpublished.module 30 Aug 2009 19:48:24 -0000 1.2.4.1 +++ view_unpublished.module 27 Jan 2010 09:42:10 -0000 @@ -14,7 +14,7 @@ * a new permission for each content type. */ function view_unpublished_perm() { - $perms = array('view all unpublished content'); + $perms = array('view all unpublished content', 'view own unpublished content'); foreach (node_get_types() as $type => $name) { $perms[] = 'view unpublished ' . $type . ' content'; @@ -28,16 +28,18 @@ function view_unpublished_perm() { * * Modifies the path node/nid to use our access callback. */ -function view_unpublished_menu_alter(&$items) { +function view_unpublished_menu_alter(&$items) { + $old_access_callback = $items['node/%node']['access callback']; + $items['node/%node']['access callback'] = '_view_unpublished_node_access'; - $items['node/%node']['access arguments'] = array(1); + $items['node/%node']['access arguments'] = array(1, $old_access_callback); } /** * Returns true if the user has 'view all unpublished content' or if * they have the permission corresponding to the node's content type. */ -function _view_unpublished_node_access($node) { +function _view_unpublished_node_access($node, $old_access_callback = 'node_access') { // Only check permissions on nodes that are unpublished. if ($node->status == 0) { if (user_access('view all unpublished content')) { @@ -47,8 +49,74 @@ function _view_unpublished_node_access($ if (user_access('view unpublished ' . $node->type . ' content')) { return TRUE; } + + if(user_access('view own unpublished content')) { + global $user; + if($node->uid == $user->uid) + return TRUE; + } } // If none of the above conditions were satisfied, then use node_access like normal. - return node_access('view', $node); + return $old_access_callback('view', $node); +} + +function view_unpublished_node_grants($account, $op) { + if (user_access('view all unpublished content')) { + $grants['view_unpublished_all_unpublished'] = array(0); + } + //If user can see all unpublished, we don't need to grant it specific node type or own unpublished in addition + else { + if (is_null($account)) { + global $user; + $account = $user; + } + + if(user_access('view own unpublished content', $account)) { + $grants['view_unpublished_own_unpublished'] = array($account->uid); + } + foreach (node_get_types() as $type => $name) { + if(user_access('view unpublished ' . $type . ' content', $account)) + $grants['view_unpublished_' . $type . '_content'] = array(0); + } + } + + $grants['view_unpublished_published'] = array(0); + + return $grants; +} + +function view_unpublished_node_access_records($node) { + $grants = array(); + + $base = array( + 'grant_view' => TRUE, + 'grant_update' => FALSE, + 'grant_delete' => FALSE, + 'priority' => 0, + ); + + //This view permission is for viewing published content + //(since this module provides grants, the gid=0/realm=all is not provided anymore by node) + //We give this realm to all users + if($node->status == 1) + $grants[] = $base + array( + 'realm' => 'view_unpublished_published', + 'gid' => 0, + ); + else { + $grants[] = $base + array( + 'realm' => 'view_unpublished_own_unpublished', + 'gid' => $node->uid, + ); + $grants[] = $base + array( + 'realm' => 'view_unpublished_all_unpublished', + 'gid' => 0, + ); + $grants[] = $base + array( + 'realm' => 'view_unpublished_' . $node->type . '_content', + 'gid' => 0, + ); + } + return $grants; }