You are here

function _homebox_can_view_block in Homebox 7.3

Same name and namespace in other branches
  1. 6.3 homebox.module \_homebox_can_view_block()
  2. 6.2 homebox.module \_homebox_can_view_block()
  3. 7.2 homebox.module \_homebox_can_view_block()

Determine if user has access to view a block

Parameters

$block: A block object

Return value

Boolean value of user's access to the block

2 calls to _homebox_can_view_block()
homebox_build in ./homebox.module
Responsible for firing the hook_theme().
homebox_prepare_block in ./homebox.module
Prepare a block for rendering with theme('homebox_block').

File

./homebox.module, line 824
Homebox main file, takes care of global functions settings constants, etc.

Code

function _homebox_can_view_block($block) {
  global $user;

  // Detect valid blocks in the system.
  $valid_blocks =& drupal_static(__FUNCTION__ . ':valid_blocks');
  if (!is_array($valid_blocks)) {
    $valid_blocks = array();
    $results = db_query("SELECT module, delta FROM {block}");
    foreach ($results as $record) {
      $valid_blocks[$record->module][$record->delta] = TRUE;
    }
  }

  // Check if the view access is requested for a valid block.
  if (empty($valid_blocks[$block->module][$block->delta])) {
    return FALSE;
  }

  // Check for roles set at the block level.
  $allowed_roles =& drupal_static(__FUNCTION__ . ':allowed_roles');
  if (!is_array($allowed_roles)) {
    $allowed_roles = array();

    // Since this function is usually called once for each block on a page, and
    // {block_role} doesn't usually contain many records, it's cheaper to cache
    // them all at once. (Especially since no index on module+delta exists.)
    $result = db_query('SELECT module, delta, rid FROM {block_role}');
    foreach ($result as $record) {
      $allowed_roles[$record->module . '-' . $record->delta][$record->rid] = TRUE;
    }
  }
  if (isset($allowed_roles[$block->module . '-' . $block->delta])) {

    // Role restrictions were set on the block. Check whether any of ther user's
    // roles is among them.
    $our_allowed_roles = array_intersect_key($user->roles, $allowed_roles[$block->module . '-' . $block->delta]);
    return !empty($our_allowed_roles);
  }

  // If here, user has access
  return TRUE;
}