You are here

class uc_addresses_views_access in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 views/uc_addresses_views_access.inc \uc_addresses_views_access

Access plugin that provides access control based on custom PHP code.

Hierarchy

Expanded class hierarchy of uc_addresses_views_access

3 string references to 'uc_addresses_views_access'
UcAddressesViewsTestCase::createDisplayAccessView in tests/uc_addresses.views.test
Creates a View for Display access test.
uc_addresses_views_default_views in views/uc_addresses.views_default.inc
Implements hook_views_default_views().
uc_addresses_views_plugins in views/uc_addresses.views.inc
Implements hook_views_plugins().

File

views/uc_addresses_views_access.inc, line 13
Contains uc_addresses_views_access class.

View source
class uc_addresses_views_access extends views_plugin_access {

  /**
   * Implements views_plugin_access#summary_title().
   */
  public function summary_title() {
    $title = array();
    switch ($this->options['access_type']) {
      case 'view':
        $title[] = t('View access');
        break;
      case 'edit':
        $title[] = t('Edit access');
        break;
      case 'delete':
        $title[] = t('Delete access');
        break;
    }
    if ($this->options['uid_argument'] && $this->options['aid_argument']) {
      $title[] = t('by User ID and Address ID');
    }
    elseif ($this->options['uid_argument']) {
      $title[] = t('by User ID');
    }
    elseif ($this->options['aid_argument']) {
      $title[] = t('by Address ID');
    }
    else {
      return t('Error! No arguments selected.');
    }
    return implode(', ', $title);
  }

  /**
   * Implements views_plugin_access#option_definition().
   */
  public function option_definition() {
    $options = parent::option_definition();
    $options['uid_argument'] = array(
      'default' => NULL,
    );
    $options['aid_argument'] = array(
      'default' => NULL,
    );
    $options['access_type'] = array(
      'default' => 'view',
    );
    return $options;
  }

  /**
   * Implements views_plugin#options_form().
   */
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['help'] = array(
      '#type' => 'markup',
      '#value' => t('With this access control type you can restrict access to this display based on an user ID argument and/or an address ID argument.') . ' ' . t('Note that this currently only works for path based displays, such as  "!page" displays.', array(
        '!page' => t('Page'),
      )),
    );

    // Get list of Views arguments.
    $handlers = $this->display->handler
      ->get_handlers('argument');

    // User can select specific arguments to pass on.
    $options = array(
      '' => t('- None -'),
    );
    foreach ($handlers as $key => $handler) {
      $options[$key] = $handler->definition['title'];
    }
    $form['uid_argument'] = array(
      '#type' => 'select',
      '#title' => t('User argument'),
      '#description' => t('Optionally select an Views argument that represents an User ID. Only users with permission to access addresses of this user will have access to this display.'),
      '#options' => $options,
      '#default_value' => $this->options['uid_argument'],
    );
    $form['aid_argument'] = array(
      '#type' => 'select',
      '#title' => t('Address ID argument'),
      '#description' => t('Optionally select an Views argument that represents an address ID. Only users with permission to access this address will have access to this display.'),
      '#options' => $options,
      '#default_value' => $this->options['aid_argument'],
    );

