<div dir="ltr">Haven't tested your code, but maybe you want to take a look at <span style="color:rgb(0,0,0);font-family:Monaco,'Courier New',monospace;font-size:12px;line-height:16.796875px;white-space:pre;background-color:rgb(248,248,248)">masquerade_switch_user </span>and<span style="color:rgb(0,0,0);font-family:Monaco,'Courier New',monospace;font-size:12px;line-height:16.796875px;white-space:pre;background-color:rgb(248,248,248)"> </span><span style="color:rgb(0,0,0);font-family:Monaco,'Courier New',monospace;font-size:12px;line-height:16.796875px;white-space:pre;background-color:rgb(248,248,248)">masquerade_switch_back: </span><a href="http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module">http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module</a>.<div>
<br></div><div style>And if the impersonation is only for the given request, maybe just</div><div style> </div><div style> global $user;</div><div style> $previous = $user;</div><div style> $user = user_load($uid);</div>
<div style>
// Do your stuff.</div><div style> $user = $previous;</div><div style><br></div><div style>would work.</div><div style><br></div></div><div class="gmail_extra"><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 class="HOEnZb"><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>-- <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>
</div>