소스 검색

Merge pull request #1023 from reynoldtan/patch-3

Proposed Fix to Issue #1022 - A and B
Lacey-Anne Sanderson 5 년 전
부모
커밋
e755ce05d5
2개의 변경된 파일28개의 추가작업 그리고 3개의 파일을 삭제
  1. 16 3
      tests/tripal_ws/http/TripalWebServicesContentTest.php
  2. 12 0
      tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

+ 16 - 3
tests/tripal_ws/http/TripalWebServicesContentTest.php

@@ -12,6 +12,14 @@ class TripalWebServicesContentTest extends TripalTestCase {
 
   /** @test */
   public function testGettingMainContentList() {
+    // Grant user permission to all content.
+    $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
+    $bundles = db_query('SELECT name FROM tripal_bundle');
+    foreach($bundles as $bundle) {
+      $bundle_name = 'view ' . $bundle->name;
+      user_role_grant_permissions($role_id, array($bundle_name));
+    }
+
     $response = $this->get('web-services/content/v0.1');
 
     // Make sure it returned valid json
@@ -38,10 +46,15 @@ class TripalWebServicesContentTest extends TripalTestCase {
    */
   public function testGettingListOfEntitiesInABundle() {
     // Get bundle label
-    $label = db_query('SELECT label FROM tripal_bundle LIMIT 1')->fetchField();
+    $label = db_query('SELECT label, name FROM tripal_bundle LIMIT 1')->fetchObject();
+
+    // Grant user permission to this content.
+    $role_id = (user_is_anonymous()) ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID;
+    user_role_grant_permissions($role_id, array('view ' . $label->name));
 
     // Call /web-services/content/v0.1/[label]
-    $response = $this->get("web-services/content/v0.1/$label");
+    $ctype = preg_replace('/[^\w]/', '_', $label->label);
+    $response = $this->get("web-services/content/v0.1/" . $ctype);
 
     // Verify the returned JSON matches the structure
     $response->assertSuccessful();
@@ -56,7 +69,7 @@ class TripalWebServicesContentTest extends TripalTestCase {
 
     // Verify the collection is of the correct type
     $json = $response->json();
-    $this->assertEquals($json['label'], "$label Collection");
+    $this->assertEquals($json['label'], "$label->label Collection");
   }
 
   /**

+ 12 - 0
tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

@@ -879,6 +879,13 @@ class TripalContentService_v0_1 extends TripalWebService {
 
     // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
     $bundle = tripal_load_bundle_entity(['label' => $ctype]);
+    
+    // Check that the user has access to this bundle.  If not then the
+    // function call will throw an error.
+    if (!user_access('view ' . $bundle->name)) {
+      throw new Exception("Permission Denied.");
+    }
+        
     $term = entity_load('TripalTerm', ['id' => $bundle->term_id]);
     $term = reset($term);
 
@@ -1047,6 +1054,11 @@ class TripalContentService_v0_1 extends TripalWebService {
     // Iterate through the terms and add an entry in the collection.
     $i = 0;
     while ($bundle = $bundles->fetchObject()) {
+      if (!user_access('view ' . $bundle->name)) {
+        // Show only content types users have access to and skip the rest.
+        continue;
+      }
+      
       $entity = entity_load('TripalTerm', ['id' => $bundle->term_id]);
       $term = reset($entity);
       $vocab = $term->vocab;