Browse Source

Merge pull request #542 from statonlab/538-fix_menu_speed

Stop loading the entire entity to check user access
Stephen Ficklin 6 years ago
parent
commit
a4bccbde30
2 changed files with 50 additions and 9 deletions
  1. 16 3
      tripal/includes/TripalEntityUIController.inc
  2. 34 6
      tripal/includes/tripal.entity.inc

+ 16 - 3
tripal/includes/TripalEntityUIController.inc

@@ -60,7 +60,7 @@ class TripalEntityUIController extends EntityDefaultUIController {
     }
 
     // Link for viewing a tripal data type.
-    $items['bio_data/' . $wildcard] = array(
+    $items['bio_data/%'] = array(
       'title callback' => 'tripal_entity_title',
       'title arguments' => array(1),
       'page callback' => 'tripal_view_entity',
@@ -71,7 +71,7 @@ class TripalEntityUIController extends EntityDefaultUIController {
     );
 
     // 'View' tab for an individual entity page.
-    $items['bio_data/' . $wildcard . '/view'] = array(
+    $items['bio_data/%/view'] = array(
       'title' => 'View',
       'page callback' => 'tripal_view_entity',
       'page arguments' => array(1),
@@ -115,7 +115,20 @@ class TripalEntityUIController extends EntityDefaultUIController {
  * @see hook_entity_view_alter()
  */
 function tripal_view_entity($entity, $view_mode = 'full') {
-   $content = '';
+   if(!is_object($entity)) {
+     $id = intval($entity);
+     if($id === 0) {
+       return drupal_not_found();
+     }
+
+     $entities = tripal_load_entity('TripalEntity', [$id]);
+     if(empty($entities)) {
+       return drupal_not_found();
+     }
+
+     $entity = reset($entities);
+   }
+
    $controller = entity_get_controller($entity->type);
    $content = $controller->view(array($entity->id => $entity));
    drupal_set_title($entity->title);

+ 34 - 6
tripal/includes/tripal.entity.inc

@@ -432,20 +432,48 @@ function tripal_field_property_get($entity, array $options, $field_name, $entity
  */
 function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
   global $user;
+  $cache = &drupal_static(__FUNCTION__, NULL);
 
-  if ($entity) {
+  if (!isset($account)) {
+    $account = $user;
+  }
+
+  if (is_object($entity)) {
     $bundle_name = $entity->bundle;
   }
+  elseif (intval($entity) !== 0) {
+    if (!isset($cache)) {
+      $cache = cache_get("tripal_entity_access_cache");
+      if (isset($cache->data)) {
+        $cache = $cache->data;
+      }
+    }
+
+    if (empty($cache)) {
+      $cache = [];
+    }
+
+    if (isset($cache[$entity])) {
+      $bundle_name = $cache[$entity];
+    }
+    else {
+      $sql = 'SELECT {bundle} FROM tripal_entity WHERE id = :id';
+      $bundle_name = db_query($sql, [':id' => $entity])->fetchField();
+      $cache[$entity] = $bundle_name;
+      cache_set("tripal_entity_access_cache", $cache);
+    }
+  }
   else {
     return FALSE;
   }
 
-  if (!isset($account)) {
-    $account = $user;
-  }
-
   if (!$entity_type) {
-    $entity_type = $entity->type;
+    if (is_object($entity)) {
+      $entity_type = $entity->type;
+    }
+    else {
+      $entity_type = 'TripalEntity';
+    }
   }
 
   // See if other modules want to adust permissions.