Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / permissions_by_term / src / Service / AccessStorage.php
index 2342e898837846e100e24e21878f80d75175c55f..257ce76a090ff7cbc3018189082fef575b44d56a 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace Drupal\permissions_by_term\Service;
 
-use Drupal\Core\Database\Connection;
 use Drupal\Component\Utility\Tags;
+use Drupal\Core\Database\Connection;
 use Drupal\Core\Form\FormState;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\user\Entity\User;
@@ -275,29 +275,47 @@ class AccessStorage {
   }
 
   /**
-   * @param array $aUserIdsGrantedAccess
-   * @param int   $term_id
+   * Delete access storage when a term is removed.
+   *
+   * @param int $term_id
+   *   The term ID being deleted.
+   */
+  public function deleteAllTermPermissionsByTid($term_id) {
+    $this->database->delete('permissions_by_term_user')
+      ->condition('tid', $term_id, '=')
+      ->execute();
+
+    $this->database->delete('permissions_by_term_role')
+      ->condition('tid', $term_id, '=')
+      ->execute();
+  }
+
+  /**
+   * @param array  $aUserIdsGrantedAccess
+   * @param int    $term_id
+   * @param string $langcode
    *
    * @throws \Exception
    */
-  public function addTermPermissionsByUserIds($aUserIdsGrantedAccess, $term_id) {
+  public function addTermPermissionsByUserIds($aUserIdsGrantedAccess, $term_id, $langcode = 'en') {
     foreach ($aUserIdsGrantedAccess as $iUserIdGrantedAccess) {
       $this->database->insert('permissions_by_term_user')
-        ->fields(['tid', 'uid'], [$term_id, $iUserIdGrantedAccess])
+        ->fields(['tid', 'uid', 'langcode'], [$term_id, $iUserIdGrantedAccess, $langcode])
         ->execute();
     }
   }
 
   /**
-   * @param array $aRoleIdsGrantedAccess
-   * @param int   $term_id
+   * @param array  $aRoleIdsGrantedAccess
+   * @param int    $term_id
+   * @param string $langcode
    *
    * @throws \Exception
    */
-  public function addTermPermissionsByRoleIds($aRoleIdsGrantedAccess, $term_id) {
+  public function addTermPermissionsByRoleIds($aRoleIdsGrantedAccess, $term_id, $langcode = 'en') {
     foreach ($aRoleIdsGrantedAccess as $sRoleIdGrantedAccess) {
       $this->database->insert('permissions_by_term_role')
-        ->fields(['tid', 'rid'], [$term_id, $sRoleIdGrantedAccess])
+        ->fields(['tid', 'rid', 'langcode'], [$term_id, $sRoleIdGrantedAccess, $langcode])
         ->execute();
     }
   }
@@ -320,27 +338,13 @@ class AccessStorage {
       return [];
     }
 
-    $aRawUsers = explode('),', $sRawUsers);
+    $aRawUsers = Tags::explode($sRawUsers);
+
     $aUserIds = [];
     if (!empty($aRawUsers)) {
       foreach ($aRawUsers as $sRawUser) {
-        $aTempRawUser = explode(' (', $sRawUser);
-        // We check the user id by user name. If we get null back, the user might
-        // be the Anonymous user. In that case we get null back and then we use
-        // this id, which is 0.
-        if (!empty($aTempRawUser[1])) {
-          $fallback_user_id = str_replace(')', '', $aTempRawUser[1]);
-          $fallback_user_id = intval($fallback_user_id);
-        }
-
-        $sRawUser = trim($aTempRawUser['0']);
-        $uid = $this->getUserIdByName($sRawUser)['uid'];
-        if ($uid == NULL && $fallback_user_id == 0) {
-          // We might want to give access to the Anonymous user.
-          $aUserIds[] = 0;
-        }
-        else {
-          $aUserIds[] = $this->getUserIdByName($sRawUser)['uid'];
+        if (preg_match("/.+\s\(([^\)]+)\)/", $sRawUser, $matches)) {
+          $aUserIds[] = $matches[1];
         }
       }
     }
@@ -370,12 +374,17 @@ class AccessStorage {
       $aSubmittedUserIdsGrantedAccess, $aExistingRoleIdsGrantedAccess,
       $aSubmittedRolesGrantedAccess);
 
+    $langcode = 'en';
+    if (!empty($form_state->getValue('langcode', 'en'))) {
+      $langcode = $form_state->getValue('langcode', 'en')['0']['value'];
+    }
+
     // Run the database queries.
     $this->deleteTermPermissionsByUserIds($aRet['UserIdPermissionsToRemove'], $term_id);
-    $this->addTermPermissionsByUserIds($aRet['UserIdPermissionsToAdd'], $term_id);
+    $this->addTermPermissionsByUserIds($aRet['UserIdPermissionsToAdd'], $term_id, $langcode);
 
     $this->deleteTermPermissionsByRoleIds($aRet['UserRolePermissionsToRemove'], $term_id);
-    $this->addTermPermissionsByRoleIds($aRet['aRoleIdPermissionsToAdd'], $term_id);
+    $this->addTermPermissionsByRoleIds($aRet['aRoleIdPermissionsToAdd'], $term_id, $langcode);
 
     return $aRet;
   }
@@ -508,18 +517,6 @@ class AccessStorage {
     return $sUserInfos;
   }
 
-  /**
-   * @return array
-   */
-  public function getAllNids()
-  {
-    $query = $this->database->select('node', 'n')
-        ->fields('n', ['nid']);
-
-    return $query->execute()
-        ->fetchCol();
-  }
-
   /**
    * @param $nid
    *
@@ -614,13 +611,13 @@ class AccessStorage {
 
   private function computePermittedTids(AccountInterface $user)
   {
-    $nidsWithNoTidRestriction = $this->getNidsWithNoTidRestriction();
+    $nidsWithNoTidRestriction = $this->getUnrestrictedNids();
     $nidsByTids = $this->term->getNidsByTids($this->getPermittedTids($user->id(), $user->getRoles()));
 
     if (\Drupal::config('permissions_by_term.settings.single_term_restriction')->get('value')) {
       $permittedNids = [];
       foreach ($nidsByTids as $nid) {
-        if($this->accessCheck->canUserAccessByNodeId($nid)) {
+        if($this->accessCheck->canUserAccessByNodeId($nid, $user->id(), $this->getLangCode($nid))) {
           $permittedNids[] = $nid;
         }
       }
@@ -629,7 +626,7 @@ class AccessStorage {
 
     if (!empty($nidsByTids)) {
       return array_merge(
-        $this->getNidsWithNoTidRestriction(),
+        $this->getUnrestrictedNids(),
         $nidsByTids
       );
     }
@@ -637,21 +634,50 @@ class AccessStorage {
     return $nidsWithNoTidRestriction;
   }
 
-  private function getNidsWithNoTidRestriction() {
-    $queryNidsNoRestriction = $this->database->select('node', 'n')
-      ->fields('n', ['nid']);
+  private function getUnrestrictedNids() {
+    $tidsRestrictedUserQuery = $this->database->select('permissions_by_term_user', 'u')
+      ->fields('u', ['tid']);
+
+    $restrictedTids = $this->database->select('permissions_by_term_role', 'r')
+      ->fields('r', ['tid'])
+      ->union($tidsRestrictedUserQuery)
+      ->execute()
+      ->fetchCol();
 
-    $queryNidsNoRestriction->leftJoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
-    $queryNidsNoRestriction->leftJoin('permissions_by_term_user', 'ptu', 'ptu.tid = ti.tid');
-    $queryNidsNoRestriction->condition('ptu.tid', NULL, 'IS');
-    $queryNidsNoRestriction->leftJoin('permissions_by_term_role', 'ptr', 'ptr.tid = ti.tid');
-    $queryNidsNoRestriction->condition('ptr.tid', NULL, 'IS');
+    if (empty($restrictedTids)) {
+      return $this->getAllNids();
+    }
 
-     return $queryNidsNoRestriction
+    $restrictedNids = $this->database->select('taxonomy_index', 't')
+      ->fields('t', ['nid'])
+      ->condition('t.tid', $restrictedTids, 'IN')
+      ->distinct(TRUE)
       ->execute()
       ->fetchCol();
+
+    if (empty($restrictedNids)) {
+      return $this->getAllNids();
+    }
+
+    $unrestrictedNids = $this->database->select('taxonomy_index', 't')
+      ->fields('t', ['nid'])
+      ->condition('t.nid', $restrictedNids, 'NOT IN')
+      ->distinct(TRUE)
+      ->execute()
+      ->fetchCol();
+
+    return $unrestrictedNids;
   }
 
+  /**
+   * @return array
+   */
+  public function getAllNids() {
+    return $this->database->select('node', 'n')
+      ->fields('n', ['nid'])
+      ->execute()
+      ->fetchCol();
+  }
 
   /**
    * @param $uid