[development] Rules action to impersonate a user

Lluís Forns enboig at gmail.com
Mon Jan 21 09:37:09 UTC 2013

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?


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);
  $user = 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'])) {

*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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20130121/27eceaa1/attachment.html 

More information about the development mailing list