0

I have been trying to get the current authenticated user ID in Hook_cron() function.

But Its showing only the anonymous user, though the current user is an authenticated user.

How Could I get the current authenticated user in hook_cron()

0

1 Answer 1

1

There's no such thing - cron always runs as the anonymous user.

This is forced by the drupal_cron_run() function in Drupal 7:

// Force the current user to anonymous to ensure consistent permissions on
// cron runs.
$original_user = $GLOBALS['user'];
$GLOBALS['user'] = drupal_anonymous_user();

and Cron::run in Drupal 8:

// Force the current user to anonymous to ensure consistent permissions on
// cron runs.
$this->accountSwitcher
  ->switchTo(new AnonymousUserSession());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.