Security update for permissions_by_term
[yaffs-website] / web / modules / contrib / permissions_by_term / src / Service / NodeAccess.php
similarity index 50%
rename from web/modules/contrib/permissions_by_term/src/NodeAccess.php
rename to web/modules/contrib/permissions_by_term/src/Service/NodeAccess.php
index 4015c65952b84475e6c7b81733cb5396fee25eba..42e0798c647f3ff1bdbedfc479c28f0e8119bb36 100644 (file)
@@ -1,17 +1,23 @@
 <?php
 
-namespace Drupal\permissions_by_term;
+namespace Drupal\permissions_by_term\Service;
 
-use Drupal\Core\Entity\EntityManager;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\permissions_by_term\Factory\NodeAccessRecordFactory;
-use Drupal\user\Entity\User;
 use Drupal\node\Entity\Node;
-use Drupal\Core\Database\Driver\mysql\Connection;
+use Drupal\Core\Database\Connection;
+use Drupal\permissions_by_term\Model\NodeAccessRecordModel;
+use Drupal\user\Entity\User;
 
+/**
+ * Class NodeAccess
+ *
+ * @package Drupal\permissions_by_term
+ */
 class NodeAccess {
 
   /**
-   * @var array $uniqueGid
+   * @var int $uniqueGid
    */
   private $uniqueGid = 0;
 
@@ -31,9 +37,9 @@ class NodeAccess {
   private $node;
 
   /**
-   * @var EntityManager $entityManager
+   * @var EntityTypeManagerInterface $entityTypeManager
    */
-  private $entityManager;
+  private $entityTypeManager;
 
   /**
    * @var AccessCheck $accessCheck
@@ -51,65 +57,72 @@ class NodeAccess {
   private $userInstance;
 
   /**
-   * @var Connection $database
+   * The database connection.
+   *
+   * @var Connection
    */
   private $database;
 
-  public function __construct(AccessStorage $accessStorage, NodeAccessRecordFactory $nodeAccessRecordFactory, EntityManager $entityManager, AccessCheck $accessCheck, Connection $database) {
+  /**
+   * NodeAccess constructor.
+   *
+   * @param AccessStorage           $accessStorage
+   * @param NodeAccessRecordFactory $nodeAccessRecordFactory
+   * @param EntityTypeManagerInterface           $entityTypeManager
+   * @param AccessCheck             $accessCheck
+   * @param Connection              $database
+   */
+  public function __construct(
+    AccessStorage $accessStorage,
+    NodeAccessRecordFactory $nodeAccessRecordFactory,
+    EntityTypeManagerInterface $entityTypeManager,
+    AccessCheck $accessCheck,
+    Connection $database
+  ) {
     $this->accessStorage = $accessStorage;
     $this->nodeAccessRecordFactory = $nodeAccessRecordFactory;
-    $this->entityManager = $entityManager;
-    $this->userEntityStorage = $this->entityManager->getStorage('user');
-    $this->node = $this->entityManager->getStorage('node');
+    $this->entityTypeManager = $entityTypeManager;
+    $this->userEntityStorage = $this->entityTypeManager->getStorage('user');
+    $this->node = $this->entityTypeManager->getStorage('node');
     $this->accessCheck = $accessCheck;
     $this->database = $database;
   }
 
-  public function createRealm($uid) {
-    return 'permissions_by_term__uid_' . $uid;
-  }
-
-  public function createGrants($nid, $uid = FALSE) {
-    if (empty($uid)) {
-      $uids = $this->accessStorage->getAllUids();
-    } else {
-      $uids[] = $uid;
-    }
-
-    $grants = [];
-    foreach ($uids as $uid) {
-      if ($this->accessCheck->canUserAccessByNodeId($nid, $uid)) {
-        $realm = $this->createRealm($uid);
-        $nodeType = $this->accessStorage->getNodeType($nid);
-        $langcode = $this->accessStorage->getLangCode($nid);
-        $grants[] = $this->nodeAccessRecordFactory->create($realm, $this->createUniqueGid(), $nid, $langcode, $this->getGrantUpdate($uid, $nodeType, $nid), $this->getGrantDelete($uid, $nodeType, $nid));
-      }
-    }
-
-    return $grants;
-  }
-
-  public function createUniqueGid() {
-    $uniqueGid = $this->getUniqueGid();
-    $uniqueGid++;
-    $this->setUniqueGid($uniqueGid);
-    return $this->getUniqueGid();
+  /**
+   * @return NodeAccessRecordModel
+   */
+  public function createGrant($nid, $gid) {
+    return $this->nodeAccessRecordFactory->create(
+      AccessStorage::NODE_ACCESS_REALM,
+      $gid,
+      $nid,
+      $this->accessStorage->getLangCode($nid),
+      0,
+      0
+    );
   }
 
   /**
-   * @return array
+   * @return int
    */
   public function getUniqueGid() {
     return $this->uniqueGid;
   }
 
   /**
-   * @param array $uniqueGid
+   * @param int $uniqueGid
    */
   public function setUniqueGid($uniqueGid) {
     $this->uniqueGid = $uniqueGid;
   }
 
+  /**
+   * @param $uid
+   * @param $nodeType
+   * @param $nid
+   *
+   * @return bool
+   */
   public function canUserUpdateNode($uid, $nodeType, $nid) {
     $user = $this->getUserInstance($uid);
 
@@ -135,6 +148,13 @@ class NodeAccess {
     return FALSE;
   }
 
+  /**
+   * @param $uid
+   * @param $nodeType
+   * @param $nid
+   *
+   * @return bool
+   */
   public function canUserDeleteNode($uid, $nodeType, $nid) {
     $user = $this->getUserInstance($uid);
     if ($user->hasPermission('delete any ' . $nodeType . ' content')) {
@@ -172,6 +192,12 @@ class NodeAccess {
     return 0;
   }
 
+  /**
+   * @param $nid
+   * @param $uid
+   *
+   * @return bool
+   */
   public function isNodeOwner($nid, $uid) {
     $node = $this->node->load($nid);
     if (intval($node->getOwnerId()) == intval($uid)) {
@@ -199,6 +225,11 @@ class NodeAccess {
     return 0;
   }
 
+  /**
+   * @param $nid
+   *
+   * @return array
+   */
   public function getGrantsByNid($nid) {
     $grants = [];
     foreach ($this->grants as $grant) {
@@ -244,73 +275,25 @@ class NodeAccess {
     $this->userInstance = $userInstance;
   }
 
-  public function rebuildByTid($tid, $formState) {
-    $nids = $this->accessStorage->getNidsByTid($tid);
-    if (!empty($nids)) {
-      $this->dropRecordsByNids($nids);
-    }
-
-    if (empty($this->accessStorage->getSubmittedUserIds()) && empty($this->accessStorage->getSubmittedRolesGrantedAccess($formState))) {
-      return;
-    }
-
-    foreach ($nids as $nid) {
-      $grants = $this->createGrants($nid);
-      foreach ($grants as $grant) {
-        $this->database->insert('node_access')
-          ->fields(
-            ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
-            [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]
-          )
-          ->execute();
-      }
-    }
-
-  }
-
-  private function dropRecordsByNids($nids) {
-    $this->database->delete('node_access')
-      ->condition('nid', $nids, 'IN')
-      ->execute();
-  }
+  /**
+   * @param int $nid
+   *
+   * @return bool
+   */
+  public function isAccessRecordExisting($nid) {
+    $query = $this->database->select('node_access', 'na')
+      ->fields('na', ['nid'])
+      ->condition('na.nid', $nid)
+      ->condition('na.realm', AccessStorage::NODE_ACCESS_REALM);
 
-  private function dropRecordsByUid($uid) {
-    $this->database->delete('node_access')
-      ->condition('realm', 'permissions_by_term__uid_' . $uid)
-      ->execute();
-  }
+    $result = $query->execute()
+      ->fetchCol();
 
-  public function rebuildByUid($uid, $noDrop = FALSE) {
-    if ($noDrop === FALSE) {
-      $this->dropRecordsByUid($uid);
+    if (empty($result)) {
+      return FALSE;
     }
 
-    $nids = $this->accessStorage->getAllNids();
-
-    foreach ($nids as $nid) {
-      $grants = $this->createGrants($nid, $uid);
-      foreach ($grants as $grant) {
-        $this->database->insert('node_access')
-          ->fields(
-            ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
-            [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]
-          )
-          ->execute();
-      }
-    }
-
-  }
-
-  public function rebuildByNid($nid) {
-    $grants = $this->createGrants($nid);
-    foreach ($grants as $grant) {
-      $this->database->insert('node_access')
-        ->fields(
-          ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
-          [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]
-        )
-        ->execute();
-    }
+    return TRUE;
   }
 
-}
\ No newline at end of file
+}