Commit b7c95d9e authored by Lucas Barbosa de Almeida's avatar Lucas Barbosa de Almeida Committed by Alison on Ubuntu VM
Browse files

Issue #3145629 by lucasbaralm, bruno.bicudo, Aakansha Tyagi, Suresh Prabhu...

Issue #3145629 by lucasbaralm, bruno.bicudo, Aakansha Tyagi, Suresh Prabhu Parkala, michelecris, penyaskito, alison, WagnerMelo, drupal.ninja03, jungle, Shashwat Purav: Fix Coding Standards
parent 8f7a9abc
Loading
Loading
Loading
Loading
+8 −3
Changes for nodeaccess.module: 8 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -69,7 +69,10 @@ function nodeaccess_node_access_records($node) {
    else {
      // Otherwise, check access to unpublished content for authenticated and
      // anonymous users.
      $role_perms = user_role_permissions([AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE]);
      $role_perms = user_role_permissions([
        AccountInterface::ANONYMOUS_ROLE,
        AccountInterface::AUTHENTICATED_ROLE,
      ]);
      // Anonymous user setting.
      $grants[] = [
        'gid' => AccountInterface::ANONYMOUS_ROLE,
@@ -161,7 +164,6 @@ function nodeaccess_entity_update(EntityInterface $entity) {
  }
}


/**
 * Implements hook_entity_insert().
 */
@@ -230,7 +232,10 @@ function nodeaccess_node_type_insert(NodeTypeInterface $type) {
  $config = \Drupal::configFactory()->getEditable('nodeaccess.settings');

  // New node type, default to whatever is set for access content permission.
  $role_perms = user_role_permissions([AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE]);
  $role_perms = user_role_permissions([
    AccountInterface::ANONYMOUS_ROLE,
    AccountInterface::AUTHENTICATED_ROLE,
  ]);
  $anonymous_access = (int) in_array('access content', $role_perms[AccountInterface::ANONYMOUS_ROLE]);
  $authenticated_access = (int) in_array('access content', $role_perms[AccountInterface::AUTHENTICATED_ROLE]);

+37 −10
Changes for src/Form/ConfigForm.php: 37 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -4,14 +4,41 @@ namespace Drupal\nodeaccess\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Builds the configuration form.
 */
class ConfigForm extends ConfigFormBase {

  /**
   * The entity type manager.
   *
   * @var Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a grants form object.
   *
   * @param Drupal\Core\Entity\EntityTypeManagerInterface $entitytype_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entitytype_manager) {
    $this->entityTypeManager = $entitytype_manager;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -45,7 +72,7 @@ class ConfigForm extends ConfigFormBase {
      $this->t('EDIT'),
      $this->t('DELETE'),
    ];
    $node_types = NodeType::loadMultiple();
    $node_types = $this->entityTypeManager->getStorage('node')->loadMultiple();
    $roles = user_roles();

    $form['priority'] = [
@@ -147,7 +174,7 @@ class ConfigForm extends ConfigFormBase {
      $form[$type]['show'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Show grant tab for this node type'),
        '#default_value' => isset($allowed_types[$type]) ? $allowed_types[$type] : 0,
        '#default_value' => isset($allowed_types[$type]) ?? 0,
      ];

      $form[$type]['user_permissions'] = [
@@ -163,15 +190,15 @@ class ConfigForm extends ConfigFormBase {
          ],
          'grant_view' => [
            '#type' => 'checkbox',
            '#default_value' => isset($user_perms[$id]['grant_view']) ? $user_perms[$id]['grant_view'] : 0,
            '#default_value' => isset($user_perms[$id]['grant_view']) ?? 0,
          ],
          'grant_update' => [
            '#type' => 'checkbox',
            '#default_value' => isset($user_perms[$id]['grant_update']) ? $user_perms[$id]['grant_update'] : 0,
            '#default_value' => isset($user_perms[$id]['grant_update']) ?? 0,
          ],
          'grant_delete' => [
            '#type' => 'checkbox',
            '#default_value' => isset($user_perms[$id]['grant_delete']) ? $user_perms[$id]['grant_delete'] : 0,
            '#default_value' => isset($user_perms[$id]['grant_delete']) ?? 0,
          ],
        ];
      }
@@ -181,15 +208,15 @@ class ConfigForm extends ConfigFormBase {
        ],
        'grant_view' => [
          '#type' => 'checkbox',
          '#default_value' => isset($user_perms['author']['grant_view']) ? $user_perms['author']['grant_view'] : 0,
          '#default_value' => isset($user_perms['author']['grant_view']) ?? 0,
        ],
        'grant_update' => [
          '#type' => 'checkbox',
          '#default_value' => isset($user_perms['author']['grant_update']) ? $user_perms['author']['grant_update'] : 0,
          '#default_value' => isset($user_perms['author']['grant_update']) ?? 0,
        ],
        'grant_delete' => [
          '#type' => 'checkbox',
          '#default_value' => isset($user_perms['author']['grant_delete']) ? $user_perms['author']['grant_delete'] : 0,
          '#default_value' => isset($user_perms['author']['grant_delete']) ?? 0,
        ],
      ];

@@ -203,7 +230,7 @@ class ConfigForm extends ConfigFormBase {
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Update configuration.
    $values = $form_state->getValues();
    $node_types = NodeType::loadMultiple();
    $node_types = $this->entityTypeManager->getStorage('node')->loadMultiple();
    $allowed_types = [];

    $settings = $this->config('nodeaccess.settings')
+96 −22
Changes for src/Form/GrantsForm.php: 96 added lines, 22 removed lines.
Original line number Diff line number Diff line
@@ -3,15 +3,91 @@
namespace Drupal\nodeaccess\Form;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeGrantDatabaseStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Builds the configuration form.
 */
class GrantsForm extends FormBase {

  /**
   * The current database.
   *
   * @var Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The configuration factory.
   *
   * @var Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity type manager.
   *
   * @var Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The node grant storage.
   *
   * @var Drupal\node\NodeGrantDatabaseStorageInterface
   */
  protected $nodeGrantStorage;

  /**
   * The messenger.
   *
   * @var Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a grants form object.
   *
   * @param Drupal\Core\Database\Connection $database
   *   The database.
   * @param Drupal\Core\Config\ConfigFactoryInterface $configfactory
   *   The configuration factory.
   * @param Drupal\Core\Entity\EntityTypeManagerInterface $entitytype_manager
   *   The entity type manager.
   * @param Drupal\node\NodeGrantDatabaseStorageInterface $nodegrant_storage
   *   The node grant storage.
   * @param Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(Connection $database, ConfigFactoryInterface $configfactory, EntityTypeManagerInterface $entitytype_manager, NodeGrantDatabaseStorageInterface $nodegrant_storage, MessengerInterface $messenger) {
    $this->database = $database;
    $this->configFactory = $configfactory;
    $this->entityTypeManager = $entitytype_manager;
    $this->nodeGrantStorage = $nodegrant_storage;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('database'),
      $container->get('config.factory'),
      $container->get('entity_type.manager'),
      $container->get('node.grant_storage'),
      $container->get('messenger'),
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -23,9 +99,8 @@ class GrantsForm extends FormBase {
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Node $node = NULL) {
    $db = \Drupal::database();
    $form_values = $form_state->getValues();
    $settings = \Drupal::configFactory()->get('nodeaccess.settings');
    $settings = $this->configFactory->get('nodeaccess.settings');
    $nid = $node->id();
    $role_alias = $settings->get('role_alias');
    $role_map = $settings->get('role_map');
@@ -42,7 +117,7 @@ class GrantsForm extends FormBase {
      // Load all roles.
      foreach ($role_alias as $id => $role) {
        $rid = $role_map[$id];
        $query = $db->select('node_access', 'n')
        $query = $this->database->select('node_access', 'n')
          ->fields('n', ['grant_view', 'grant_update', 'grant_delete'])
          ->condition('n.gid', $rid, '=')
          ->condition('n.realm', 'nodeaccess_rid', '=')
@@ -68,7 +143,7 @@ class GrantsForm extends FormBase {
      }

      // Load users from node_access.
      $query = $db->select('node_access', 'n');
      $query = $this->database->select('node_access', 'n');
      $query->join('users_field_data', 'ufd', 'ufd.uid = n.gid');
      $query->fields('n', ['grant_view', 'grant_update', 'grant_delete', 'nid']);
      $query->fields('ufd', ['name', 'uid']);
@@ -90,7 +165,7 @@ class GrantsForm extends FormBase {
      // Perform search.
      if ($form_values['keys']) {
        $uids = [];
        $query = $db->select('users_field_data', 'ufd');
        $query = $this->database->select('users_field_data', 'ufd');
        $query->fields('ufd', ['uid', 'name']);
        if (isset($form_values['uid']) && is_array($form_values['uid'])) {
          $uids = array_keys($form_values['uid']);
@@ -109,22 +184,22 @@ class GrantsForm extends FormBase {
      }
      // Calculate default grants for found users.
      if (isset($form_values['uid']) && is_array($form_values['uid'])) {
        // set the cast type depending on which database engine is being used.
        if (strstr($db->version(), 'MariaDB') !== FALSE) {
        // Set the cast type depending on which database engine is being used.
        if (strstr($this->database->version(), 'MariaDB') !== FALSE) {
          $cast_type = 'int';
        }
        elseif (strstr($db->clientVersion(), 'PostgreSQL') !== FALSE) {
        elseif (strstr($this->database->clientVersion(), 'PostgreSQL') !== FALSE) {
          $cast_type = 'integer';
        }
        else {
          // assume it's MySQL.
          // Assume it's MySQL.
          $cast_type = 'unsigned';
        }
        foreach (array_keys($form_values['uid']) as $uid) {
          if (!$form_values['uid'][$uid]['keep']) {
            foreach (['grant_view', 'grant_update', 'grant_delete'] as $grant_type) {

              $query = $db->select('node_access', 'na');
              $query = $this->database->select('node_access', 'na');
              $query->join('user__roles', 'r', '(na.gid = CAST(r.roles_target_id as ' . $cast_type . '))');
              $query->condition('na.nid', $nid, '=');
              $query->condition('na.realm', 'nodeaccess_rid', '=');
@@ -135,7 +210,7 @@ class GrantsForm extends FormBase {
              $results = $query->execute();
              $count1 = $results->fetchField();

              $query = $db->select('node_access', 'na');
              $query = $this->database->select('node_access', 'na');
              $query->condition('na.nid', $nid, '=');
              $query->condition('na.realm', 'nodeaccess_uid', '=');
              $query->condition('na.gid', $uid, '=');
@@ -153,8 +228,8 @@ class GrantsForm extends FormBase {
      }
    }

    $form_values['rid'] = isset($form_values['rid']) ? $form_values['rid'] : [];
    $form_values['uid'] = isset($form_values['uid']) ? $form_values['uid'] : [];
    $form_values['rid'] = isset($form_values['rid']) ?? [];
    $form_values['uid'] = isset($form_values['uid']) ?? [];
    $roles = $form_values['rid'];
    $users = $form_values['uid'];
    $form['nid'] = [
@@ -213,7 +288,7 @@ class GrantsForm extends FormBase {
    if ($user->hasPermission('access user profiles')) {
      $form['keys'] = [
        '#type' => 'entity_autocomplete',
        '#default_value' => isset($form_values['keys']) ? $form_values['keys'] : '',
        '#default_value' => isset($form_values['keys']) ?? '',
        '#size' => 40,
        '#target_type' => 'user',
        '#title' => $this->t('Enter names to search for users'),
@@ -222,7 +297,7 @@ class GrantsForm extends FormBase {
    else {
      $form['keys'] = [
        '#type' => 'textfield',
        '#default_value' => isset($form_values['keys']) ? $form_values['keys'] : '',
        '#default_value' => isset($form_values['keys']) ?? '',
        '#size' => 40,
      ];
    }
@@ -306,12 +381,11 @@ class GrantsForm extends FormBase {
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $db = \Drupal::database();
    // Update configuration.
    $values = $form_state->getValues();
    $nid = $values['nid'];
    $grants = [];
    $node = Node::load($nid);
    $node = $this->entityTypeManager->getStorage('node')->load($nid);

    foreach (['uid', 'rid'] as $type) {
      $realm = 'nodeaccess_' . $type;
@@ -331,11 +405,11 @@ class GrantsForm extends FormBase {
      }
    }
    // Save role and user grants to our own table.
    $db->delete('nodeaccess')
    $this->database->delete('nodeaccess')
      ->condition('nid', $nid)
      ->execute();
    foreach ($grants as $grant) {
      $id = $db->insert('nodeaccess')
      $this->database->insert('nodeaccess')
        ->fields([
          'nid' => $nid,
          'gid' => $grant['gid'],
@@ -346,9 +420,9 @@ class GrantsForm extends FormBase {
        ])
        ->execute();
    }
    \Drupal::entityTypeManager()->getAccessControlHandler('node')->acquireGrants($node);
    \Drupal::service('node.grant_storage')->write($node, $grants);
    \Drupal::messenger()->addMessage($this->t('Grants saved.'));
    $this->entityTypeManager->getAccessControlHandler('node')->acquireGrants($node);
    $this->nodeGrantStorage->write($node, $grants);
    $this->messenger->addMessage($this->t('Grants saved.'));

    $tags = ['node:' . $node->id()];
    Cache::invalidateTags($tags);