views:

484

answers:

3

Is there a way to allow hook_cron to run with administrator privileges -- e.g a sort of sudo hook_cron() as it were?

This question is a follow up to my earlier question trying to diagnose why hook_cron() functions invoked by /drupal/cron.php run differently that the same functions invoked by /admin/reports/status/run-cron.

My particular function (call it foo_cron()) updates user profiles as part of its nightly duties. But, /drupal/cron.php apparently runs as an anonymous user. Anonymous users cannot modify user profile fields in this site, for obvious reasons, so this part of the script fails.

I know I can work around this by modifying profile_fields directly using SQL, but that feels like an ugly hack, and could be tough to maintain if I start doing this in multiple modules.

Is the a way, within the existing Drupal functions and framework, to make certain foo_cron() jobs run with privs that we normally only give administrators, such as updating profiles?

This particular site is running Drupal 6.1.3.

+1  A: 

All you need to do is save the global $user variable, then change the $user global to a user with that permission and then change the user back again.

Now I wouldn't recommend this but would suggest that you use SQL instead as this approach is very hacky and possible a bit insecure to run cron jobs with admin privs.

googletorp
Could you elaborate a bit on why using global $user is hackier than using SQL? My own thought on the matter is that SQL is hackier because it goes outside Drupal to complete a Drupal task, but I'm curious what led you to that.
anschauung
It's the core of Drupal core that sets the value of the global variables (during the bootstrap I believe). So altering the value of one temporarily (which effect all of Drupal) is more hacky than writing a bit of SQL yourself thatthe function would generate anyways.
googletorp
+1  A: 

From drupal cron manual page: Running cron as an authenticated user

#!/bin/sh
# Reference http://drupal.org/node/479948#comment-1673488 by pearlbear

SITE=https://dev.example.com/
USERNAME=user.name
PASS=ChangeMe!!12

COOKIES=/tmp/cron-cookies.txt
WGETPARAMS="--quiet -O /dev/null --no-check-certificate --save-cookies $COOKIES --keep-session-cookies --load-cookies $COOKIES"
# if you run drupal in a default language different than English you need to modify this
LOGIN="Log%20in"

wget $WGETPARAMS "${SITE}user"
wget $WGETPARAMS --post-data="name=$USERNAME&pass=$PASS&op=$LOGIN&form_id=user_login" "${SITE}user"
wget $WGETPARAMS " ${SITE}cron.php"
mirzu
Perfect ... thanks!
anschauung
A: 

Can you elaborate on how to make the above solution(s) work? In regards to that big snippet posted above, where do we put this? And with the solution of simply changing $user - which elements in $user do we change? It's a large variable with a bunch of values...

Jason
@mirzu's solution is a bash script that you would run from cron -- its a way of loading the normal cron.php in an automated way while acting as a logged-in user. On the other hand, if you want to change $user, a better way is described at http://drupal.org/node/218104
anschauung

related questions