Revert "0011172: optimize getGroupmemberships in Principalbackend"
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 14 Aug 2015 10:01:21 +0000 (12:01 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 14 Aug 2015 10:01:34 +0000 (12:01 +0200)
This reverts commit b9ce589cf497bce21a02e9881269c309ac42508c.

Change-Id: I14b8e5e86a2b0198a69f363db2a43ce7ebcf0bfb
Reviewed-on: http://gerrit.tine20.com/customers/2110
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Container.php
tine20/Tinebase/WebDav/PrincipalBackend.php

index 2726354..f6fa5f1 100644 (file)
@@ -653,10 +653,9 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
      * @param  String            $_accountId
      * @param  Array|String      $_grant
      * @param  String            $_aclTableName
-     * @param  bool              $_andGrants
      * @return void
      */
-    public static function addGrantsSql($_select, $_accountId, $_grant, $_aclTableName = 'container_acl', $_andGrants = FALSE)
+    public static function addGrantsSql($_select, $_accountId, $_grant, $_aclTableName = 'container_acl')
     {
         $accountId = $_accountId instanceof Tinebase_Record_Abstract
             ? $_accountId->getId()
@@ -666,7 +665,10 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
         
         $grants = is_array($_grant) ? $_grant : array($_grant);
         
-
+        // admin grant includes all other grants
+        if (! in_array(Tinebase_Model_Grants::GRANT_ADMIN, $grants)) {
+            $grants[] = Tinebase_Model_Grants::GRANT_ADMIN;
+        }
 
         $groupMemberships   = Tinebase_Group::getInstance()->getGroupMemberships($accountId);
         
@@ -692,18 +694,8 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
             
             $grantsSelect = new Tinebase_Backend_Sql_Filter_GroupSelect($_select);
             foreach ($grants as $grant) {
-                if ($_andGrants) {
-                    $grantsSelect->where("{$quotedGrant} LIKE ?", $grant);
-                } else {
-                    $grantsSelect->orWhere("{$quotedGrant} LIKE ?", $grant);
-                }
+                $grantsSelect->orWhere("{$quotedGrant} LIKE ?", $grant);
             }
-
-            // admin grant includes all other grants
-            if (! in_array(Tinebase_Model_Grants::GRANT_ADMIN, $grants)) {
-                $grantsSelect->orWhere("{$quotedGrant} LIKE ?", Tinebase_Model_Grants::GRANT_ADMIN);
-            }
-
             $grantsSelect->appendWhere(Zend_Db_Select::SQL_AND);
         }
     }
@@ -775,11 +767,10 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
      * @param   string|Tinebase_Model_Application   $recordClass
      * @param   array|string                        $_grant
      * @param   bool                                $_ignoreACL
-     * @param   bool                                $_andGrants
      * @return  Tinebase_Record_RecordSet set of Tinebase_Model_Container
      * @throws  Tinebase_Exception_NotFound
      */
