From 6285812629df65184f4159197cfab23a2e2ab332 Sun, 25 Mar 2012 13:31:50 -0300
From: Javier Castro <javier.alejandro.castro@gmail.com>
Date: Sun, 25 Mar 2012 13:31:16 -0300
Subject: [PATCH] Default permissions are denied to all users/roles when there are no grants for a book

diff --git a/book_access.module b/book_access.module
old mode 100644
new mode 100755
index 6f81d10..2988987
--- a/book_access.module
+++ b/book_access.module
@@ -823,7 +823,7 @@
  * If we have a book child page, we return the access settings of the top level
  * parent book page node.
  */
- function book_access_node_access_records($node) {
+function book_access_node_access_records($node) {
   $grants = array();
 
   if (!empty($node->book['bid'])) {
@@ -834,6 +834,26 @@
 }
 
 /**
+ * Implements hook_node_access_records_alter().
+ * 
+ * If grants is empty for a given book, it means the permissions are
+ * not initialized. Se we detect this and rebuild the permissions
+ */
+function book_access_node_access_records_alter(&$grants, $node) {
+  if (!empty($node->book['bid']) && count($grants) == 0) {
+    $bid = $node->book['bid'];
+    $defaults = array('grant_view' => 0, 'grant_update' => 0, 'grant_delete' => 0, 'grant_admin_access' => 0, 'grant_add_child' => 0, 'grant_edit_outline' => 0);
+    $rids = array_keys(user_roles());
+    
+    BookAccess::setAuthorGrants($bid, $node->uid, $defaults);
+    BookAccess::setRoleGrants($bid, $rids, $defaults);
+
+    /* Now yeah, get the real permissions */
+    $grants = BookAccess::getGrantRecords($bid);
+  }
+}
+
+/**
  * Implements hook_permission().
  */
 function book_access_permission() {
