How do I check if a user has view access to a node, in a template file? I'm currently working on a simple LMS in D8 and have already defined the fundamental content types. Among them is a content type called Course, which can be considered "Premium" content, i.e. all users may access those fields that contain more general information, but the users will have to buy a subscription for accessing the full content.

To make that possible without any Premium module (which, at the moment, have not been ported to Drupal 8), I created a simple grid-view and deactivated "SQL rewriting", which works perfectly, so far. For displaying the nodes in this view, I have created a new view mode, called gallery, so that the nodes that are displayed can be styled in a template.
I would like to apply different CSS styles to those nodes in the view that the user may access. I'm not sure if this can be done in the view itself.

Anyway, I'm currently thinking of checking the view permission inside node--course--gallery.html and I would like to add a class to the article tag, if access is permitted.

Unfortunately, I can't seem to find the right function for this. I set it up like this:

{%
  set classes = [
    node.bundle|clean_class,
    node.isPromoted() ? 'is-promoted',
    node.isSticky() ? 'is-sticky',
    not node.isPublished() ? 'is-unpublished',
    view_mode ? view_mode|clean_class,
    user.hasPermission('view nodes') ? 'is-permitted',
    node.access('view') ? 'is-permitted',
    'clearfix',
  ]
%}


Neither user.hasPermission('view nodes') ? 'is-permitted' nor node.access('view') ? 'is-permitted' seem to have any effect.

Do you have any idea?
Not really an answer, more like a suggestion for an alternative suggestion where that problem should exist, at least not exactly like that.

They way I handled similar problems before (paywall for newspapers in my case) is to not use the node access system for this. In my case, I did it using logic in the template/preprocess as well as dynamically switching the view mode. If you can handle it on a field-by-field basis, then you can also implement hook_entity_field_access() and deny view access to just those specific fields.

That said, I would expect that the method isn't allowed to be called due to the twig sandbox that only allows is/get method calls, but then I'd kind of expect it to throw an exception.

I guess also with my suggestion, you'll need a way to check for something in the template but I would simply do that in hook_preprocess_node() then, add it as a boolean variable to $variables.