Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2015 13:25
Show Gist options
  • Save anonymous/a8f0ddaff7a3b0bc1706 to your computer and use it in GitHub Desktop.
Save anonymous/a8f0ddaff7a3b0bc1706 to your computer and use it in GitHub Desktop.
Safely run a function in Drupal as another user.
<?php
function drupal_run_as($user, $func, $arg1) {
global $user;
$original_user = $user;
$old_state = drupal_save_session();
drupal_save_session(FALSE);
$args = func_get_args();
$user = array_shift($args);
$func = array_shift($args);
$r = call_user_func_array($func, $args);
$user = $original_user;
drupal_save_session($old_state);
return $r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment