<div dir="ltr">It is ok to me; you can take my code as start if you want. I think it wouldn't be any problem (and action calling die() could help to check this) if you don't "close" the rule as long as you use "session_save_session(FALSE);" (I read your project); but it will not hurt.<div>
<br></div><div><div><?php</div><div><br></div><div>/**</div><div> * Implementation of hook_rules_action_info().</div><div> */</div><div>function rules_switch_user_rules_action_info() {</div><div> return array(</div><div>
'mymodule_action_impersonate' => array(</div><div> 'label' => t('Select the user to masquerade as'),</div><div> 'arguments' => array(</div><div> 'uid' => array('type' => 'string', 'label' => t('uid')),</div>
<div> ),</div><div> 'module' => 'WU',</div><div> 'help' => t('Use this action to change the user for next actions'),</div><div> ),</div><div> 'mymodule_action_unimpersonate' => array(</div>
<div> 'label' => t('Return to previous user'),</div><div> 'help' => t('It returns to the user who called the rule.'),</div><div> 'module' => 'WU',</div>
<div> ),</div><div> );</div><div>}</div><div><br></div><div>function rules_switch_user_action_impersonate($uid) {</div><div> global $user;</div><div> if (!array_key_exists('rules_switch_user_action_impersonate', $_SESSION)) {</div>
<div> $_SESSION['rules_switch_user_action_impersonate'] = array();</div><div> }</div><div> array_push($_SESSION['rules_switch_user_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 rules_switch_user_action_unimpersonate() {</div><div> global $user;</div><div> if (array_key_exists('rules_switch_user_action_impersonate', $_SESSION)) {</div>
<div> if (!empty($_SESSION['rules_switch_user_action_impersonate'])) {</div><div> $user = array_pop($_SESSION['rules_switch_user_action_impersonate']);</div><div> }</div><div> if (empty($_SESSION['rules_switch_user_action_impersonate'])) {</div>
<div> unset($_SESSION['rules_switch_user_action_impersonate']);</div><div> }</div><div> }</div><div> session_save_session(TRUE);</div><div>}</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
2013/1/23 Michael Moritz <span dir="ltr"><<a href="mailto:mimo@gn.apc.org" target="_blank">mimo@gn.apc.org</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi<br>
<br>
I got inspired by this and created a very basic module to have these actions<br>
available in Rules <a href="http://drupal.org/project/rules_switch_user" target="_blank">http://drupal.org/project/rules_switch_user</a><br>
<br>
What do you think?<br>
<br>
Regards,<br>
<br>
mimo<br>
<div class="HOEnZb"><div class="h5"><br>
On Tuesday 22 Jan 2013 15:00:38 Lluís Forns wrote:<br>
> Ok.<br>
><br>
> Till now I have created a pair of actions; one which I call at the start of<br>
> my rule to impersonate "node changing user", and one at last to change<br>
> back. Using this I can call scheduled rules with the user who triggered<br>
> them. It is all I need to the moment, so I will stop looking further into<br>
> batching actions (limited time).<br>
><br>
> Thanks.<br>
><br>
><br>
> 2013/1/21 Earnie Boyd <<a href="mailto:earnie@users.sourceforge.net">earnie@users.sourceforge.net</a>><br>
><br>
> > drupal_static() is not equivalent to PHP static. The example you've<br>
> > given is using PHP static and yes, it will not be available in another<br>
> > function.<br>
> ><br>
> ><br>
> > <a href="http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_" target="_blank">http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_</a><br>
> > static/7><br>
> > On Mon, Jan 21, 2013 at 11:21 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>> wrote:<br>
> > > I know we are going OT, but I also want to understand this;<br>
> > ><br>
> > > what about:<br>
> > ><br>
> > ><br>
> > > function mymodule_complex_calculation() {<br>
> > ><br>
> > > static $cache;<br>
> > > if (isset($cache)) {<br>
> > ><br>
> > > return $cache;<br>
> > ><br>
> > > }<br>
> > > // Otherwise we need to perform our calculations<br>
> > > // ...some complex calculation that returns<br>
> > > $cache = complex_calculation();<br>
> > > return $cache;<br>
> > ><br>
> > > }<br>
> ><br>
> > <a href="http://techblog.zabuchy.net/2011/enhancing-drupal-performance-with-drupal_" target="_blank">http://techblog.zabuchy.net/2011/enhancing-drupal-performance-with-drupal_</a><br>
> > static/><br>
> > > You are saying that if I instantiate a variable in<br>
> ><br>
> > "mymodule_impersonate()"<br>
> ><br>
> > > it will not be available in "mymodule_unimpersonate()"?<br>
> > ><br>
> > ><br>
> > > 2013/1/21 Earnie Boyd <<a href="mailto:earnie@users.sourceforge.net">earnie@users.sourceforge.net</a>><br>
> > ><br>
> > >> You "think" wrongly. A static variable has local scope to the<br>
> > >> function declaring the static variable.<br>
> > >><br>
> > >> On Mon, Jan 21, 2013 at 11:03 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>> wrote:<br>
> > >> > I think static are also available to other functions, use them to<br>
> ><br>
> > store<br>
> ><br>
> > >> > cached information across my code and they work.<br>
> > >> ><br>
> > >> ><br>
> > >> > 2013/1/21 Darren Oh <<a href="mailto:darrenoh@sidepotsinternational.com">darrenoh@sidepotsinternational.com</a>><br>
> > >> ><br>
> > >> >> Static variables are only saved for a single function. Global<br>
> ><br>
> > variables<br>
> ><br>
> > >> >> are available to all functions during a single page request. Session<br>
> > >> >> variables are available to all page requests during a session.<br>
> > >> >><br>
> > >> >> On Jan 21, 2013, at 10:08 AM, Lluís Forns wrote:<br>
> > >> >><br>
> > >> >> Darren, thanks for the link. What is the difference between using<br>
> > >> >> global<br>
> > >> >> or static to save the data?<br>
> > >> >><br>
> > >> >><br>
> > >> >><br>
> > >> >> 2013/1/21 Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>><br>
> > >> >><br>
> > >> >>> My code was from a module where impersonation recursively could<br>
> > >> >>> happen,<br>
> > >> >>> this is why I use a stack to save user state. I am also saving it<br>
> > >> >>> to<br>
> > >> >>> session<br>
> > >> >>> and not using a static variable because I may uses batch functions.<br>
> > >> >>><br>
> > >> >>> But I see some problems right now:<br>
> > >> >>> - To be safe impersonation should be saved into static variables<br>
> ><br>
> > (and<br>
> ><br>
> > >> >>> "using session_save_session(FALSE);" ), so if anything goes wrong,<br>
> > >> >>> $user is<br>
> > >> >>> not afected.<br>
> > >> >>> - If I want to be able to execute every Rules action in a batch, I<br>
> > >> >>> need<br>
> > >> >>> to change user "per action"<br>
> > >> >>><br>
> > >> >>> Any idea of how to "inject" code before and after a rule action?<br>
> > >> >>><br>
> > >> >>><br>
> > >> >>> 2013/1/21 Walt Daniels <<a href="mailto:wdlists@gmail.com">wdlists@gmail.com</a>><br>
> > >> >>><br>
> > >> >>>> We do the latter simple trick in our store code so we can show<br>
> ><br>
> > people<br>
> ><br>
> > >> >>>> the member prices. We have a dummy id that is a member so we<br>
> > >> >>>> switch<br>
> > >> >>>> to it,<br>
> > >> >>>> pick up the price and use it so we can show both member and<br>
> > >> >>>> non-member<br>
> > >> >>>> prices on the screen.<br>
> > >> >>>><br>
> > >> >>>><br>
> > >> >>>> On Mon, Jan 21, 2013 at 9:18 AM, Christian López Espínola<br>
> > >> >>>><br>
> > >> >>>> <<a href="mailto:penyaskito@computer.org">penyaskito@computer.org</a>> wrote:<br>
> > >> >>>>> Haven't tested your code, but maybe you want to take a look at<br>
> ><br>
> > >> >>>>> masquerade_switch_user and masquerade_switch_back:<br>
> > <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>.<br>
> ><br>
> > >> >>>>> And if the impersonation is only for the given request, maybe<br>
> > >> >>>>> just<br>
> > >> >>>>><br>
> > >> >>>>> global $user;<br>
> > >> >>>>> $previous = $user;<br>
> > >> >>>>> $user = user_load($uid);<br>
> > >> >>>>><br>
> > >> >>>>> // Do your stuff.<br>
> > >> >>>>><br>
> > >> >>>>> $user = $previous;<br>
> > >> >>>>><br>
> > >> >>>>> would work.<br>
> > >> >>>>><br>
> > >> >>>>><br>
> > >> >>>>><br>
> > >> >>>>> On Mon, Jan 21, 2013 at 10:37 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>><br>
> > >> >>>>><br>
> > >> >>>>> wrote:<br>
> > >> >>>>>> Some of my scheduled task needs to run as the user who triggered<br>
> > >> >>>>>> them<br>
> > >> >>>>>> (not anonymous neither admin); so I thought of writing two<br>
> ><br>
> > actions:<br>
> > >> >>>>>> - One to impersonate a user, which will receive uid as an<br>
> ><br>
> > argument.<br>
> ><br>
> > >> >>>>>> - Another to undo the impersonate.<br>
> > >> >>>>>> All the called actions should be between them.<br>
> > >> >>>>>><br>
> > >> >>>>>> Anybody see any fault in this idea or the actions code?<br>
> > >> >>>>>><br>
> > >> >>>>>> Thanks.<br>
> > >> >>>>>><br>
> > >> >>>>>> function wu_action_impersonate($uid) {<br>
> > >> >>>>>><br>
> > >> >>>>>> global $user;<br>
> > >> >>>>>> if (!array_key_exists('wu_action_impersonate', $_SESSION)) {<br>
> > >> >>>>>><br>
> > >> >>>>>> $_SESSION['wu_action_impersonate'] = array();<br>
> > >> >>>>>><br>
> > >> >>>>>> }<br>
> > >> >>>>>> array_push($_SESSION['wu_action_impersonate'], $user);<br>
> > >> >>>>>> session_save_session(FALSE);<br>
> > >> >>>>>> $user = user_load($uid);<br>
> > >> >>>>>> user_load($uid);<br>
> > >> >>>>>><br>
> > >> >>>>>> }<br>
> > >> >>>>>><br>
> > >> >>>>>> function wu_action_unimpersonate() {<br>
> > >> >>>>>><br>
> > >> >>>>>> global $user;<br>
> > >> >>>>>> if (array_key_exists('wu_action_impersonate', $_SESSION)) {<br>
> > >> >>>>>><br>
> > >> >>>>>> if (!empty($_SESSION['wu_action_impersonate'])) {<br>
> > >> >>>>>><br>
> > >> >>>>>> $user = array_pop($_SESSION['wu_action_impersonate']);<br>
> > >> >>>>>><br>
> > >> >>>>>> }<br>
> > >> >>>>>> if (empty($_SESSION['wu_action_impersonate'])) {<br>
> > >> >>>>>><br>
> > >> >>>>>> unset($_SESSION['wu_action_impersonate']);<br>
> > >> >>>>>><br>
> > >> >>>>>> }<br>
> > >> >>>>>><br>
> > >> >>>>>> }<br>
> > >> >>>>>> session_save_session(TRUE);<br>
> > >> >>>>>><br>
> > >> >>>>>> }<br>
> > >> >>>>>><br>
> > >> >>>>>> --<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.<br>
> > >> >>>>><br>
> > >> >>>>> --<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><br>
> > >> >>><br>
> > >> >>> --<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.<br>
> > >> >><br>
> > >> >> --<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.<br>
> > >> ><br>
> > >> > --<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.<br>
> > >><br>
> > >> --<br>
> > >> Earnie<br>
> > >> -- <a href="https://sites.google.com/site/earnieboyd" target="_blank">https://sites.google.com/site/earnieboyd</a><br>
> > ><br>
> > > --<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.<br>
> ><br>
> > --<br>
> > Earnie<br>
> > -- <a href="https://sites.google.com/site/earnieboyd" target="_blank">https://sites.google.com/site/earnieboyd</a><br>
</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>