? 448850-domain-access.patch
? includes/domain_views_plugin_page.inc
Index: domain_views.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_views/domain_views.module,v
retrieving revision 1.8
diff -u -p -r1.8 domain_views.module
--- domain_views.module	23 Apr 2009 21:42:20 -0000	1.8
+++ domain_views.module	29 Apr 2009 19:33:14 -0000
@@ -60,3 +60,70 @@ function theme_domain_views_view_multipl
   }
   return $output;
 }
+
+/**
+ * Access callback for use with domain_views_plugin_access.
+ *
+ * @param $domains
+ *  An array of domain ids that may access this view.
+ * @return
+ *  Boolean TRUE or FALSE.
+ */
+function domain_views_access($domains) {
+  global $user, $_domain;
+  $grants = domain_view_get_grants();
+  foreach ($grants['domain_id'] as $grant) {
+    if ($grant == 0) {
+      $grant = -1;
+    }
+    if (in_array($grant, $domains)) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
+/**
+ * Helper function to return the node grants for this user.
+ *
+ * @param $account
+ *  The account object of the user requesting the View.
+ * @return $grants
+ *  An array indicating which domains the user may access.
+ */
+function domain_view_get_grants($account = NULL)  {
+  global $user;
+  static $grants;
+  if (empty($account)) {
+    $account = $user;
+  }
+  if (isset($grants[$account->uid])) {
+    return $grants[$account->uid];
+  }
+  $grants = domain_node_grants($account, 'view');
+  // Domain All gets in the way of normal grants.
+  if (!empty($grants['domain_all'])) {
+    $grants['domain_id'] = array($_domain['domain_id']);
+    _domain_views_alter_grants($grants);
+  }
+  $grants[$account->uid] = $grants;
+  return $grants[$account->uid];
+}
+
+/**
+ * Helper function to middor hook_domaingrants().
+ * This should be replaced by a drupal_alter().
+ */
+function _domain_views_alter_grants(&$grants) {
+  static $_modules;
+  if (!isset($_modules)) {
+    $_modules = module_implements('domaingrants');
+  }
+  if (!empty($_modules)) {
+    foreach ($_modules as $module) {
+      // Cannot use module_invoke_all() since these are passed by reference.
+      $function = $module .'_domaingrants';
+      $function($grants, $account, $op);
+    }
+  }
+}
Index: domain_views.views.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_views/domain_views.views.inc,v
retrieving revision 1.7
diff -u -p -r1.7 domain_views.views.inc
--- domain_views.views.inc	23 Apr 2009 21:42:20 -0000	1.7
+++ domain_views.views.inc	29 Apr 2009 19:33:14 -0000
@@ -276,8 +276,27 @@ function domain_views_views_handlers() {
 }
 
 /**
+ * Implement hook_views_plugins().
+ */
+function domain_views_views_plugins() {
+  $path = drupal_get_path('module', 'domain_views') .'/includes';
+  return array(
+    'access' => array(
+      'domain' => array(
+        'title' => t('Domain'),
+        'help' => t('Will be available only on selected domains.'),
+        'handler' => 'domain_views_plugin_access',
+        'uses options' => TRUE,
+        'path' => $path,
+      ),
+    ),
+  );
+}
+
+/**
  * Implementation of hook_views_query_substitutions ()
  */
 function domain_views_query_substitutions($view) {
   return array('***CURRENT_DOMAIN***' => $GLOBALS['_domain']['domain_id']);
 }
+
Index: includes/domain_views_plugin_access.inc
===================================================================
RCS file: includes/domain_views_plugin_access.inc
diff -N includes/domain_views_plugin_access.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/domain_views_plugin_access.inc	29 Apr 2009 19:33:14 -0000
@@ -0,0 +1,62 @@
+<?php
+
+// $Id:$
+
+/**
+ * @file
+ *  Doamin Views plugin that resitrcts View display based on the current domain.
+ *  This plugin respects hook_domaingrants().
+
+/**
+ * Access plugin that provides permission-based access control.
+ */
+class domain_views_plugin_access extends views_plugin_access {
+  function access($account) {
+    $grants = domain_view_get_grants($account);
+    foreach ($grants['domain_id'] as $grant) {
+      if ($grant == 0) {
+        $grant = -1;
+      }
+      if (in_array($grant, array_filter($this->options['domains']))) {
+        return TRUE;
+      }
+    }
+    return FALSE;
+  }
+
+  function get_access_callback() {
+    return array('domain_views_access', array(array_filter($this->options['domains'])));
+  }
+
+  function summary_title() {
+    return t('Domains');
+  }
+
+  function option_defaults(&$options) {
+    $options['domains'] = array(-1);
+  }
+
+  function options_form(&$form, &$form_state) {
+    $domains = domain_domains();
+    $options = array();
+    foreach ($domains as $domain) {
+      // Checkboxes cannot handles zeros.
+      if ($domain['domain_id'] == 0) {
+        $domain['domain_id'] = -1;
+      }
+      $options[$domain['domain_id']] = check_plain($domain['sitename']);
+    }
+    $type = 'checkboxes';
+    if (count($options) > 20) {
+      $type = 'select';
+    }
+    $form['domains'] = array(
+      '#type' => $type,
+      '#multiple' => TRUE,
+      '#options' => $options,
+      '#title' => t('Domains'),
+      '#default_value' => $this->options['domains'],
+      '#description' => t('Only users with the selected permission flag will be able to access this display. Note that users with "access all views" can see any view, regardless of other permissions.'),
+    );
+  }
+}
