? og_access_module.patch
? og_node_access.inc_old
? og_node_access.patch
Index: og.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og/og.module,v
retrieving revision 1.298.2.161
diff -u -r1.298.2.161 og.module
--- og.module	5 Apr 2008 15:05:46 -0000	1.298.2.161
+++ og.module	8 Apr 2008 00:51:30 -0000
@@ -1762,53 +1762,6 @@
     $groups = array();
   }
 
-  // don't bother with visibility if access control is disabled. all is public.
-  if (variable_get('og_enabled', FALSE)) {
-    // get the visibility for normal users
-    $vis = variable_get('og_visibility', 0);
-
-    // override visibility for og admins and when author only has 1 group
-    if (user_access('administer organic groups') && $vis < 2) {
-      $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
-    }
-    elseif (!count($options)) {
-      // don't show checkbox if no memberships. must be public.
-      $vis = OG_VISIBLE_BOTH;
-    }
-
-    // If the post is to a private group, visibility must default to one of the private options.
-    // Try not to show checkbox if admin likes to reduce decisions for node authors.
-    $selected_groups = isset($form['#post']['og_groups']) ? array_filter($form['#post']['og_groups']) : $groups;
-    if (count($selected_groups)) {  
-      foreach ($selected_groups as $gid) {
-        $group_node = new stdClass();
-        $group_node->nid = $gid;
-        og_load_group($group_node);
-         if ($group_node->og_private) {
-           $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
-          break;
-        }
-      }
-    } 
-
-    switch ($vis) {
-      case OG_VISIBLE_BOTH:
-        $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
-        break;
-      case OG_VISIBLE_GROUPONLY:
-        $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 0);
-        break;
-
-      //user decides how public the post is.
-      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 members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
-        break;
-      case OG_VISIBLE_CHOOSE_PRIVATE:
-        $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 0, '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
-        break;
-    }
-  }
-
   // Emit the audience form element.
   if ($simple) {
     // 'simple' mode. read only.
@@ -2286,27 +2239,6 @@
   }
 }
 
-function og_node_grants($account, $op) {
-  if (variable_get('og_enabled', FALSE)) {
-    if ($op == 'view') {
-      $grants['og_public'][] = 0; // everyone can see a public node
-    }
-
-    // get memberships
-    if ($subscriptions = og_get_subscriptions($account->uid)) {
-      foreach ($subscriptions as $key => $val) {
-        if ($op == 'view') {
-          $grants['og_subscriber'][] = $key;
-        }
-        if (($op == 'update' || $op == 'delete') && $val['is_admin']) {
-          $grants['og_subscriber'][] = $key;
-        }
-      }
-    }
-    return $grants ? $grants : array();
-  }
-}
-
 function og_save_ancestry($node) {
   if (!og_is_omitted_type($node->type)) {
     $sql = "DELETE FROM {og_ancestry} WHERE nid = %d";
@@ -2321,40 +2253,6 @@
   }
 }
 
