After posting a new question, anonymous users are seeing Access Denied because they are being redirected to the unpublished new node they created. But since they are anonymous, and it is unpublished, they don't have access to it. In drupal 6, they were redirected to the /faq page instead. Can this be fixed to work the way it worked in drupal 6?

Comments

jlea9378’s picture

I removed these lines from faq_ask_form_faq_node_form_alter():

  // Sean Corales: Redirect to faq page if user is anonymous or cannot edit own faq nodes
  global $user;
  if ((!user_access('edit own faq') && !user_access('edit faq')) || ($user->uid == 0)) {
    $form['#redirect'] = 'faq';
  }

and added these lines to faq_ask_submit():

  // Sean Corales: Redirect to faq page if user is anonymous or cannot edit own faq nodes
  global $user;
  if ((!user_access('edit own faq content') && !user_access('edit any faq content')) || ($user->uid == 0)) {
    $form_state['redirect'] = array('faq-page');
  }
  else {
    $form_state['redirect'] = array('node/'.$form_state['nid']);
  }

I tested it and it works for anonymous users or users who lack permissions, but the else branch needs work.
$form_state['nid'] isn't right.

stenjo’s picture

Assigned: Unassigned » stenjo
Status: Active » Needs review

Thanks for the update.
$form_state['nid'] or $form_state['values']['nid'] for that matter does not exist when we are creating a new node. Therefore I have moved some of this to the node_insert hook.
The else path is not needed as the form will redirect to itself anyway unless we tell it differently .-)

stenjo’s picture

I'm really struggeling with the redirect thing. It seems there is something happening in the current code. I have tried a lot of different approaches to making sure the user is redirected to the faq-page if she does not have the access rights to view unpublished nodes, but it seems the redirect field is overwritten at some later stage.

function faq_ask_submit($form, &$form_state) {

  global $user;

  if ($form_state['values']['op'] != t('Save')) {  // If we're not saving then do not do actions
    return;
  }

  // Use only the first term entered in the correct vocabulary.
  $category = array();
  $vocabs = variable_get('faq_ask_vocabularies', 0);
  $faq_cat = variable_get('faq_category_field', 'tags');
  if (isset($form_state['values'][$faq_cat][$form_state['values']['language']])) {
    foreach ($form_state['values'][$faq_cat][$form_state['values']['language']] as $term) {
        $tid = $term['tid'];
        $category[$tid] = $tid;
    }
  }

  // Save this is the node to be created
  $form_state['node']->faq_ask_data = array();
  $form_state['node']->faq_ask_data['categories'] = $category;

  if (variable_get('faq_ask_notify', FALSE)) {  // Are we notifying the expert(s)?

    // Find out who the experts are.
    $query = db_select('faq_expert', 'fe')->fields('fe', array('uid'))->condition('fe.tid', $category, 'IN');
    $experts = $query->execute()->fetchAll();
    // Save in node object for use in the hook_insert() implementation
    $form_state['node']->faq_ask_data['experts'] = $experts;

  }

  // Handle the notification of asker
  if (isset($form_state['values']['faq_email']) && $form_state['values']['faq_email']) {
    $form_state['node']->faq_ask_data['asker-email'] = $form_state['values']['faq_email'];

    // If this user is not registered as a user before - check if all asking anonymous users should be added to the newsletter list
    if (module_exists('simplenews') && $tid = variable_get('faq_ask_notify_asker_simplenews_tid', '0')) {
      // If we have selected a newsletter to add
      if (function_exists('simplenews_subscribe_user')) {
        simplenews_subscribe_user($form_state['values']['faq_email'], $tid,  variable_get('faq_ask_notify_asker_simplenews_confirm', 1), 'FAQ-Ask');
      }
    }
  }
  elseif (isset($form_state['values']['faq_notify']) && $form_state['values']['faq_notify']) {
    $form_state['node']->faq_ask_data['asker-email'] = $user->mail;
  }
  else {
    drupal_set_message(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'), 'status');
  }

  // Issue #1554912 by jlea9378: Access Denied for Anonymous
  if ((!user_access('view own unpublished content')) || ($user->uid == 0)) {
    $form_state['redirect'] = array('faq-page');  // Redirect to faq-page if the user is not allowed to view content
  }

}

jlea9378’s picture

Status: Needs review » Needs work

Hmm I see what you mean. I put a drupal_set_message in the if statement where the redirect gets set, so it IS getting set, but it is getting overwritten later someplace... because I'm still getting an access denied... I know the code is right for the redirect because it was working before (when the node wasn't getting saved).
Can we use drupal_goto at the end of hook_node_update instead, perhaps?

jlea9378’s picture

Still getting "Access denied" in the 5/16/12 dev release.

stenjo’s picture

Status: Needs work » Needs review

After posting the question on the issue queue for the core, #1590022: Setting $form_state['redirect'] is ignored I got an answer that I used to implement some changes. Basically, a lot of what was going on in the submit handler is now moved to the hook_node_insert() implementation.
The result is promising and it now seems that users without permission to see unpublished nodes are correctly redirected to the faq-page as of the latest dev release.

jlea9378’s picture

Status: Needs review » Reviewed & tested by the community

Looks good! Thanks!!

stenjo’s picture

Status: Reviewed & tested by the community » Fixed

I'll consider this fixed then

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Donovan’s picture

Issue summary: View changes

I am seeing this issue with the latest dev version: 7.x-1.0-alpha1+4-dev

Donovan’s picture

Status: Closed (fixed) » Active

Re-opening this issue as I am seeing it with both the alpha version: 7.x-1.0-alpha1, and the latest dev version: 7.x-1.0-alpha1+4-dev

Donovan’s picture

Feedback: To get around this problem I implemented a workaround using Rules. It's a simple rule that redirects to faq-page after saving new content type of FAQ.

I hope this is helpful to someone encountering this issue.