-    public function getSharedContainer($_accountId, $recordClass, $_grant, $_ignoreACL = FALSE, $_andGrants = FALSE)
+    public function getSharedContainer($_accountId, $recordClass, $_grant, $_ignoreACL = FALSE)
     {
         // legacy handling
         $meta = $this->_resolveRecordClassArgument($recordClass);
@@ -791,8 +782,7 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
             $accountId .
             $application->getId() .
             implode('', (array)$grant) .
-            (int)$_ignoreACL .
-            (int)$_andGrants
+            (int)$_ignoreACL
         );
         
         try {
@@ -814,7 +804,7 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
             
             ->order('container.name');
         
-        $this->addGrantsSql($select, $accountId, $grant, 'container_acl', $_andGrants);
+        $this->addGrantsSql($select, $accountId, $grant);
         
         $stmt = $this->_db->query('/*' . __FUNCTION__ . '*/' . $select);
         
@@ -836,13 +826,12 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
      * @param   string|Tinebase_Model_Application   $recordClass
      * @param   array|string                        $_grant
      * @param   bool                                $_ignoreACL
-     * @param   bool                                $_andGrants
      * @return  Tinebase_Record_RecordSet set of Tinebase_Model_User
      */
-    public function getOtherUsers($_accountId, $recordClass, $_grant, $_ignoreACL = FALSE, $_andGrants = FALSE)
+    public function getOtherUsers($_accountId, $recordClass, $_grant, $_ignoreACL = FALSE)
     {
         $meta = $this->_resolveRecordClassArgument($recordClass);
-        $userIds = $this->_getOtherAccountIds($_accountId, $meta['appName'], $_grant, $_ignoreACL, $_andGrants);
+        $userIds = $this->_getOtherAccountIds($_accountId, $meta['appName'], $_grant, $_ignoreACL);
         
         $users = Tinebase_User::getInstance()->getMultiple($userIds);
         $users->sort('accountDisplayName');
@@ -857,16 +846,15 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
      * @param   string|Tinebase_Model_Application   $_application
      * @param   array|string                        $_grant
      * @param   bool                                $_ignoreACL
-     * @param   bool                                $_andGrants
      * @return  array of array of containerData
      */
-    protected function _getOtherAccountIds($_accountId, $_application, $_grant, $_ignoreACL = FALSE, $_andGrants = FALSE)
+    protected function _getOtherAccountIds($_accountId, $_application, $_grant, $_ignoreACL = FALSE)
     {
         $accountId   = Tinebase_Model_User::convertUserIdToInt($_accountId);
         $application = Tinebase_Application::getInstance()->getApplicationByName($_application);
         $grant       = $_ignoreACL ? '*' : $_grant;
 
-        $classCacheId = Tinebase_Helper::convertCacheId($accountId . $application->getId() . implode('', (array)$grant) . (int)$_ignoreACL . (int)$_andGrants);
+        $classCacheId = Tinebase_Helper::convertCacheId($accountId . $application->getId() . implode('', (array)$grant) .(int)$_ignoreACL);
         try {
             return $this->loadFromClassCache(__FUNCTION__, $classCacheId);
         } catch (Tinebase_Exception_NotFound $tenf) {
@@ -885,9 +873,9 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
             ->where("{$this->_db->quoteIdentifier('container.application_id')} = ?", $application->getId())
             ->where("{$this->_db->quoteIdentifier('container.type')} = ?", Tinebase_Model_Container::TYPE_PERSONAL)
             ->where("{$this->_db->quoteIdentifier('container.is_deleted')} = ?", 0, Zend_Db::INT_TYPE);
-
-        $this->addGrantsSql($select, $accountId, $grant, 'container_acl', $_andGrants);
-
+            
+        $this->addGrantsSql($select, $accountId, $grant);
+        
         $stmt = $this->_db->query('/*' . __FUNCTION__ . '*/' . $select);
         $containerIds = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
         
index 08a259e..a4cdb01 100644 (file)
@@ -361,19 +361,32 @@ class Tinebase_WebDav_PrincipalBackend implements \Sabre\DAVACL\PrincipalBackend
                     }
                     
                     if (Tinebase_Core::getUser()->hasRight('Calendar', Tinebase_Acl_Rights::RUN)) {
-                        // return users only, if they have the sync AND read grant set
-                        $otherUsers = Tinebase_Container::getInstance()->getOtherUsers($user, 'Calendar', array(Tinebase_Model_Grants::GRANT_SYNC, Tinebase_Model_Grants::GRANT_READ), false, true);
-                        foreach ($otherUsers as $u) {
-                            if ($u->contact_id && $u->visibility == Tinebase_Model_User::VISIBILITY_DISPLAYED) {
-                                $result[] = self::PREFIX_USERS . '/' . $u->contact_id . '/calendar-proxy-write';
+                        // return user only, if the containers have the sync AND read grant set
+                        $otherUsersSync = Tinebase_Container::getInstance()->getOtherUsers($user, 'Calendar', Tinebase_Model_Grants::GRANT_SYNC);
+                        
+                        if ($otherUsersSync->count() > 0) {
+                            $otherUsersRead = Tinebase_Container::getInstance()->getOtherUsers($user, 'Calendar', Tinebase_Model_Grants::GRANT_READ);
+                            
+                            $otherUsersIds = array_intersect($otherUsersSync->getArrayOfIds(), $otherUsersRead->getArrayOfIds());
+                            
+                            foreach ($otherUsersIds as $userId) {
+                                if ($otherUsersSync->getById($userId)->contact_id && $otherUsersSync->getById($userId)->visibility == Tinebase_Model_User::VISIBILITY_DISPLAYED) {
+                                    $result[] = self::PREFIX_USERS . '/' . $otherUsersSync->getById($userId)->contact_id . '/calendar-proxy-write';
+                                }
                             }
                         }
                         
-                        // return containers only, if the user has the sync AND read grant set
-                        $sharedContainers = Tinebase_Container::getInstance()->getSharedContainer($user, 'Calendar', array(Tinebase_Model_Grants::GRANT_SYNC, Tinebase_Model_Grants::GRANT_READ), false, true);
+                        // return user only, if the containers have the sync AND read grant set
+                        $sharedContainersSync = Tinebase_Container::getInstance()->getSharedContainer($user, 'Calendar', Tinebase_Model_Grants::GRANT_SYNC);
                         
-                        if ($sharedContainers->count() > 0) {
-                            $result[] = self::PREFIX_USERS . '/' . self::SHARED . '/calendar-proxy-write';
+                        if ($sharedContainersSync->count() > 0) {
+                            $sharedContainersRead = Tinebase_Container::getInstance()->getSharedContainer($user, 'Calendar', Tinebase_Model_Grants::GRANT_READ);
+                            
+                            $sharedContainerIds = array_intersect($sharedContainersSync->getArrayOfIds(), $sharedContainersRead->getArrayOfIds());
+                            
+                            if (count($sharedContainerIds) > 0) {
+                                $result[] = self::PREFIX_USERS . '/' . self::SHARED . '/calendar-proxy-write';
+                            }
                         }
                     }
                     Tinebase_Cache_PerRequest::getInstance()->save(__CLASS__, __FUNCTION__, $classCacheId, $result);