-
-
Save angry-dan/25d68b9acdfc102ef40b to your computer and use it in GitHub Desktop.
Safely run a function in Drupal as another user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Do you need $arg1 in the function definition? Won't that generate a PHP Notice when $func has no arguments?