You are here

function ctools_access in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/context.inc \ctools_access()

Determine if the current user has access via a plugin.

Parameters

array $settings: An array of settings theoretically set by the user, including the array of plugins to check:

  • 'plugins': the array of plugin metadata info to check
  • 'logic': (optional) either 'and' or 'or', indicating how to combine restrictions. The 'or' case is not fully implemented and returns the input contexts unchanged.

array $contexts: An array of zero or more contexts that may be used to determine if the user has access.

Return value

bool TRUE if access is granted, FALSE if otherwise.

4 calls to ctools_access()
ctools_access_menu in ./ctools.module
Determine if the current user has access via a plugin.
ctools_context_handler_select in includes/context-task-handler.inc
Compare arguments to contexts for selection purposes.
ctools_ruleset_ctools_access_check in ctools_access_ruleset/plugins/access/ruleset.inc
Check for access.
page_manager_page_access_check in page_manager/plugins/tasks/page.inc
Callback to determine if a page is accessible.

File

includes/context.inc, line 1944
Contains code related to the ctools system of 'context'.

Code

function ctools_access($settings, $contexts = array()) {
  if (empty($settings['plugins'])) {
    return TRUE;
  }
  if (!isset($settings['logic'])) {
    $settings['logic'] = 'and';
  }
  if (!isset($contexts['logged-in-user'])) {
    $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  }
  foreach ($settings['plugins'] as $test) {
    $pass = FALSE;
    $plugin = ctools_get_access_plugin($test['name']);
    if ($plugin && ($function = ctools_plugin_get_function($plugin, 'callback'))) {

      // Do we need just some contexts or all of them?
      if (!empty($plugin['all contexts'])) {
        $test_contexts = $contexts;
      }
      else {
        $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
        $context = isset($test['context']) ? $test['context'] : array();
        $test_contexts = ctools_context_select($contexts, $required_context, $context);
      }
      $pass = $function($test['settings'], $test_contexts, $plugin);
      if (!empty($test['not'])) {
        $pass = !$pass;
      }
    }
    if ($pass && $settings['logic'] == 'or') {

      // Pass if 'or' and this rule passed.
      return TRUE;
    }
    elseif (!$pass && $settings['logic'] == 'and') {

      // Fail if 'and' and this rule failed.
      return FALSE;
    }
  }

  // Return TRUE if logic was and, meaning all rules passed.
  // Return FALSE if logic was or, meaning no rule passed.
  return $settings['logic'] === 'and';
}