From fc97f320105e52884c1aed57b6f5d4ecf4ae591b Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 16 Mar 2023 16:56:07 +0100 Subject: [PATCH] MDL-73226 files: Add quota checks to core_user_add_user_private_files --- user/externallib.php | 10 +++++++ user/tests/externallib_test.php | 48 +++++++++++++++++++++++++++++++++ user/upgrade.txt | 1 + 3 files changed, 59 insertions(+) diff --git a/user/externallib.php b/user/externallib.php index 9a58d1103da..946027a9ab5 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -1186,6 +1186,7 @@ class core_user_external extends \core_external\external_api { * Copy files from a draft area to users private files area. * * @throws invalid_parameter_exception + * @throws moodle_exception * @param int $draftid Id of a draft area containing files. * @return array An array of warnings * @since Moodle 2.6 @@ -1208,6 +1209,15 @@ class core_user_external extends \core_external\external_api { if (has_capability('moodle/user:ignoreuserquota', $context)) { $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS; $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED; + } else { + // Get current used space for this user. + $usedspace = file_get_user_used_space(); + // Get the total size of the new files we want to add to private files. + $newfilesinfo = file_get_draft_area_info($params['draftid']); + + if (($newfilesinfo['filesize_without_references'] + $usedspace) > $maxareabytes) { + throw new moodle_exception('maxareabytes'); + } } $options = array('subdirs' => 1, diff --git a/user/tests/externallib_test.php b/user/tests/externallib_test.php index 387bca93f6a..d986c4b0e71 100644 --- a/user/tests/externallib_test.php +++ b/user/tests/externallib_test.php @@ -1014,6 +1014,54 @@ class externallib_test extends externallib_advanced_testcase { $this->assertNotEmpty($file); } + + /** + * Test add_user_private_files quota + */ + public function test_add_user_private_files_quota() { + global $USER, $CFG, $DB; + + $this->resetAfterTest(true); + + $context = \context_system::instance(); + $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id); + + $context = \context_user::instance($USER->id); + $contextid = $context->id; + $component = "user"; + $filearea = "draft"; + $itemid = 0; + $filepath = "/"; + $filename = "Simple.txt"; + $filecontent = base64_encode("Let us create a nice simple file"); + $contextlevel = null; + $instanceid = null; + $browser = get_file_browser(); + + // Call the files api to create a file. + $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, + $filename, $filecontent, $contextlevel, $instanceid); + $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile); + $draftid = $draftfile['itemid']; + + // Call the external function to add the file to private files. + core_user_external::add_user_private_files($draftid); + + // Force the quota so we are sure it won't be space to add the new file. + $CFG->userquota = file_get_user_used_space() + 1; + + // Generate a new draftitemid for the same testfile. + $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, + $filename, $filecontent, $contextlevel, $instanceid); + $draftid = $draftfile['itemid']; + + $this->expectException('moodle_exception'); + $this->expectExceptionMessage(get_string('maxareabytes', 'error')); + + // Call the external function to include the new file. + core_user_external::add_user_private_files($draftid); + } + /** * Test add user device */ diff --git a/user/upgrade.txt b/user/upgrade.txt index 609451b6456..9bda89264ba 100644 --- a/user/upgrade.txt +++ b/user/upgrade.txt @@ -14,6 +14,7 @@ This files describes API changes for code that uses the user API. * New method `core_user::is_current_user`, useful for components implementing permission callbacks for their preferences * New `profile_get_user_field` method for returning profile field instance of given type * The `profile_field_base::is_visible` method now accepts an optional `$context` argument +* External function core_user_external::add_user_private_files() now returns moodle_exception when the user quota is exceeded === 4.1 ===