I have a custom module which implements hook_node_view() and checks certain conditions to decide whether a user can view the content or not. The code snippet is:

    if( ! user_is_logged_in() && $node->type == "abc") {
    else {
        //do nothing

The code works fine for me but the problem occurs when drupal's cron runs as Anonymous user. The only message I get is Attempting to re-run cron while it is already running. Could you please give me your suggestion to handle this.

1 Answer 1


hook_node_view() shouldn't be used for access checks, there's hook_node_access() for that sort of situation:

function MYMODULE_node_access($node, $op, $account) {
  $type = is_string($node) ? $node : $node->type;
  if ($type == 'abc' && $op == 'view') {
    return $account->uid > 0;

That'll handle the access check for the node gracefully, and shouldn't stop cron running halfway through.

  • I also wanted to check if there are certain fields marked or not i.e. only deny if the node has taxonomy term 'A grade' else allow the user to view the content or node. Apr 23, 2013 at 8:26
  • No problem there, you can do that from hook_node_access() too. Making access checks, and invoking a 403 from as late on as the _view() hook, isn't the best idea is all
    – Clive
    Apr 23, 2013 at 8:27
  • But how will the hook differentiate between corn running as anonymous and normal anonymous user. Apr 23, 2013 at 9:29
  • It won't...cron is always run as an anonymous. See drupal_cron_run(), line 5200ish of /includes/common.inc: // Force the current user to anonymous to ensure consistent permissions on cron runs. $original_user = $GLOBALS['user']; $GLOBALS['user'] = drupal_anonymous_user();. There's nothing to differentiate the context the bootstrap happens in, Drupal just knows that it's bootstrapped and has some env variables available
    – Clive
    Apr 23, 2013 at 9:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.