diff --git a/og_context/includes/views/og_context_plugin_access_og_perm.inc b/og_context/includes/views/og_context_plugin_access_og_perm.inc index d4050c4..c792099 100644 --- a/og_context/includes/views/og_context_plugin_access_og_perm.inc +++ b/og_context/includes/views/og_context_plugin_access_og_perm.inc @@ -16,13 +16,25 @@ class og_context_plugin_access_og_perm extends views_plugin_access { function access($account) { // Attempt to get the group from the current context and determine if the // user has the appropriate permission within the group - $group = og_context(); - if (!$group) { - // We should not allow access to the view if we were unable to find a - // group - return FALSE; + if ($group = og_context()) { + return og_user_access($group->gid, $this->options['perm'], $account); } - return og_user_access($group->gid, $this->options['perm'], $account); + return FALSE; + } + + /** + * Determine the access callback and arguments. + */ + function get_access_callback() { + return array( + '_og_context_views_page_access', + array( + $this->options['group_type'], + $this->options['perm'], + $this->options['entity_id_arg'], + $this->options['on_multiple'], + ) + ); } /** @@ -34,12 +46,14 @@ class og_context_plugin_access_og_perm extends views_plugin_access { } /** - * Retrieve the options when this is a new access - * control plugin + * Retrieve the options when this is a new access control plugin. */ function option_definition() { $options = parent::option_definition(); $options['perm'] = array('default' => 'edit group'); + $options['group_type'] = array('default' => 'node'); + $options['entity_id_arg'] = array('default' => ''); + $options['on_multiple'] = array('default' => 'all'); return $options; } @@ -56,12 +70,42 @@ class og_context_plugin_access_og_perm extends views_plugin_access { $perms[$module_name][$perm] = strip_tags($info['title']); } + // Build the options for the entity that the user may select. + $og_info = entity_get_info('group'); + $entity_options = og_get_all_group_entity(); + $entity_options['group'] = $og_info['label']; + asort($entity_options); + $form['perm'] = array( '#type' => 'select', '#options' => $perms, '#title' => t('OG permission'), '#default_value' => $this->options['perm'], - '#description' => t('Only users with the selected permission flag in a group retrieved from context will be able to access this display.') + '#description' => t('Only users with the selected permission flag in a group retrieved from context will be able to access this display.'), + ); + $form['group_type'] = array( + '#type' => 'select', + '#options' => $entity_options, + '#title' => t('Group entity type'), + '#default_value' => $this->options['group_type'], + '#description' => t('Select which entity type the group entity should be. If the entity is the og entity then select "@og_type".', array('@og_type' => $og_info['label'])), + ); + $form['entity_id_arg'] = array( + '#type' => 'textfield', + '#title' => t('Argument position for group ID'), + '#size' => 1, + '#default_value' => $this->options['entity_id_arg'], + '#description' => t('Entity ID (Entity forming the group you want) position accessed with the arg() function. e.g If your dynamic path is "node/%/overview" to choose "2" like "arg(2)".'), + ); + $form['on_multiple'] = array( + '#type' => 'select', + '#title' => t('How should access be determine for multiple groups?'), + '#options' => array( + 'all' => t('Verify access for all groups'), + 'first' => t('Verify access for only the first group'), + ), + '#default_value' => $this->options['on_multiple'], + '#description' => t('If the specified argument results in multiple groups, how should page access be determined within those groups.'), ); } } diff --git a/og_context/og_context.module b/og_context/og_context.module index 6c8a83a..c585f22 100644 --- a/og_context/og_context.module +++ b/og_context/og_context.module @@ -568,3 +568,53 @@ function _group_context_handler_entity($entity_type = 'node', $entity = NULL, $p return $context; } + +/** + * Helper function to handle views page access. + * + * @param string $entity_type + * The group's entity type. + * @param string $perm + * The group permission to search for. + * @param integer $entity_id_arg + * The position in arg() where the group's entity ID can be found. + * @param string $action_on_multiple + * How to determine access when multiple groups are found. Possible values + * include: + * - all: Verify that the user has the specified permission in all groups. + * - first: Verify that the user has the specified permission in only the + * first group. + * + * @return boolean + * Whether or not the current user has the desired permission to access the + * view page. + */ +function _og_context_views_page_access($entity_type, $perm, $entity_id_arg, $action_on_multiple = 'all') { + $access = FALSE; + if ($entity_id_arg != '' && is_numeric($entity_id_arg)) { + $entity_ids = arg($entity_id_arg); + if (!empty($entity_ids)) { + $entity_ids = explode(',', $entity_ids); + + // If more than one group argument was found but we only need to verify + // the first group then trim down the entity ids. + if (count($entity_ids) > 1 && $action_on_multiple == 'first') { + $entity_ids = array(array_shift($entity_ids)); + } + + foreach (entity_load($entity_type, $entity_ids) as $entity) { + // If the user does not have the desired permission for this group then + // there is no need to continue. + $access = ('group' == $entity_type + ? og_user_access($entity->gid, $perm) + : og_user_access_entity($perm, $entity_type, $entity)); + if (!$access) { + break; + } + } + } + } + + return $access; +} +