From e06fa200b394162f2a30a481a8b1423685182863 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 17 Jun 2014 20:08:40 +0200 Subject: [PATCH] make sure that we always use the right user --- apps/files_trashbin/ajax/delete.php | 2 +- apps/files_trashbin/ajax/list.php | 2 +- apps/files_trashbin/ajax/undelete.php | 2 +- apps/files_trashbin/lib/helper.php | 4 ++-- apps/files_trashbin/lib/trashbin.php | 31 +++++++++++++------------- apps/files_trashbin/tests/trashbin.php | 12 +++++----- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/apps/files_trashbin/ajax/delete.php b/apps/files_trashbin/ajax/delete.php index ebabc5bc7a..9d9c1dd100 100644 --- a/apps/files_trashbin/ajax/delete.php +++ b/apps/files_trashbin/ajax/delete.php @@ -37,7 +37,7 @@ foreach ($list as $file) { $timestamp = null; } - OCA\Files_Trashbin\Trashbin::delete($filename, $timestamp); + OCA\Files_Trashbin\Trashbin::delete($filename, \OCP\User::getUser(), $timestamp); if (OCA\Files_Trashbin\Trashbin::file_exists($filename, $timestamp)) { $error[] = $filename; OC_Log::write('trashbin','can\'t delete ' . $filename . ' permanently.', OC_Log::ERROR); diff --git a/apps/files_trashbin/ajax/list.php b/apps/files_trashbin/ajax/list.php index e1f52e814b..6cad101d34 100644 --- a/apps/files_trashbin/ajax/list.php +++ b/apps/files_trashbin/ajax/list.php @@ -10,7 +10,7 @@ $data = array(); // make filelist try { - $files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir, $sortAttribute, $sortDirection); + $files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir, \OCP\User::getUser(), $sortAttribute, $sortDirection); } catch (Exception $e) { header("HTTP/1.0 404 Not Found"); exit(); diff --git a/apps/files_trashbin/ajax/undelete.php b/apps/files_trashbin/ajax/undelete.php index 2b00078669..afab79fcac 100644 --- a/apps/files_trashbin/ajax/undelete.php +++ b/apps/files_trashbin/ajax/undelete.php @@ -16,7 +16,7 @@ if (isset($_POST['allfiles']) and $_POST['allfiles'] === 'true') { if ($dir === '' || $dir === '/') { $dirListing = false; } - foreach (OCA\Files_Trashbin\Helper::getTrashFiles($dir) as $file) { + foreach (OCA\Files_Trashbin\Helper::getTrashFiles($dir, \OCP\User::getUser()) as $file) { $fileName = $file['name']; if (!$dirListing) { $fileName .= '.d' . $file['mtime']; diff --git a/apps/files_trashbin/lib/helper.php b/apps/files_trashbin/lib/helper.php index ebedce31ab..d0ca5fb153 100644 --- a/apps/files_trashbin/lib/helper.php +++ b/apps/files_trashbin/lib/helper.php @@ -11,14 +11,14 @@ class Helper * * @param string $dir path to the directory inside the trashbin * or empty to retrieve the root of the trashbin + * @param string $user * @param string $sortAttribute attribute to sort on or empty to disable sorting * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] */ - public static function getTrashFiles($dir, $sortAttribute = '', $sortDescending = false){ + public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false){ $result = array(); $timestamp = null; - $user = \OCP\User::getUser(); $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 7683f97548..f3d255d179 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -540,12 +540,12 @@ class Trashbin { * delete file from trash bin permanently * * @param string $filename path to the file + * @param string $user * @param int $timestamp of deletion time * * @return int size of deleted files */ - public static function delete($filename, $timestamp = null) { - $user = \OCP\User::getUser(); + public static function delete($filename, $user, $timestamp = null) { $view = new \OC\Files\View('/' . $user); $size = 0; @@ -667,11 +667,11 @@ class Trashbin { * calculate remaining free space for trash bin * * @param integer $trashbinSize current size of the trash bin + * @param string $user * @return int available free space for trash bin */ - private static function calculateFreeSpace($trashbinSize) { + private static function calculateFreeSpace($trashbinSize, $user) { $softQuota = true; - $user = \OCP\User::getUser(); $quota = \OC_Preferences::getValue($user, 'files', 'quota'); $view = new \OC\Files\View('/' . $user); if ($quota === null || $quota === 'default') { @@ -709,7 +709,7 @@ class Trashbin { $size = self::getTrashbinSize($user); - $freeSpace = self::calculateFreeSpace($size); + $freeSpace = self::calculateFreeSpace($size, $user); if ($freeSpace < 0) { self::expire($size, $user); @@ -731,24 +731,23 @@ class Trashbin { return 0; } - $user = \OCP\User::getUser(); - $availableSpace = self::calculateFreeSpace($trashbinSize); + $availableSpace = self::calculateFreeSpace($trashbinSize, $user); $size = 0; $retention_obligation = \OC_Config::getValue('trashbin_retention_obligation', self::DEFAULT_RETENTION_OBLIGATION); $limit = time() - ($retention_obligation * 86400); - $dirContent = Helper::getTrashFiles('/', 'mtime'); + $dirContent = Helper::getTrashFiles('/', $user, 'mtime'); // delete all files older then $retention_obligation - list($delSize, $count) = self::deleteExpiredFiles($dirContent, $limit, $retention_obligation); + list($delSize, $count) = self::deleteExpiredFiles($dirContent, $user, $limit, $retention_obligation); $size += $delSize; $availableSpace += $size; // delete files from trash until we meet the trash bin size limit again - $size += self::deleteFiles(array_slice($dirContent, $count), $availableSpace); + $size += self::deleteFiles(array_slice($dirContent, $count), $user, $availableSpace); return $size; } @@ -757,16 +756,17 @@ class Trashbin { * if the size limit for the trash bin is reached, we delete the oldest * files in the trash bin until we meet the limit again * @param array $files - * @param init $availableSpace available disc space + * @param string $user + * @param int $availableSpace available disc space * @return int size of deleted files */ - protected function deleteFiles($files, $availableSpace) { + protected function deleteFiles($files, $user, $availableSpace) { $size = 0; if ($availableSpace < 0) { foreach ($files as $file) { if ($availableSpace < 0) { - $tmp = self::delete($file['name'], $file['mtime']); + $tmp = self::delete($file['name'], $user, $file['mtime']); \OC_Log::write('files_trashbin', 'remove "' . $file['name'] . '" (' . $tmp . 'B) to meet the limit of trash bin size (50% of available quota)', \OC_log::INFO); $availableSpace += $tmp; $size += $tmp; @@ -782,11 +782,12 @@ class Trashbin { * delete files older then max storage time * * @param array $files list of files sorted by mtime + * @param string $user * @param int $limit files older then limit should be deleted * @param int $retention_obligation max age of file in days * @return array size of deleted files and number of deleted files */ - protected static function deleteExpiredFiles($files, $limit, $retention_obligation) { + protected static function deleteExpiredFiles($files, $user, $limit, $retention_obligation) { $size = 0; $count = 0; foreach ($files as $file) { @@ -794,7 +795,7 @@ class Trashbin { $filename = $file['name']; if ($timestamp < $limit) { $count++; - $size += self::delete($filename, $timestamp); + $size += self::delete($filename, $user, $timestamp); \OC_Log::write('files_trashbin', 'remove "' . $filename . '" from trash bin because it is older than ' . $retention_obligation, \OC_log::INFO); } else { break; diff --git a/apps/files_trashbin/tests/trashbin.php b/apps/files_trashbin/tests/trashbin.php index 9e29d84464..e94a501109 100644 --- a/apps/files_trashbin/tests/trashbin.php +++ b/apps/files_trashbin/tests/trashbin.php @@ -88,7 +88,7 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { \OC\Files\Filesystem::unlink('file3.txt'); //make sure that files are in the trash bin - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/'); + $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); $this->assertSame(3, count($filesInTrash)); $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $expiredDate); @@ -106,7 +106,7 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { $this->assertSame('file2.txt', $remainingFile['name']); // check that file1.txt and file3.txt was really deleted - $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/'); + $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); $this->assertSame(1, count($newTrashContent)); $element = reset($newTrashContent); $this->assertSame('file2.txt', $element['name']); @@ -147,7 +147,7 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { \OC\Files\Filesystem::unlink('file1.txt'); //make sure that files are in the trash bin - $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', 'mtime'); + $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime'); $this->assertSame(3, count($filesInTrash)); $testClass = new TrashbinForTesting(); @@ -156,7 +156,7 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { // the two oldest files (file3.txt and file2.txt) should be deleted $this->assertSame(10, $sizeOfDeletedFiles); - $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/'); + $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1); $this->assertSame(1, count($newTrashContent)); $element = reset($newTrashContent); $this->assertSame('file1.txt', $element['name']); @@ -189,10 +189,10 @@ class Test_Trashbin extends \PHPUnit_Framework_TestCase { class TrashbinForTesting extends Files_Trashbin\Trashbin { public function dummyDeleteExpiredFiles($files, $limit) { // dummy value for $retention_obligation because it is not needed here - return parent::deleteExpiredFiles($files, $limit, 0); + return parent::deleteExpiredFiles($files, \Test_Trashbin::TEST_TRASHBIN_USER1, $limit, 0); } public function dummyDeleteFiles($files, $availableSpace) { - return parent::deleteFiles($files, $availableSpace); + return parent::deleteFiles($files, \Test_Trashbin::TEST_TRASHBIN_USER1, $availableSpace); } }