Merge pull request #11416 from colemanw/phpversion
[civicrm-core.git] / CRM / ACL / API.php
index 113bcce0e9250eb7871bc196f68d6b9756eb3953..3337c4ab9b897c8846d2bcd028a46d2200e72ee1 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 class CRM_ACL_API {
 
@@ -84,6 +84,10 @@ class CRM_ACL_API {
    * @param bool $skipDeleteClause
    *   Don't add delete clause if this is true,.
    *   this means it is handled by generating query
+   * @param bool $skipOwnContactClause
+   *   Do not add 'OR contact_id = $userID' to the where clause.
+   *   This is a hideously inefficient query and should be avoided
+   *   wherever possible.
    *
    * @return string
    *   the group where clause for this user
@@ -94,7 +98,8 @@ class CRM_ACL_API {
     &$whereTables,
     $contactID = NULL,
     $onlyDeleted = FALSE,
-    $skipDeleteClause = FALSE
+    $skipDeleteClause = FALSE,
+    $skipOwnContactClause = FALSE
   ) {
     // the default value which is valid for the final AND
     $deleteClause = ' ( 1 ) ';
@@ -131,9 +136,9 @@ class CRM_ACL_API {
       )
     );
 
-    // Add permission on self
-    if ($contactID && (CRM_Core_Permission::check('edit my contact') ||
-      $type == self::VIEW && CRM_Core_Permission::check('view my contact'))
+    // Add permission on self if we really hate our server or have hardly any contacts.
+    if (!$skipOwnContactClause && $contactID && (CRM_Core_Permission::check('edit my contact') ||
+        $type == self::VIEW && CRM_Core_Permission::check('view my contact'))
     ) {
       $where = "(contact_a.id = $contactID OR ($where))";
     }
@@ -185,10 +190,8 @@ class CRM_ACL_API {
    * @param string $tableName
    * @param null $allGroups
    * @param null $includedGroups
-   * @param bool $flush
    *
-   * @return array
-   *   the ids of the groups for which the user has permissions
+   * @return bool
    */
   public static function groupPermission(
     $type,
@@ -196,39 +199,23 @@ class CRM_ACL_API {
     $contactID = NULL,
     $tableName = 'civicrm_saved_search',
     $allGroups = NULL,
-    $includedGroups = NULL,
-    $flush = FALSE
+    $includedGroups = NULL
   ) {
 
-    static $cache = array();
-    $groups = array();
-    //@todo this is pretty hacky!!!
-    //adding a way for unit tests to flush the cache
-    if ($flush) {
-      $cache = array();
-      return NULL;
+    if (!isset(Civi::$statics[__CLASS__]) || !isset(Civi::$statics[__CLASS__]['group_permission'])) {
+      Civi::$statics[__CLASS__]['group_permission'] = array();
     }
+
     if (!$contactID) {
-      $session = CRM_Core_Session::singleton();
-      $contactID = NULL;
-      if ($session->get('userID')) {
-        $contactID = $session->get('userID');
-      }
+      $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
     }
 
     $key = "{$tableName}_{$type}_{$contactID}";
-    if (array_key_exists($key, $cache)) {
-      $groups = &$cache[$key];
-    }
-    else {
-      $groups = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
-      $cache[$key] = $groups;
-    }
-    if (empty($groups)) {
-      return FALSE;
+    if (!array_key_exists($key, Civi::$statics[__CLASS__]['group_permission'])) {
+      Civi::$statics[__CLASS__]['group_permission'][$key] = self::group($type, $contactID, $tableName, $allGroups, $includedGroups);
     }
 
-    return in_array($groupID, $groups) ? TRUE : FALSE;
+    return in_array($groupID, Civi::$statics[__CLASS__]['group_permission'][$key]);
   }
 
 }