    // User can choose which access operation to check: view, edit or delete.
    $form['access_type'] = array(
      '#type' => 'select',
      '#title' => t('Access type'),
      '#description' => t('Select for which type of operation permissions should be checked. Usually "!access_type".', array(
        '!access_type' => t('View'),
      )),
      '#options' => array(
        'view' => t('View'),
        'edit' => t('Edit'),
        'delete' => t('Delete'),
      ),
      '#default_value' => $this->options['access_type'],
    );
  }

  /**
   * Implements views_plugin#options_validate().
   *
   * Checks if at least one argument is selected.
   *
   * @todo Implement!
   */
  public function options_validate(&$form, &$form_state) {
    $option_values = $form_state['values']['access_options'];
    if (empty($option_values['uid_argument']) && empty($option_values['aid_argument'])) {
      form_set_error('access_options][uid_argument', t('Please select at least one argument.'));
      form_set_error('access_options][aid_argument');
    }
  }

  /**
   * Implements views_plugin_access#access().
   */
  public function access($account) {
    $args = $this->view->args;
    if (count($args) < 1) {

      // Not enough information. Deny access.
      return FALSE;
    }

    // Pick placeholder path.
    $path = implode('/', array_fill(1, count($args), '%'));
    $positions = $this
      ->uc_addresses_find_arg_positions($path);
    $uid = NULL;
    $aid = NULL;
    if (isset($args[$positions['uid']])) {
      $uid = $args[$positions['uid']];
    }
    if (isset($args[$positions['aid']])) {
      $aid = $args[$positions['aid']];
    }
    return uc_addresses_check_access_by_ids($this->options['access_type'], $uid, $aid, $account);
  }

  /**
   * Implements views_plugin_access#get_access_callback().
   */
  public function get_access_callback() {

    // Find out at which position in the path the given argument is.
    if (isset($this->display->display_options['path'])) {
      $positions = $this
        ->uc_addresses_find_arg_positions($this->display->display_options['path']);
      return array(
        'uc_addresses_check_access_by_ids',
        array(
          $this->options['access_type'],
          $positions['uid'],
          $positions['aid'],
        ),
      );
    }

    // If no path is applied for this display, then there are no restrictions.
    return TRUE;
  }

  /**
   * Looksup the position in the path for each argument.
   *
   * @param string $path
   *   The path to examine.
   *
   * @return array positions
   *   An array with the following items:
   *   - uid_arg_position: integer if found, NULL otherwise.
   *   - aid_arg_position: integer if found, NULL otherwise.
   */
  protected function uc_addresses_find_arg_positions($path) {
    $positions = array(
      'uid' => NULL,
      'aid' => NULL,
    );

    // Get list of Views arguments.
    $handlers = $this->display->handler
      ->get_handlers('argument');

    // Find the positions for any arguments embedded in the path via '%'.
    $i = 0;
    $args = array();
    foreach (explode('/', $path) as $element) {
      if ($element == '%') {
        $args[] = $i;
      }
      $i++;
    }

    // Find out which argument handler belongs to which position.
    $j = 0;
    foreach ($handlers as $key => $handler) {
      switch ($key) {
        case $this->options['uid_argument']:
        case $this->options['aid_argument']:

          // Check if this argument is explicitly noted in the path.
          if (isset($args[$j])) {

            // The argument is one of the '%' signs in the path.
            $position = $args[$j];
          }
          else {

            // The argument is not defined in the path. Assume the
            // argument is at the end of the path.
            $position = $i;
            $i++;
          }
          if ($key == $this->options['uid_argument']) {
            $positions['uid'] = $position;
          }
          if ($key == $this->options['aid_argument']) {
            $positions['aid'] = $position;
          }
          break;
      }
      $j++;
    }
    return $positions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
uc_addresses_views_access::access public function Implements views_plugin_access#access(). Overrides views_plugin_access::access
uc_addresses_views_access::get_access_callback public function Implements views_plugin_access#get_access_callback(). Overrides views_plugin_access::get_access_callback
uc_addresses_views_access::options_form public function Implements views_plugin#options_form(). Overrides views_plugin_access::options_form
uc_addresses_views_access::options_validate public function Implements views_plugin#options_validate(). Overrides views_plugin_access::options_validate
uc_addresses_views_access::option_definition public function Implements views_plugin_access#option_definition(). Overrides views_plugin_access::option_definition
uc_addresses_views_access::summary_title public function Implements views_plugin_access#summary_title(). Overrides views_plugin_access::summary_title
uc_addresses_views_access::uc_addresses_find_arg_positions protected function Looksup the position in the path for each argument.
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::query public function Add anything to the query that we might need to. 7
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_access::init public function Initialize the plugin.
views_plugin_access::options_submit public function Provide the default form form for submitting options. Overrides views_plugin::options_submit 1