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()

  • Current user is an authenticated user with active session at the present. And cron is a process that executes at given periods regardlessly if any user is logged in or not. Two things just do not have anything in common. – AltaGrade Mar 21 '18 at 17:45
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());
|improve this answer|||||

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.