Commit 798fe1a0 authored by Qiangjun Ran's avatar Qiangjun Ran Committed by Qiangjun Ran
Browse files

Issue #3316343 by jungle, timodwhit, erikaagp: Refactor code and get rid of NodeGrantAccessCheck

parent 9933a1cc
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -11,7 +11,9 @@ entity.node.grants:
    _form: '\Drupal\nodeaccess\Form\GrantsForm'
    _title: 'Grants'
  requirements:
    _custom_access: '\Drupal\nodeaccess\AccessChecks\NodeGrantAccessCheck::access'
    node: \d+
    _custom_access: '\Drupal\nodeaccess\Form\GrantsForm::access'
  options:
    _admin_route: TRUE
    parameters:
      node:
        type: entity:node

services.yml

deleted100644 → 0
+0 −5
Original line number Diff line number Diff line
services:
  nodeaccess.access_checker:
      class: Drupal\nodeaccess\AccessChecks\NodeGrantAccessCheck
      tags:
        - { name: access_check }
+0 −34
Original line number Diff line number Diff line
<?php

namespace Drupal\nodeaccess\AccessChecks;

use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\node\Entity\Node;

/**
 * A custom access check for grants form.
 */
class NodeGrantAccessCheck implements AccessInterface {

  /**
   * A custom access check.
   */
  public function access($node, AccountInterface $account) {
    if (!$node) {
      return AccessResult::forbidden();
    }
    $nid = $node;
    $node = Node::load($nid);

    $config = \Drupal::configFactory()->get('nodeaccess.settings');
    $allowed_types = $config->get('allowed_types');
    if ($node && isset($allowed_types[$node->getType()]) && !empty($allowed_types[$node->getType()]) &&
        ($account->hasPermission('grant node permissions') || $account->hasPermission('administer nodeaccess'))) {
      return AccessResult::Allowed();
    }
    return AccessResult::forbidden();
  }

}
+25 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\nodeaccess\Form;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
@@ -9,8 +10,10 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeGrantDatabaseStorageInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -437,4 +440,26 @@ class GrantsForm extends FormBase {
    $form_state->setStorage($values);
  }

  /**
   * Checks access to the `Grants` tab.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node to check access against.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to verify access.
   *
   * @return \Drupal\Core\Access\AccessResultAllowed|\Drupal\Core\Access\AccessResultForbidden
   *   The access result.
   */
  public function access(NodeInterface $node, AccountInterface $account) {
    $config = $this->configFactory->get('nodeaccess.settings');
    $allowed_types = $config->get('allowed_types');
    $node_type = $node->getType();
    if (isset($allowed_types[$node_type]) && !empty($allowed_types[$node_type]) &&
      ($account->hasPermission('grant node permissions') || $account->hasPermission('administer nodeaccess'))) {
      return AccessResult::Allowed();
    }
    return AccessResult::forbidden();
  }

}