Admin - searchQuotaNodes
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 11 Jul 2017 16:31:00 +0000 (18:31 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Wed, 12 Jul 2017 10:42:00 +0000 (12:42 +0200)
added admin_acl_right view_quota_usage
Tinebase_Model_Tree_Node_PathFiler - allow search for '/'
requires admin view_quota_usage right, searches for parent_id is null

Change-Id: I8da6298549bb90c4f05a49b310320b2095621e19
Reviewed-on: http://gerrit.tine20.com/customers/5106
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tests/tine20/Admin/JsonTest.php
tine20/Admin/Acl/Rights.php
tine20/Admin/Frontend/Json.php
tine20/Tinebase/Model/Tree/Node/PathFilter.php

index b6073a9..9180d40 100644 (file)
@@ -1285,4 +1285,29 @@ class Admin_JsonTest extends TestCase
         $result = Admin_Controller_User::getInstance()->setAccountStatus($userArray['accountId'], Tinebase_Model_User::ACCOUNT_STATUS_EXPIRED);
         $this->assertEquals(1, $result);
     }
         $result = Admin_Controller_User::getInstance()->setAccountStatus($userArray['accountId'], Tinebase_Model_User::ACCOUNT_STATUS_EXPIRED);
         $this->assertEquals(1, $result);
     }
+
+    public function testSearchQuotaNodes()
+    {
+        $filterNullResult = $this->_json->searchQuotaNodes();
+        $filterRootResult = $this->_json->searchQuotaNodes(array(array(
+            'field'     => 'path',
+            'operator'  => 'equals',
+            'value'     => '/'
+        )));
+
+        static::assertEquals($filterNullResult['totalcount'], $filterRootResult['totalcount']);
+        static::assertGreaterThan(0, $filterNullResult['totalcount']);
+        foreach ($filterNullResult['results'] as $node) {
+            Tinebase_Application::getInstance()->getApplicationById($node['name']);
+        }
+
+        $filterAppResult = $this->_json->searchQuotaNodes(array(array(
+            'field'     => 'path',
+            'operator'  => 'equals',
+            'value'     => '/' . $filterNullResult['results'][0]['name']
+        )));
+
+        static::assertEquals(1, $filterAppResult['totalcount']);
+        static::assertEquals('folders', $filterAppResult['results'][0]['name']);
+    }
 }
 }
index c5b5c6c..566a81e 100644 (file)
@@ -5,7 +5,7 @@
  * @package     Admin
  * @subpackage  Acl
  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
  * @package     Admin
  * @subpackage  Acl
  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
- * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2007-2017 Metaways Infosystems GmbH (http://www.metaways.de)
  * @author      Philipp Schüle <p.schuele@metaways.de>
  * 
  */
  * @author      Philipp Schüle <p.schuele@metaways.de>
  * 
  */
@@ -73,7 +73,7 @@ class Admin_Acl_Rights extends Tinebase_Acl_Rights_Abstract
      * @staticvar string
      */
     const MANAGE_CUSTOMFIELDS = 'manage_customfields';
      * @staticvar string
      */
     const MANAGE_CUSTOMFIELDS = 'manage_customfields';
-    
+
     /**
      * the right to view roles
      * @staticvar string
     /**
      * the right to view roles
      * @staticvar string
@@ -123,6 +123,12 @@ class Admin_Acl_Rights extends Tinebase_Acl_Rights_Abstract
      * @staticvar string
      */
     const VIEW_SERVERINFO = 'view_serverinfo';
      * @staticvar string
      */
     const VIEW_SERVERINFO = 'view_serverinfo';
+
+    /**
+     * the right to view quota usage
+     * @staticvar string
+     */
+    const VIEW_QUOTA_USAGE = 'view_quota_usage';
         
     /**
      * holds the instance of the singleton
         
     /**
      * holds the instance of the singleton
@@ -188,6 +194,7 @@ class Admin_Acl_Rights extends Tinebase_Acl_Rights_Abstract
             self::VIEW_CONTAINERS,
             self::VIEW_CUSTOMFIELDS,
             self::VIEW_SERVERINFO,
             self::VIEW_CONTAINERS,
             self::VIEW_CUSTOMFIELDS,
             self::VIEW_SERVERINFO,
+            self::VIEW_QUOTA_USAGE,
         );
         $allRights = array_merge($allRights, $addRights);
         
         );
         $allRights = array_merge($allRights, $addRights);
         
@@ -268,6 +275,10 @@ class Admin_Acl_Rights extends Tinebase_Acl_Rights_Abstract
                 'text'          => $translate->_('view serverinfo'),
                 'description'   => $translate->_('view serverinfo list'),
             ),
                 'text'          => $translate->_('view serverinfo'),
                 'description'   => $translate->_('view serverinfo list'),
             ),
+            self::VIEW_QUOTA_USAGE => array(
+                'text'          => $translate->_('view quota usage'),
+                'description'   => $translate->_('view quota usage'),
+            ),
         );
         
         $rightDescriptions = array_merge($rightDescriptions, parent::getTranslatedRightDescriptions());
         );
         
         $rightDescriptions = array_merge($rightDescriptions, parent::getTranslatedRightDescriptions());
index 121cb8a..3c7edee 100644 (file)
@@ -1320,7 +1320,70 @@ class Admin_Frontend_Json extends Tinebase_Frontend_Json_Abstract
             'html' => $phpinfo
         );
     }
             'html' => $phpinfo
         );
     }
-    
+
+    public function searchQuotaNodes($filter = null)
+    {
+        if (! Tinebase_Core::getUser()->hasRight('Admin', Admin_Acl_Rights::VIEW_QUOTA_USAGE)) {
+            return FALSE;
+        }
+
+        $path = '';
+        if (null !== $filter) {
+            array_walk($filter, function ($val) use (&$path) {
+                if ('path' === $val['field']) {
+                    $path = $val['value'];
+                }
+            });
+        }
+        $filter = $this->_decodeFilter($filter, 'Tinebase_Model_Tree_Node_Filter');
+        // ATTENTION sadly the pathfilter to Array does path magic, returns the flatpath and not the statpath
+        // etc. this is Filemanager path magic. We don't want that here!
+        $filterArray = $filter->toArray();
+        array_walk($filterArray, function (&$val) use($path) {
+            if('path' === $val['field']) {
+                $val['value'] = $path;
+            }
+        });
+        $filter = new Tinebase_Model_Tree_Node_Filter($filterArray, '', array('ignoreAcl' => true));
+
+        $pathFilters = $filter->getFilter('path', TRUE);
+        if (count($pathFilters) !== 1) {
+            if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
+                . 'Exactly one path filter required.');
+            $pathFilter = $filter->createFilter(array(
+                    'field'     => 'path',
+                    'operator'  => 'equals',
+                    'value'     => '/',)
+            );
+            $filter->removeFilter('path');
+            $filter->addFilter($pathFilter);
+            $path = '/';
+        }
+
+        $filter->removeFilter('type');
+        $filter->addFilter($filter->createFilter(array(
+                    'field'     => 'type',
+                    'operator'  => 'equals',
+                    'value'     => Tinebase_Model_Tree_FileObject::TYPE_FOLDER,
+                )));
+
+        $records = Tinebase_FileSystem::getInstance()->search($filter);
+
+        $result = $this->_multipleRecordsToJson($records, $filter);
+
+        $filterArray = $filter->toArray();
+        array_walk($filterArray, function (&$val) use($path) {
+            if('path' === $val['field']) {
+                $val['value'] = $path;
+            }
+        });
+        return array(
+            'results'       => array_values($result),
+            'totalcount'    => count($result),
+            'filter'        => $filterArray
+        );
+    }
+
     /****************************** common ******************************/
     
     /**
     /****************************** common ******************************/
     
     /**
index fd61992..63216c3 100644 (file)
@@ -55,20 +55,20 @@ class Tinebase_Model_Tree_Node_PathFilter extends Tinebase_Model_Filter_Text
     {
         $result = parent::toArray($_valueToJson);
         
     {
         $result = parent::toArray($_valueToJson);
         
-        if (! $this->_path) {
+        if (! $this->_path && '/' !== $this->_value) {
             $this->_path = Tinebase_Model_Tree_Node_Path::createFromPath($this->_value);
         }
         
             $this->_path = Tinebase_Model_Tree_Node_Path::createFromPath($this->_value);
         }
         
-        if ($this->_path->containerType === Tinebase_Model_Tree_Node_Path::TYPE_ROOT) {
+        if ('/' === $this->_value || $this->_path->containerType === Tinebase_Model_Tree_Node_Path::TYPE_ROOT) {
             $node = new Tinebase_Model_Tree_Node(array(
                 'name' => 'root',
                 'path' => '/',
             $node = new Tinebase_Model_Tree_Node(array(
                 'name' => 'root',
                 'path' => '/',
-            ), TRUE);
+            ), true);
         } else {
             $node = Tinebase_FileSystem::getInstance()->stat($this->_path->statpath);
             $node->path = $this->_path->flatpath;
         }
         } else {
             $node = Tinebase_FileSystem::getInstance()->stat($this->_path->statpath);
             $node->path = $this->_path->flatpath;
         }
-        
+
         $result['value'] = $node->toArray();
         
         return $result;
         $result['value'] = $node->toArray();
         
         return $result;
@@ -92,6 +92,13 @@ class Tinebase_Model_Tree_Node_PathFilter extends Tinebase_Model_Filter_Text
      */
     protected function _parsePath()
     {
      */
     protected function _parsePath()
     {
+        if ('/' === $this->_value) {
+            if (! Tinebase_Core::getUser()->hasRight('Admin', Admin_Acl_Rights::VIEW_QUOTA_USAGE)) {
+                throw new Tinebase_Exception_AccessDenied('You don\'t have the right to run this application');
+            }
+            return;
+        }
+
         $this->_path = Tinebase_Model_Tree_Node_Path::createFromPath($this->_value);
         
         if (! $this->_options['ignoreAcl'] && ! Tinebase_Core::getUser()->hasRight($this->_path->application->name, Tinebase_Acl_Rights_Abstract::RUN)) {
         $this->_path = Tinebase_Model_Tree_Node_Path::createFromPath($this->_value);
         
         if (! $this->_options['ignoreAcl'] && ! Tinebase_Core::getUser()->hasRight($this->_path->application->name, Tinebase_Acl_Rights_Abstract::RUN)) {
@@ -107,9 +114,12 @@ class Tinebase_Model_Tree_Node_PathFilter extends Tinebase_Model_Filter_Text
      */
     protected function _addParentIdFilter($_select, $_backend)
     {
      */
     protected function _addParentIdFilter($_select, $_backend)
     {
-        $node = Tinebase_FileSystem::getInstance()->stat($this->_path->statpath);
-
-        $parentIdFilter = new Tinebase_Model_Filter_Text('parent_id', 'equals', $node->getId());
+        if ('/' === $this->_value) {
+            $parentIdFilter = new Tinebase_Model_Filter_Text('parent_id', 'isnull', '');
+        } else {
+            $node = Tinebase_FileSystem::getInstance()->stat($this->_path->statpath);
+            $parentIdFilter = new Tinebase_Model_Filter_Text('parent_id', 'equals', $node->getId());
+        }
         $parentIdFilter->appendFilterSql($_select, $_backend);
     }
 }
         $parentIdFilter->appendFilterSql($_select, $_backend);
     }
 }