Running Cron As An Authenticated User

It's clear from the Drupal 7 documentation, cron jobs don't play nice together when you need the cron job be associated with a non-anonymous publishing task. But, there is a way to solve this issue with a quick snippet of code on a per case basis.


You can find more in the documentation here: Configuring cron jobs using the cron command which then lead to a dead end at Documentation on running cron as an authenticated user is out of date. Though, there is a work around solution for Drupal 6, there was none for Drupal 7. I’ll focus on an example and solution which you should be able to apply to other scenarios (just maybe not with the same hook). For this case we’ll use Feeds and hook_node_presave to manually change what should be happening if the request was being processed by a specific authenticated user.

Feeds Imports as Authenticated User

Feeds uses cron jobs when you syndicate an RSS feed or CSV import to nodes. Nodes are always created by an anonymous user. You can see this happening by looking at your watchdog reports after a feed import happens in Watchdog:

- Watchdog: http://domain.com/admin/reports/dblog

Even though the node author of my nodes was set as uid 1, the fields being created were not honoring the creators access permissions. This is a problem spot for everyone using a feed import to node on a site that is also public facing. Chances are you don’t want to give anonymous users access to use the full_html text filter by default just so that my nodes can be displayed properly when syndicated RSS feeds to a node.

The solution was to manually set it in hook_node_presave($node):

 * Implements hook_node_presave().
function module_node_presave($node) {
 // If it belongs to a feed processing nid then it was created by a feed.
 // I also want this to only happen on the initial import (is_new) and be changeable later via node edit.
 if (!empty($node->feeds_item->feed_nid) && $node->is_new == 1) {
 // If the node author is uid 1 then we always want to use the full_html filter for these node imports.
 if (!empty($node->uid) && $node->uid == 1) {
 $node->body['und'][0]['format'] = 'publishing';

It’s not a solution for everyone with this general problem without a bit of tweaking perhaps, but for the Feeds case specifically you should be able to copy/paste into a module. Other scenarios where your cron job is not creating a node will need a different hook or addition to your custom module that uses hook_cron.

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.

Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois