webform_results_access.module 1.97 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// FROM: http://tylerfrankenstein.com/code/drupal-webform-submission-results-user-access-control

/**
 * Implementation of hook_webform_results_access().
 */
function webform_results_access_webform_results_access($node, $account = NULL) {
  return webform_results_access_webform_access($node, $account);
}

/**
 * Implementation of hook_webform_submission_access().
 */
function webform_results_access_webform_submission_access($node, $submission, $op = 'view', $account = NULL) {
  return webform_results_access_webform_access($node, $account);
}

/**
 * Returns true if account is a value in the webform results user
 * reference field on the webform content type.
 *
 * @param object $node
 *   The webform node.
 * @param object $account
 *   The user account, optional. Defaults to current user.
 *
 * @return bool
 *   Returns true if user is listed, false otherwise.
 */
function webform_results_access_webform_access ($node, $account = NULL) {
  global $user;
  $account = isset($account) ? $account : $user;
  $access = false;
34
  if (isset($node->field_webform_results_access)) {
35 36 37 38 39 40 41
    // This webform has user(s) specified for submission results access control.
    if (user_access("administer nodes") || user_access("edit any webform content")) {
      $access = true;
    }
    else {
      // For each user specified, make sure the current user is one of them,
      // otherwise don't show the results.
42 43
      if (isset($node->field_webform_results_access['und'])){ //bug quick fix for anonymous users 170128
       foreach ($node->field_webform_results_access['und'] as $i => $ref) {
44 45 46 47 48 49 50
	// User Reference
        if (isset($ref['uid']) && (($ref['uid'] == $account->uid))) {
          $access = true;
          break;
        }
	// Entity Reference
        if (isset($ref['target_id']) && (($ref['target_id'] == $account->uid))) {
51 52 53
          $access = true;
          break;
        }
54 55
       }
      } //end of if (isset($node->field_webform_results_access['und']))      
56 57 58 59
    }
  }
  return $access;
}