1

I'm working on a Drupal 7 project. And for this I need to get all the users that have a certain permission for a node. I need this info in the hook_node_update method, which means that I can currently get the $node object just fine but I'm unable to find a way to query a list of users that have edit rights to the particular node that's being updated.

Can anyone provide an insight? That would be great!

Thanks.

3 Answers 3

0

Every content type has fix pattern of permission

For node edit permission, Just use user_access() with string parameter "edit own [content-type] content" OR "edit any [content-type] content" If current user has this permission it return TRUE else FALSE

AND

If you want this permission for other user, Just pass user object as second argument in user_access()

0

One problem you might run into is that you can't reliably get this list of users via a database query only since access (or editing) rights can be modified in code by any module. This means that a views based solution probably won't cut it either.

One possibility is to get a shortlist via a view / EntityFieldQuery / db_select(), and then loop through and check if each user has edit access using node_access(). For performance your query could limit to certain roles if you have large numbers of users without edit permissions.

Once you have you have your shortlist (I'm using an array of user objects here but adjust this code if you're working with a different format) do something like this:

$node = node_load(12345);
$accounts_with_edit_access = array();
foreach($shortlist as $delta => $account) {
  if (node_access("update", $node, $account) === TRUE) {
    $accounts_with_edit_access[] = $account;
  }
}
0

So I managed to come up with the following solution:

function revision_author_token_node_update($node) {

  $users = entity_load('user');
  foreach ($users as $user) {
    $account = user_load($user->uid);

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

}

This seems to do the trick as it first loops through all the users and then checks if they have edit rights.

1
  • Just to improve the performance you can skip the line $account = user_load($user->uid); since you already have the user object loaded from entity_load().
    – schlicki
    Commented Aug 12, 2015 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.