Safely impersonating another user

Public

You can run the cron as administrator with this solution. https://drupal.org/node/218104

</> CopyGet raw version
php
  1. <?php
  2. global $user;
  3. $original_user = $user;
  4. $old_state = drupal_save_session();
  5. drupal_save_session(FALSE);
  6. $user = user_load(1);
  7.  
  8. // Take your action here where you pretend to be the user with UID = 1 (typically the admin user on a site)
  9. // If your code fails, it's not a problem because the session will not be saved
  10. $user = $original_user;
  11. drupal_save_session($old_state);
  12.  
  13. // From here on the $user is back to normal so it's OK for the session to be saved
  14. ?>

Comments