-function og_node_access_records($node) {
-  // don't write records if og access control is disabled or the node type is omitted or node is a group
-  if (og_is_omitted_type($node->type) || !variable_get('og_enabled', FALSE)) {
-    return;
-  }
-  
-  if (og_is_group_type($node->type)) {
-    // this grant allows group admins to manage stuff
-    $grants[] = array('realm' => 'og_subscriber', 'gid' => $node->nid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
-    // If the group is not marked private let everyone view the group homepage.
-    if (!$node->og_private) {
-        $grants[] = array (
-          'realm' => 'og_public',
-          'gid' => 0,
-          'grant_view' => 1,
-          'grant_update' => 0,
-          'grant_delete' => 0 );
-    }
-  }
-  elseif (is_array($node->og_groups)) {
-    // applies to non group nodes
-    if ($node->og_public) {
-      $grants[] = array('realm' => 'og_public', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
-    }
-    
-    foreach ($node->og_groups as $gid) {
-      // we write a broad grant here but og_node_grants() only issues it selectively.
-      $grants[] = array('realm' => 'og_subscriber', 'gid' => $gid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
-    }
-  }
-
-  return $grants;
-}
-
 function og_is_omitted_type($type) {
   return in_array($type, variable_get('og_omitted', array()));
 }
@@ -2721,38 +2619,11 @@
   return $form;
 } 
 
-function og_settings_submit($form_id, $form_values) {
-  if ($form_values['op'] == t('Enable')) {
-    variable_set('og_enabled', 1);
-    node_access_rebuild();
-    drupal_set_message(t('The node access table has been rebuilt.'));
-  }
-  elseif ($form_values['op'] == t('Disable')) {
-    variable_set('og_enabled', 0);
-    node_access_rebuild();
-    drupal_set_message(t('The node access table has been rebuilt.'));
-  }
-}
-
 function og_admin_settings() {
   $form['#submit']['og_settings_submit'] = array(); // custom submit handler
   $form['#submit']['system_settings_form_submit'] = array(); // form.inc never calls the $callback if a submit handler is defined
   drupal_set_title(t('Organic groups configuration'));
   drupal_add_js(drupal_get_path('module', 'og'). '/og.js'); // load the form javascript to handle private groups / node visibility conflicts in the og settings form.
- 
-  if (variable_get('og_enabled', 0)) {
-    $status = t('enabled');
-    $btn_text = t('Disable');
-    $description = t('Usually, you want access control enabled. Much functionality is unavailable without it.');
-  }
-  else {
-    $status = t('disabled');
-    $btn_text = t('Enable');
-    $description = t('Enable access control if you want to author any content that is restricted to group members. The button below will delete one record in your node_access table (if needed) and thus enable node permissions on your site. You may revert by clicking the same button again. If you switch back and forth, your node permissions are preserved.');
-  }
-  $form['og_settings']['og_module_status'] = array('#type' => 'fieldset', '#title' => t('Access control'), '#description' => $description, '#collapsible' => TRUE, '#collapsed' => TRUE);
-  $form['og_settings']['og_module_status']['module_action'] = array('#type' => 'submit', '#value' => $btn_text, '#prefix' => '<p>'. t('Organic groups access control is currently %status.', array('%status' => $status)). '</p>');
-
 
   $form['og_settings']['group_details'] = array('#type' => 'fieldset', '#title' => t('Group details'), '#collapsible' => TRUE, '#collapsed' => TRUE);
   // groups directory visibility
@@ -3047,7 +2918,7 @@
       );
     }
     
-    if (!variable_get('og_enabled', FALSE)) {
+    if (!module_exists('og_access')) {
       $requirements['og_access'] = array(
         'title' => $t('Organic groups access control'),
         'value' => $t('Organic groups access control is disabled. See the !settings', array('!settings' => l($t('settings page'), 'admin/og/og'))),
@@ -3081,16 +2952,3 @@
   }
   return FALSE;
 }
-
-/**
-* Implementation of hook_node_access_explain
-*/
-function og_node_access_explain($row) {
-  if ($row->realm == 'og_public') {
-    return t('All users may view this node.', array('@op' => $op));
-  }
-  elseif ($row->realm == 'og_subscriber') {
-    $node = node_load((int)$row->gid);
-    return t('Members of !group_n may view this node.', array('@op' => $op, '!group_n' => l($node->title, "node/$row->gid")));
-  }
-}
Index: og_access.info
===================================================================
RCS file: og_access.info
diff -N og_access.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ og_access.info	8 Apr 2008 00:51:30 -0000
@@ -0,0 +1,4 @@
+name = Organic groups access control
+description = "Enable access control to nodes that are private to a group."
+package = "Organic groups"
+dependencies = og
\ No newline at end of file
Index: og_access.install
===================================================================
RCS file: og_access.install
diff -N og_access.install
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ og_access.install	8 Apr 2008 00:51:30 -0000
@@ -0,0 +1,11 @@
+<?php
+// $Id$
+
+function og_access_install() {
+}
+
+function og_access_uninstall() {
+  variable_del('og_access_rebuild');
+  node_access_rebuild();
+  drupal_set_message(t('The node access table has been rebuilt.'));
+}
\ No newline at end of file
Index: og_access.module
===================================================================
RCS file: og_access.module
diff -N og_access.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ og_access.module	8 Apr 2008 00:51:30 -0000
@@ -0,0 +1,146 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_init().
+ * Rebuilds node access table if og_access_rebuild toggle is true.
+ * @todo  This functionality lives in hook_init() because node_access_rebuild() in og_access.install 
+ * doesn't have the desired effect as on install() og_access.module is not loaded.   
+ * Can this be done better?
+ */
+function og_access_init() {
+  if (variable_get('og_access_rebuild', TRUE)) {
+    node_access_rebuild();
+    variable_set('og_access_rebuild', FALSE);
+    drupal_set_message(t('The node access table has been rebuilt.'));
+  }
+}
+
+/**
+ * Implementation of hook_form_alter().
+ * Add public check boxes to node edit form.
+ */
+function og_access_form_alter($form_id, &$form) {
+  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
+    $node = $form['#node'];
+    if (!in_array($node->type, variable_get('og_omitted', array()))) {
+      // get the visibility for normal users
+      $vis = variable_get('og_visibility', 0);
+  
+      // override visibility for og admins and when author only has 1 group
+      if (user_access('administer organic groups') && $vis < 2) {
+        $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
+      }
+      elseif (!count($options)) {
+        // don't show checkbox if no memberships. must be public.
+        $vis = OG_VISIBLE_BOTH;
+      }
+  
+      // If the post is to a private group, visibility must default to one of the private options.
+      // Try not to show checkbox if admin likes to reduce decisions for node authors.
+      $selected_groups = isset($form['#post']['og_groups']) ? array_filter($form['#post']['og_groups']) : $groups;
+      if (count($selected_groups)) {  
+        foreach ($selected_groups as $gid) {
+          $group_node = new stdClass();
+          $group_node->nid = $gid;
+          og_load_group($group_node);
+           if ($group_node->og_private) {
+             $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
+            break;
+          }
+        }
+      } 
+  
+      switch ($vis) {
+        case OG_VISIBLE_BOTH:
+          $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
+          break;
+        case OG_VISIBLE_GROUPONLY:
+          $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 0);
+          break;
+  
+        //user decides how public the post is.
+        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 members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
+          break;
+        case OG_VISIBLE_CHOOSE_PRIVATE:
+          $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 0, '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
+          break;
+      }
+    }
+  }
+}
+
+/**
+ * Implementation of hook_node_grants().
+ */
+function og_access_node_grants($account, $op) { 
+  if (variable_get('og_enabled', FALSE)) {
+    if ($op == 'view') {
+      $grants['og_public'][] = 0; // everyone can see a public node
+    }
+
+    // get subscriptions
+    if ($subscriptions = og_get_subscriptions($account->uid)) {
+      foreach ($subscriptions as $key => $val) {
+        if ($op == 'view') {
+          $grants['og_subscriber'][] = $key;
+        }
+        if (($op == 'update' || $op == 'delete') && $val['is_admin']) {
+          $grants['og_subscriber'][] = $key;
+        }
+      }
+    }
+    return $grants ? $grants : array();
+  }
+}
+
+/**
+ * Implementation of hook_node_acess_records.
+ */
+function og_access_node_access_records($node) {
+  // don't write records if og access control is disabled or the node type is omitted or node is a group
+  if (og_is_omitted_type($node->type) || !variable_get('og_enabled', FALSE)) {
+    return;
+  }
+  
+  if (og_is_group_type($node->type)) {
+    // this grant allows group admins to manage stuff
+    $grants[] = array('realm' => 'og_subscriber', 'gid' => $node->nid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
+    // If the group is not marked private let everyone view the group homepage.
+    if (!$node->og_private) {
+        $grants[] = array (
+          'realm' => 'og_public',
+          'gid' => 0,
+          'grant_view' => 1,
+          'grant_update' => 0,
+          'grant_delete' => 0 );
+    }
+  }
+  elseif (is_array($node->og_groups)) {
+    // applies to non group nodes
+    if ($node->og_public) {
+      $grants[] = array('realm' => 'og_public', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
+    }
+    
+    foreach ($node->og_groups as $gid) {
+      // we write a broad grant here but og_node_grants() only issues it selectively.
+      $grants[] = array('realm' => 'og_subscriber', 'gid' => $gid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
+    }
+  }
+
+  return $grants;
+}
+
+/**
+ * Implementation of hook_node_access_explain.
+ */
+function og_access_node_access_explain($row) {
+  if ($row->realm == 'og_public') {
+    return t('All users may view this node.', array('@op' => $op));
+  }
+  elseif ($row->realm == 'og_subscriber') {
+    $node = node_load((int)$row->gid);
+    return t('Subscribers of !group_n may view this node.', array('@op' => $op, '!group_n' => l($node->title, "node/$row->gid")));
+  }
+}
