How can I determine whether a user has permission to access to a certain page?
17 8 | |||||
|
21 | If you want to verify if the currently logged-in user has access to a page, you can use the following code:
The code works in Drupal 6 and higher versions, and it is the one used frommenu_execute_active_handler(). The reason I am not suggesting to directly call the access callback is because the arguments that need to be passed to that function. The code used by _menu_check_access() is the following one (Drupal 7):
The code, which needs to be as much generic as possible, doesn't directly handle a user object. This means that is not possible to replace the user object for the currently logged-in user with another user object.
In both the definitions, the access arguments don't include a user object, and node_access()in this case uses the user object for the currently logged in user. In the second case one of the argument is the node object that is obtained from the URL; for example, if the URL is example.com/node/1, then the second argument passed to the access callback is the node object for the node with node ID equal to 1. If you want to check if a user that is not the currently logged-in user can access a menu is to first change the value of the global variable | ||||
8 | Try drupal_valid_path(). The function returns
| ||||||||
|
3 | Call the Beware that some modules might not pass the user as a separate argument (as specified by the | ||||||||
|
2 | Check out the
| ||||||||||||||||||||
|
2 | If you need to know if a user can access a particular node, and are using a node access module, you can use node_access(). (without a node access module they just need the 'access content' permission.) If you want to figure out if a user can access an arbitrary path that is defined by a hook_menu() implementation, you may have to retrieve the menu entry from the database and evaluate its 'access callback' parameter. | ||
0 |
|