Skip to content

Instantly share code, notes, and snippets.

@angry-dan
Forked from anonymous/drupal_run_as.php
Created October 24, 2015 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save angry-dan/25d68b9acdfc102ef40b to your computer and use it in GitHub Desktop.
Save angry-dan/25d68b9acdfc102ef40b 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;
}
@johnennewdeeson
Copy link

Do you need $arg1 in the function definition? Won't that generate a PHP Notice when $func has no arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment