Over in an issue on the webform_entity module (#1794960: Put the logic for checking if a user can submit webforms into a function) we were trying to test if someone had permissions to create a webform submission... While webform_submission_access() will tell you about editing, deleting, listing or viewing... it doesn't say anything about creation.

All the logic for create permissions is stuck in webform_node_view() where no one else can use it:


  if ($node->webform['status'] == 0) {
    $closed = TRUE;
    $enabled = FALSE;
  }
  else {
    // Check if the user's role can submit this webform.
    if (variable_get('webform_submission_access_control', 1)) {
      foreach ($node->webform['roles'] as $rid) {
        $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
      }
      if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
        $enabled = FALSE;
      }
    }
    else {
      // If not using Webform submission access control, allow for all roles.
      $allowed_roles = array_keys(user_roles());
    }
  }

  // Get a count of previous submissions by this user. Note that the
  // webform_submission_access() function may disable the page cache for
  // anonymous users if they are allowed to edit their own submissions!
  if ($page && webform_submission_access($node, NULL, 'list')) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');
    $submission_count = webform_get_submission_count($node->nid, $user->uid);
  }

  // Check if this page is cached or not.
  $cached = $user->uid == 0 && (variable_get('cache', 0) || drupal_page_is_cacheable() === FALSE);

  // Check if the user can add another submission.
  if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
    module_load_include('inc', 'webform', 'includes/webform.submissions');

    // Disable the form if the limit is exceeded and page cache is not active.
    if (($user_limit_exceeded = _webform_submission_user_limit_check($node)) && !$cached) {
      $enabled = FALSE;
    }
  }

  // Check if the user can add another submission if there is a limit on total
  // submissions.
  if ($node->webform['total_submit_limit'] != -1) { // -1: Submissions are never throttled.
    module_load_include('inc', 'webform', 'includes/webform.submissions');

    // Disable the form if the limit is exceeded and page cache is not active.
    if (($total_limit_exceeded = _webform_submission_total_limit_check($node)) && !$cached) {
      $enabled = FALSE;
    }
  }

  // Check if this user has a draft for this webform.
  $is_draft = FALSE;
  if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {
    // Draft found - display form with draft data for further editing.
    if ($draft_sid = _webform_fetch_draft_sid($node->nid, $user->uid)) {
      module_load_include('inc', 'webform', 'includes/webform.submissions');
      $submission = webform_get_submission($node->nid, $draft_sid);
      $enabled = TRUE;
      $is_draft = TRUE;
    }
  }

And finally that gets distilled down to the $enabled variable:

  $node->content['webform'] = array(
    '#theme' => 'webform_view',
    '#node' => $node,
    '#teaser' => $teaser,
    '#page' => $page,
    '#form' => $form,
    '#enabled' => $enabled,
    '#weight' => 10,
  );

So it seems like the cleanest way to check the create permissions at this point is:

$copy = clone $node;
webform_node_view($copy, 'foo');
if ($copy->content['webform']['#enabled']) {
 // we're good to go...
}

Which is kind of embarrassing.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drewish’s picture

Issue summary: View changes

Adding a link to the other issue.

vengador’s picture

Hi @drewish,

I have developed a patch for 7.x-4.x branch with function you need. I have tested it in a project and it seems to work fine. I hope it be useful ;)

vengador’s picture

Status: Active » Needs review
quicksketch’s picture

Status: Needs review » Needs work

Hi @vengador, thanks for the patch! I like the idea of making this its own function. However, it would be much preferred for me if we actually used this new function inside of webform_node_view() to replace the current code. Having the exact same code in two places (one of which isn't even used) means that the functionality would likely break in the future, especially without tests to ensure its functionality.

DanChadwick’s picture

Status: Needs work » Closed (won't fix)

Closing for lack of activity. If this is still an issue, please re-open with a patch along the lines of what quicksketch wanted.