2

I'm using Rules to e-mail myself the content of a Mini Panel, which contains several views that are restricted to admin-only access.

I am using a component (Action set) that consists of two actions:

  • Send e-mail
  • Schedule component evaluation (re-schedule the same component to be evaluated the following day)

For Send e-mail, the body of my message looks like this (based on this code):

<?php $block = module_invoke('panels_mini', 'block_view', 'admin_user_role_statistics'); print $block['content']; ?>

When I first triggered this component (using a rule that I created to trigger the component for the first time by viewing a user profile), I was logged in as an administrator, so the e-mail I received shows all of the content of the mini panel as I expected. However, the following day, when the scheduled component was evaluated, the body of the e-mail message was blank, presumably because it was not sent by an admin user.

I re-enabled the rule to trigger the component and tried it as a non-admin user: the message body was empty. I then tried it as an administrator: the message had the full content.

So, what can I do to make sure the component is evaluated as if triggered by an admin user?

2
  • So the actions are being executed by he who triggers the rule ? How is your component triggered ?
    – drcelus
    Commented Mar 11, 2013 at 8:32
  • The problem is when the component is triggered by rules scheduler, which apparently triggers the component as an anonymous user... Commented Mar 11, 2013 at 8:56

2 Answers 2

2
+50

The problem seems to be an anonymous user triggering the component and since you want an admin user to run the component, you will have to impersonate the admin user.

Try this code inside your components to impersonate the admin user :

global $user;
// Save the current user
$current_user = $user;
// Save user session
$current_session = drupal_save_session();
// From now on we don't want to save the session, since it would be unsafe
drupal_save_session(FALSE);
// We impersonate admin user
$user = user_load(1);
// Do wathever needs to be done.
// ...
// Now let's restore the triggering user and session.
$user = $current_user;
drupal_save_session($current_session);
2
  • I added this code, inserting the line from my question to print the block. However, unfortunately this did not work for me; I still get an e-mail with nothing in the body. Commented Mar 13, 2013 at 12:35
  • Somehow I failed to add global $user; when I cut and paste the code. I got it working using the example you provided... thanks! Commented Mar 15, 2013 at 14:45
2

This page has more detail about how to set up rules scheduler to run as admin. (Wouldn't it be nice if someone made a module to solve this problem?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.