<div dir="ltr">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.<div>
<br></div><div style>But I see some problems right now:</div><div style>- 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.</div>
<div style>- If I want to be able to execute every Rules action in a batch, I need to change user "per action"<br></div><div style><br></div><div style>Any idea of how to "inject" code before and after a rule action?</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/1/21 Walt Daniels <span dir="ltr"><<a href="mailto:wdlists@gmail.com" target="_blank">wdlists@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">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.</div>
<div class="HOEnZb"><div class="h5">
<div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jan 21, 2013 at 9:18 AM, Christian López Espínola <span dir="ltr"><<a href="mailto:penyaskito@computer.org" target="_blank">penyaskito@computer.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Haven't tested your code, but maybe you want to take a look at <span style="line-height:16.796875px;font-size:12px;background-color:rgb(248,248,248);white-space:pre-wrap;font-family:Monaco,'Courier New',monospace">masquerade_switch_user </span>and<span style="line-height:16.796875px;font-size:12px;background-color:rgb(248,248,248);white-space:pre-wrap;font-family:Monaco,'Courier New',monospace"> </span><span style="line-height:16.796875px;font-size:12px;background-color:rgb(248,248,248);white-space:pre-wrap;font-family:Monaco,'Courier New',monospace">masquerade_switch_back: </span><a href="http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module" target="_blank">http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module</a>.<div>
<br></div><div>And if the impersonation is only for the given request, maybe just</div><div> </div><div> global $user;</div><div> $previous = $user;</div><div> $user = user_load($uid);</div>
<div>
// Do your stuff.</div><div> $user = $previous;</div><div><br></div><div>would work.</div><div><br></div></div><div class="gmail_extra"><div><div><br><br><div class="gmail_quote">On Mon, Jan 21, 2013 at 10:37 AM, Lluís Forns <span dir="ltr"><<a href="mailto:enboig@gmail.com" target="_blank">enboig@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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:<div>
- One to impersonate a user, which will receive uid as an argument.</div>
<div>- Another to undo the impersonate.</div><div>All the called actions should be between them.</div><div><br></div><div>Anybody see any fault in this idea or the actions code?</div><div><br></div><div>Thanks.</div>
<div><br></div><div><div>function wu_action_impersonate($uid) {</div><div> global $user;</div><div> if (!array_key_exists('wu_action_impersonate', $_SESSION)) {</div><div> $_SESSION['wu_action_impersonate'] = array();</div>
<div> }</div><div> array_push($_SESSION['wu_action_impersonate'], $user);</div><div> session_save_session(FALSE);</div><div> $user = user_load($uid);</div><div> user_load($uid);</div><div>}</div><div><br></div>
<div>function wu_action_unimpersonate() {</div><div> global $user;</div><div> if (array_key_exists('wu_action_impersonate', $_SESSION)) {</div><div> if (!empty($_SESSION['wu_action_impersonate'])) {</div>
<div> $user = array_pop($_SESSION['wu_action_impersonate']);</div><div> }</div><div> if (empty($_SESSION['wu_action_impersonate'])) {</div><div> unset($_SESSION['wu_action_impersonate']);</div>
<div> }</div><div> }</div><div> session_save_session(TRUE);</div><div>}</div><span><font color="#888888"><div><br></div>-- <br>*Ser freak no és imprescindible per ser informàtic, però ajuda.<br>*La vida no ha de ser feliç, ha de ser plena.<br>
*Abans d'imprimir aquest missatge, pensa en el medi ambient.
</font></span></div></div>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span><font color="#888888">-- <br>Cheers,<br><br>Christian López Espínola <penyaskito AT computer DOT org><br><a href="http://twitter.com/penyaskito" target="_blank">http://twitter.com/penyaskito</a> | <a href="http://penyaskito.com" target="_blank">http://penyaskito.com</a>
</font></span></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>*Ser freak no és imprescindible per ser informàtic, però ajuda.<br>*La vida no ha de ser feliç, ha de ser plena.<br>*Abans d'imprimir aquest missatge, pensa en el medi ambient.
</div>