I need to do something witn current node by my module only if anonymous users can view this node. How to check it?

By default I use $node->status == 1, but some other modules can change anonymous access rights also for published nodes.


The check if the anonymous user has access to a given $node, you can use the node_access() like this: node_access('view', $node, user_load(0)).

As a side note, $node->status == 1 will check that the node is published. An unpublished node is usually not visible by anonymous user. Which users are allowed to view unpublished node depends on the granted permissions and node access module enabled on the site.

  • If I give the "view own unpublished content" permission to the anonymous users, they will be able to see the unpublished nodes assigned to the anonymous user. – kiamlaluno Aug 6 '11 at 18:26

Do you use a special module to set node permissions? Or just core Drupal?

If just core Drupal, then I think anonymous users only need the "access content" permission, and maybe you could check this with (I have not tested this code):

$anonymous = user_load(0); // load anonymous user
$can_access_content = user_access('access content', $anonymous);
  • Drupal 7 uses a different permission. – kiamlaluno Aug 6 '11 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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