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.


Also, for development especially, Devel module's Devel Node Access submodule provides a block that you can display that shows, on any given content page, all the user access permissions/grants provided, and even displays a few users and the access they would be given if they tried accessing the page, editing, deleting, etc.

Check it out: http://drupal.org/project/devel

Also, just a short bit ago, Larry Garfield posted an excellent overview of node access in Drupal 7 (applies almost without any adaptation to the Drupal 6 systems as well).


If your node is not visible by anonymous users, they will be served a 403 access denied page. So I'm pretty sure that your node won't be loaded so you can certainly do something on hook_node_load() and be assured that anonymous will see it.
Have a look at hook_node_access() if you want to control access for your nodes.


To check if the anonymous user can access a node you can use node_access(), using the value returned from drupal_anonymous_user() as last parameter; the first parameter is "view" as you want to check if the anonymous users can view the page.

Anyway, this is not necessary if you want to show a value in a node when the user who is viewing the node is the anonymous user. In this case, you simply use code similar to the following one; in the case the anonymous user doesn't have access to the node, the part inside the IF statement will not be executed.

global $user;

if (empty($user->uid)) {
  // The user is anonymous.

If you want to show data about the anonymous users that are visible to other users too, and show those data only if the anonymous users can view the node, then I would use the node_access() method.

$node->status is equal to 1 when the node is published, and 0 when the node is unpublished. You cannot use it to determinate if the anonymous users can view it, as anonymous users could have the permission to view the unpublished content they created, or there could be a module that implements an extra permission that determinate if an unpublished node can be seen from users who are not administrator users, nor the node's author. The same is true for other users too: If a node is unpublished, it doesn't mean that node cannot be seen from every users.

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.