1

How can I programatically determine whether a certain user has "view" (or any other operation) permission for a given node?

I'm trying this, but it doesn't seem to be calling the node access hooks, and it's overall pretty ugly:

$node_grant_storage = \Drupal::service('node.grant_storage');

$current_user_has_access = is_a(
  $node_grant_storage->access(
    $node,
    'view',
    \Drupal::currentUser()->getAccount()
  ),
  'Drupal\Core\Access\AccessResultAllowed'
);

Ideally I'd like something to return a boolean and not a class (I have no use for the "neutral" option here, I just want to know if a user went into the node/X page, would they get a 403 or not).

2

This was easier than it seemed:

$current_user_has_access = $node->access();

This function has an optional parameter where you can pass an arbitrary user account, which defaults to the logged in user (suits my purpose).

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.