My code was from a module where impersonation recursively could happen, this is why I use a stack to save user state. I am also saving it to session and not using a static variable because I may uses batch functions.
But I see some problems right now: - To be safe impersonation should be saved into static variables (and "using session_save_session(FALSE);" ), so if anything goes wrong, $user is not afected. - If I want to be able to execute every Rules action in a batch, I need to change user "per action" Any idea of how to "inject" code before and after a rule action? 2013/1/21 Walt Daniels <wdli...@gmail.com> > We do the latter simple trick in our store code so we can show people the > member prices. We have a dummy id that is a member so we switch to it, pick > up the price and use it so we can show both member and non-member prices on > the screen. > > > On Mon, Jan 21, 2013 at 9:18 AM, Christian López Espínola < > penyask...@computer.org> wrote: > >> Haven't tested your code, but maybe you want to take a look at >> masquerade_switch_user >> and masquerade_switch_back: >> http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module >> . >> >> And if the impersonation is only for the given request, maybe just >> >> global $user; >> $previous = $user; >> $user = user_load($uid); >> // Do your stuff. >> $user = $previous; >> >> would work. >> >> >> >> On Mon, Jan 21, 2013 at 10:37 AM, Lluís Forns <enb...@gmail.com> wrote: >> >>> Some of my scheduled task needs to run as the user who triggered them >>> (not anonymous neither admin); so I thought of writing two actions: >>> - One to impersonate a user, which will receive uid as an argument. >>> - Another to undo the impersonate. >>> All the called actions should be between them. >>> >>> Anybody see any fault in this idea or the actions code? >>> >>> Thanks. >>> >>> function wu_action_impersonate($uid) { >>> global $user; >>> if (!array_key_exists('wu_action_impersonate', $_SESSION)) { >>> $_SESSION['wu_action_impersonate'] = array(); >>> } >>> array_push($_SESSION['wu_action_impersonate'], $user); >>> session_save_session(FALSE); >>> $user = user_load($uid); >>> user_load($uid); >>> } >>> >>> function wu_action_unimpersonate() { >>> global $user; >>> if (array_key_exists('wu_action_impersonate', $_SESSION)) { >>> if (!empty($_SESSION['wu_action_impersonate'])) { >>> $user = array_pop($_SESSION['wu_action_impersonate']); >>> } >>> if (empty($_SESSION['wu_action_impersonate'])) { >>> unset($_SESSION['wu_action_impersonate']); >>> } >>> } >>> session_save_session(TRUE); >>> } >>> >>> -- >>> *Ser freak no és imprescindible per ser informàtic, però ajuda. >>> *La vida no ha de ser feliç, ha de ser plena. >>> *Abans d'imprimir aquest missatge, pensa en el medi ambient. >>> >> >> >> >> -- >> Cheers, >> >> Christian López Espínola <penyaskito AT computer DOT org> >> http://twitter.com/penyaskito | http://penyaskito.com >> > > -- *Ser freak no és imprescindible per ser informàtic, però ajuda. *La vida no ha de ser feliç, ha de ser plena. *Abans d'imprimir aquest missatge, pensa en el medi ambient.