How can I restrict user access to view nodes based on field value? I am setting up a relatively simple website using Drupal 8.  There are several different content types.  Within each content type I would like some content to be available to anonymous users and some only available to authenticated users (i.e. 'member only' content).

I would like to add a simple tick box field that would control whether the content was publicly available or not.  i.e. if the field is ticked then it is available to anyone but if it isn't ticked then it is only available to members.

I have managed to get some way to achieving this by using Node View Permissions and creating a duplicate of each content type - both have the same fields but one is visible to anonymous users and the other is only visible to authenticated users.  This works, but isn't very satisfactory because it requires duplicate content types.  I would prefer to have a single content type with a field that controls view permissions.

Do you have any suggestions?
You could use a programmatically solution using hook_node_access(), in this example the field_YOURFIELD is a checkbox.

/**
 * Implements hook_node_access().
 */
function MYMODULE_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
  $access = new \Drupal\Core\Access\AccessResultNeutral();
  if ($node-&gt;hasField('field_YOURFIELD')) {
    $field = $node-&gt;get('field_YOURFIELD')-&gt;getString();
    if (empty($field) &amp;&amp; $account-&gt;id() != $node-&gt;getOwnerId()) {
      $access = new \Drupal\Core\Access\AccessResultForbidden();
    }
  }
  $access-&gt;addCacheableDependency($field);
  $access-&gt;addCacheableDependency($node);
  return $access;
}