[development] Rules action to impersonate a user
Christian López Espínola
penyaskito at computer.org
Mon Jan 21 14:18:34 UTC 2013
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 <enboig at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20130121/d90f975b/attachment.html
More information about the development
mailing list