You are here

function certificate_can_access_certificate in Certificate 7.2

Same name and namespace in other branches
  1. 8.3 certificate.module \certificate_can_access_certificate()
  2. 6.2 certificate.module \certificate_can_access_certificate()
  3. 6 certificate.module \certificate_can_access_certificate()
  4. 7.3 certificate.module \certificate_can_access_certificate()
  5. 3.x certificate.module \certificate_can_access_certificate()

Check if a user can access a certificate for this node.

This function:

Return value

TRUE if certificate tab should show and be accessible. @return string (eval to true for Drupal's menu) if certificate tab should show but be denied with a message. @return FALSE if certificate tab should be hidden.

3 calls to certificate_can_access_certificate()
CertificateTestCase::testCertificateAccess in ./certificate.test
certificate_node_certificate in ./certificate.pages.inc
Get certificate for a specific node.
certificate_node_view in ./certificate.module
Implements hook_node_view().
1 string reference to 'certificate_can_access_certificate'
certificate_menu in ./certificate.module
Implements hook_menu().

File

./certificate.module, line 607
Certificate module.

Code

function certificate_can_access_certificate($node, $account = NULL, $flush = FALSE) {
  static $cert_access = array();
  $found_true = NULL;
  $found_false = NULL;
  $admin = user_access('administer certificates');
  $view_all = user_access('view all user certificates');

  // Use account of a different user if allowed.
  if (($admin || $view_all) && arg(3) > 0) {
    $account = user_load(arg(3));
  }
  if (!$account) {
    global $user;
    $account = $user;
  }
  if (!$account->uid) {
    return FALSE;
  }
  if (!certificate_node_is_certifiable($node)) {
    return FALSE;
  }
  if ($flush || !isset($cert_access[$node->nid])) {
    $access = module_invoke_all('access_certificate', $node, $account);
    $cert_access[$node->nid] = $access;
  }
  else {
    $access = $cert_access[$node->nid];
  }
  foreach ($access as $item) {
    if ($item === TRUE) {

      // Something said the leaner should access the certificate.
      $found_true = TRUE;
    }
    if (is_string($item)) {

      // Something returned a string, return it (will show the menu, but error)
      return $item;
    }
    if ($item === FALSE) {
      $found_false = TRUE;
    }
  }
  if ($found_true) {
    if ($found_false) {

      // Found TRUE and FALSEs.
      return FALSE;
    }

    // Only found TRUE.
    return TRUE;
  }

  // All were false.
  return FALSE;
}