]> source.dussan.org Git - nextcloud-server.git/commitdiff
Quota storage wrapper is now used for all users in sharing mode
authorVincent Petry <pvince81@owncloud.com>
Wed, 6 Nov 2013 10:57:04 +0000 (11:57 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 21 Nov 2013 11:04:54 +0000 (12:04 +0100)
When accessing a shared folder, the folder's owner appears as mountpoint
but wasn't wrapped by a quota storage wrapper.

This fix makes sure that all home storages are wrapped by a quota
storage wrapper, if applicable, to make sure quotas are respected when
uploading into shared folders.

apps/files/tests/ajax_rename.php
lib/private/util.php
tests/lib/util.php

index e654255c40771c10575421279496b6ae0882c220..3735b0a49c8c9f83a45f764eb6f2d2971fd9febf 100644 (file)
  */
 
 class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
+       private static $user;
 
        function setUp() {
                // mock OC_L10n
+               if (!self::$user) {
+                       self::$user = uniqid();
+               }
+               \OC_User::createUser(self::$user, 'password');
+               \OC_User::setUserId(self::$user);
+
+               \OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files');
+
                $l10nMock = $this->getMock('\OC_L10N', array('t'), array(), '', false);
                $l10nMock->expects($this->any())
                        ->method('t')
@@ -39,6 +48,12 @@ class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
                $this->files = new \OCA\Files\App($viewMock, $l10nMock);
        }
 
+       function tearDown() {
+               $result = \OC_User::deleteUser(self::$user);
+               $this->assertTrue($result);
+               \OC\Files\Filesystem::tearDown();
+       }
+
        /**
         * @brief test rename of file/folder named "Shared"
         */
index 176eb4bc36955cc8ec7b6be732eca3435e314d60..88f1f197b5d8ba6ff009ee612342cd5138c46117 100755 (executable)
@@ -53,16 +53,31 @@ class OC_Util {
 
                //if we aren't logged in, there is no use to set up the filesystem
                if( $user != "" ) {
-                       $quota = self::getUserQuota($user);
-                       if ($quota !== \OC\Files\SPACE_UNLIMITED) {
-                               \OC\Files\Filesystem::addStorageWrapper(function($mountPoint, $storage) use ($quota, $user) {
-                                       if ($mountPoint === '/' . $user . '/'){
-                                               return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota));
-                                       } else {
-                                               return $storage;
+                       \OC\Files\Filesystem::addStorageWrapper(function($mountPoint, $storage){
+                               // set up quota for home storages, even for other users
+                               // which can happen when using sharing
+
+                               if (strlen($mountPoint) > 1) {
+                                       // the user name will be extracted from the mountpoint
+                                       // with the format '/username/' (no suffix)
+                                       $user = null;
+                                       // find second separator
+                                       $nextSepPos = strpos($mountPoint, '/', 1);
+                                       // next separator is the last one, format matches
+                                       if ($nextSepPos === strlen($mountPoint) - 1) {
+                                               $user = substr($mountPoint, 1, $nextSepPos - 1);
                                        }
-                               });
-                       }
+                                       if ($user) {
+                                               $quota = OC_Util::getUserQuota($user);
+                                               if ($quota !== \OC\Files\SPACE_UNLIMITED) {
+                                                       return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota));
+                                               }
+                                       }
+                               }
+
+                               return $storage;
+                       });
+
                        $userDir = '/'.$user.'/files';
                        $userRoot = OC_User::getHome($user);
                        $userDirectory = $userRoot . '/files';
index d607a3e77256ec02ae75beb29cc761b6762e9bad..852caaeccc393cc7c1398840a4780197ed6c3291 100644 (file)
@@ -93,6 +93,55 @@ class Test_Util extends PHPUnit_Framework_TestCase {
                $this->assertStringStartsWith('oc', OC_Util::getInstanceId());
        }
 
+       /**
+        * Tests that the home storage is not wrapped when no quota exists.
+        */
+       function testHomeStorageWrapperWithoutQuota() {
+               $user1 = uniqid();
+               \OC_User::createUser($user1, 'test');
+               OC_Preferences::setValue($user1, 'files', 'quota', 'none');
+               \OC_User::setUserId($user1);
+
+               \OC_Util::setupFS($user1);
+
+               $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
+               $this->assertNotNull($userMount);
+               $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
+
+               // clean up
+               \OC_User::setUserId('');
+               \OC_User::deleteUser($user1);
+               OC_Preferences::deleteUser($user1);
+               \OC_Util::tearDownFS();
+       }
+
+       /**
+        * Tests that the home storage is not wrapped when no quota exists.
+        */
+       function testHomeStorageWrapperWithQuota() {
+               $user1 = uniqid();
+               \OC_User::createUser($user1, 'test');
+               OC_Preferences::setValue($user1, 'files', 'quota', '1024');
+               \OC_User::setUserId($user1);
+
+               \OC_Util::setupFS($user1);
+
+               $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
+               $this->assertNotNull($userMount);
+               $this->assertInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
+
+               // ensure that root wasn't wrapped
+               $rootMount = \OC\Files\Filesystem::getMountManager()->find('/');
+               $this->assertNotNull($rootMount);
+               $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $rootMount->getStorage());
+
+               // clean up
+               \OC_User::setUserId('');
+               \OC_User::deleteUser($user1);
+               OC_Preferences::deleteUser($user1);
+               \OC_Util::tearDownFS();
+       }
+
        /**
         * @dataProvider baseNameProvider
         */