7

I need to know a function or a way to find out if a user can edit a certain node.

For example I have the nid 200 and the user foo has logged in, I want to check if he is able to edit node/200.

Thanks

11

You can use node_access to do this:

$node = node_load(123);
$account = user_load(456);

if (node_access("update", $node, $account) === TRUE) {
  do_something_awesome();
}

If you look at the source for node_access, you will see that the $node argument can also be a $nid. This is how the path for the edit pages get checked (see the 'node/%node/edit' entry in node_menu).

  • Thanks for your answer, but then it seems like I'm doing something wrong. pastebin.com/tjcWbZNc - only as admin I'll get the right output. But as a certain user, I can edit the node, but node_access won't return true. – lumio Jul 6 '12 at 15:13
  • Okay, it only works if the permission "Bypass content access control" is set. – lumio Jul 6 '12 at 15:22
  • well... I think it's because of a plugin I've installed - I found another way to reach my goal... but I think your way is the normal way. Thanks – lumio Jul 6 '12 at 18:40
  • If you dont use $the account parameter, it defaults to the current user. – batigolix Oct 21 '15 at 11:34

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.