By blogbold on
I want to check if a node can be viewed by anyone (anonymous user).
If a user is logged in, the result should be the same.
The problem seems to be:
If user logged in has view-permission, the test should return false if an anonymous user has no view-permission.
I tried:
// get the anonymous user (might be wrong or incomplete)
$any_user = user_load(array('name' => ''));
// check view access for anonymous user
$any_access = node_access('view', $node, $any_user);
If a user is logged in, this returns true (or >=1) if the logged in user has view permission, even if anonymous user has no view permission.
I use the module "node_access" to grant permissions. The configuration works as it should.
Version of Drupal 6 and module is the current/latest stable.
btw: the function node_access() returns not only TRUE/FALSE but also a number (maybe how many permissions are set that allow access).
Comments
Try..
Looks as if http://api.drupal.org/api/function/node_access/6 can indeed return an integer greater than 1 (in which case treat it as TRUE).
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
drupal_anonymous_user()
Thanks!
drupal_anonymous_user() obviously is the correct function to get the anonymous user.
Unfortunately, the problem is the same:
if a user is logged in, $anon_has_access is true even if anonymous user doesn't have (view) permission.
It should be false - I need a test that returns the view permission of the anonymous user, regardless of user logged in or not.
What does $node look
What does $node look like?
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
Re: What does $node look
copy&paste relevant lines:
Hmm must have missed
Hmm must have missed something simple. Looks like that should work. Maybe backup node.module and dive in, adding debug statements to http://api.drupal.org/api/function/node_access/6 (unless you have a PHP debugger). Need to find which return statement is being executed in http://api.drupal.org/api/function/node_access/6, and what $node and $user look like in that function. print_r and drupal_set_message() may come in handy.
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
Maybe bug in module nodeaccess 6.x-1.2?
Attention to all readers: maybe I'm completely wrong, and my patch has bad sideeffects!
I think the function nodeaccess_node_grants() has a bug: it doesn't use the argument $account.
file: nodeaccess/nodeaccess.module (only the function)
nodeaccess module 6.x-1.2 original code:
modified so argument $account is used (with fallback to $user, as seen in node.module):
Note the use of
$accountin the last lines ($roles = ...andreturn array...) of code!With this modification, my module gets the correct view access permission for the anonymous user, regardless of user logged in or not.
Seems to be a confirmed bug...
http://drupal.org/node/323977
Yes that would explain
Yes that would explain what's going on!
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk