Index: /home/damien/Documents/eclipse_workspace/drupal_basic_47/modules/og/og.module
===================================================================
--- /home/damien/Documents/eclipse_workspace/drupal_basic_47/modules/og/og.module	(revision 274)
+++ /home/damien/Documents/eclipse_workspace/drupal_basic_47/modules/og/og.module	(working copy)
@@ -1187,6 +1187,11 @@
     }
   }
   
+  // Group access control - should group be private to subscribers only?
+  // If this box is checked then the group's public node_access rows will not be inserted
+  // in og_save_permissions. 
+  $form['og_group_private'] =  array('#type' => 'checkbox', '#title' => t('Make group private'),  '#default_value' => $node->og_group_private ? 1 : 0 , '#description' => t('Should this group be accessible by URL for non-subscribers?  Setting the group private will also cause new group content to be private by default.'));
+
   $form['og_theme'] = system_theme_select_form(t('Selecting a different theme will change the look and feel of the group.'), $edit['theme'] ? $edit['theme'] : $node->og_theme, 2);;
   return $form;
 }
@@ -1199,6 +1204,14 @@
   return $node;
 }
 
+// return og_private_group property for a given group nodeID
+// returns 1 if group is private, 0 if not.
+function og_node_load_private_group($groupNodeID) {
+  $sql = "SELECT grant_view FROM {node_access} WHERE nid = %d AND gid=0 AND realm='og_all'";
+  $gv = db_result(db_query($sql, $groupNodeID));
+  return $gv ? 0 : 1;
+}
+
 // returns all the group affiliations for a given node.
 function og_get_node_groups($node) {
   $groups = array();
@@ -1444,6 +1457,14 @@
       $vis = OG_VISIBLE_BOTH;
     }
     
+    // override visibility if the group passed on the query string is private!
+    if ($gids) {
+    	$gid = $gids[0]; // just take the first gid in the list, it's usually the only one.
+    	if (og_node_load_private_group($gid)) { 
+    		$vis = OG_VISIBLE_GROUPONLY;
+    	}
+    }
+    
     switch ($vis) {
       case OG_VISIBLE_BOTH:
         $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
@@ -1452,7 +1473,8 @@
         $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 0);
         break;
 
-      //user decides how public the post is.
+      //user decides how public the post is, if the group isn't set private.  
+      //If the group IS set private, then the Public checkbox is not shown.
       case OG_VISIBLE_CHOOSE_PUBLIC:
         $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 1, '#description' => t('Show this post to everyone, or only to subscribers of the groups checked below. Only uncheck this box if truly needed.'));
         break;
@@ -1905,8 +1927,17 @@
   else {
     // all groups are publically accessible, although their posts may not be
     // if you really want an inaccessible group homepage, you should go to node_access table and delete the record which is inserted here.
-    $sql = "INSERT INTO {node_access} (nid, gid, realm, grant_view) VALUES (%d, 0, 'og_all', 1)";
-    db_query($sql, $node->nid);
+   
+   	// Use the group's og_group_private property to determine if it's public or not.
+    if ($node->og_group_private) {// the group is not (or no longer) marked public, remove any existing public grant_view row.
+    	$sql = "DELETE FROM {node_access} WHERE nid = %d AND gid = 0 AND realm = 'og_all' AND grant_view = 1;";
+		db_query($sql, $node->nid);
+    } else {  	  	
+    	// if not marked private or this is an old group row that doesn't have og_group_private, default
+    	// to the old behaviour of inserting the og_all grant for everybody.
+    	$sql = "INSERT INTO {node_access} (nid, gid, realm, grant_view) VALUES (%d, 0, 'og_all', 1)";
+    	db_query($sql, $node->nid);	
+    }
 
     // we INSERT a broad grant here but only og_node_grants() only issues it selectively. Enabes all group admins to edit the og node
     $sql = "INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, 'og_subscriber', 1, 1, 0)";
