Is it possible to get node permissions through PHP. Like I load node id by ID and test it thru user ID. Is this user has permission to view this node or not.

I'm using nodeaccess module and I want to archive this through grants (assigning some users have rights to see and some not depending content). Calling something like if node page is ID = 12 (to get all project certain page). Then if it is I load some project nodes there (created new content type). I have to be sure, that viewer (account) has permission to see this data.


I am not sure if I fully understand your question but this is what I make out of it: you want to check if a certain user has access to a certain node, am I right?

If this is the case, this can easily be achieved with the following code:

// Use this if you want to load the current user.
global $user;
// Use this if you want to load a user by UID.
$user = user_load($uid);
// Load the node object.
$node = node_load($nid);
// Perform the access check.
if (node_access('view', $node, $user)) {
  // The user has permission.

You can read more about node_access in the Drupal docs.

  • Thank you for your answer! I tried something similar, but it does not work like I expect.<br/> Code: // Use this if you want to load a user by UID. $user = user_load(16); // Load the node object. $node = node_load(12); // Perform the access check. if (node_access('view', $node, $user)) { echo "test"; } User with ID 12 does not have node (ID 16) view access. It is set by using grants tab. And view is unchecked. But it will still print out "test". – Tõnis Nov 23 '15 at 16:37

I found out real problem. Patrick Scheffer answer is correct but if content is created before installing nodeaccess it does not work. All new content works like expected.

  • Nice! Not sure if this works, but you can also rebuild the permissions to update old content. This can be done via admin/reports/status/rebuild or navigate to Reports -> Status report and the link is under the Node Access Permissions row. – Patrick Scheffer Nov 24 '15 at 8:52
  • Sadly no to be honest. I rebuilt those multiple times. – Tõnis Nov 24 '15 at 20:31

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.