0

I have a rule set up that runs during cron. Part of that rule is an action that is supposed to save a PDF (via Entity Print which only authenticated users can access) to my server. It works fine when I manually execute it, but when I schedule it, and it gets executed via cron, I get an access denied error.

I have tried to Safely Impersonate Another User, and have also tried to send cookies with the request (based off the Rules download module), but continue to get access denied errors.

global $user;
$original_user = $user;
$old_state = drupal_save_session();
drupal_save_session(FALSE);
$user = user_load(1);

$headers = array();
if ($cookie) {
  $headers = array(
    'Cookie' => $_SERVER['HTTP_COOKIE'],
  );
}

$response = drupal_http_request($url, array('headers' => $headers));

$user = $original_user; 
drupal_save_session($old_state); 

Is there a way to execute this drupal_http_request as an authenticated user during cron?

  • D7, look for switch-user-rule-module, error comes from that cron runs as anonimous user – pinueve Mar 11 at 18:17
  • I have tried adding those actions as well. They work in other rules, but I still get an access denied error when using drupal_http_request with them. – MrPeanut Mar 11 at 18:19
  • On the site running drupal_http_request(), there isn't any difference between running it as anonymous user and authenticated user. If you are expecting the remote site to which the request is addressed to recognize the user as authenticated, you need to do more than passing a cookie to the server. – kiamlaluno Mar 11 at 20:19
  • It is the same site. I am just trying to save the Entity Print PDF to the server. – MrPeanut Mar 11 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.