Permalink
Browse files

Merge branch 'MDL-59544-master-filemanagervalidation' of git://github…

….com/mudrd8mz/moodle
  • Loading branch information...
dmonllao committed Jul 25, 2017
2 parents 6d6b35f + 0fa2b2e commit a2b326c2322800cc084bec773b7172db1de21dc4
@@ -41,6 +41,7 @@
$string['err_numeric'] = 'You must enter a number here.';
$string['err_rangelength'] = 'You must enter between {$a->format[0]} and {$a->format[1]} characters here.';
$string['err_required'] = 'You must supply a value here.';
$string['err_wrongfileextension'] = 'Some files ({$a->wrongfiles}) cannot be uploaded. Only file types {$a->whitelist} are allowed.';
$string['filetypesany'] = 'All file types';
$string['filetypesnotall'] = 'It is not allowed to select \'All file types\' here';
$string['filetypesnotwhitelisted'] = 'These file types are not allowed here: {$a}';
@@ -308,6 +308,47 @@ public function export_for_template(renderer_base $output) {
$context['html'] = $this->toHtml();
return $context;
}
/**
* Check that all files have the allowed type.
*
* @param array $value Draft item id with the uploaded files.
* @return string|null Validation error message or null.
*/
public function validateSubmitValue($value) {
$filetypesutil = new \core_form\filetypes_util();
$whitelist = $filetypesutil->normalize_file_types($this->_options['accepted_types']);
if (empty($whitelist) || $whitelist === ['*']) {
// Any file type is allowed, nothing to check here.
return;
}
$draftfiles = file_get_drafarea_files($value);
$wrongfiles = array();
if (empty($draftfiles)) {
// No file uploaded, nothing to check here.
return;
}
foreach ($draftfiles->list as $file) {
if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
$wrongfiles[] = $file->filename;
}
}
if ($wrongfiles) {
$a = array(
'whitelist' => implode(', ', $whitelist),
'wrongfiles' => implode(', ', $wrongfiles),
);
return get_string('err_wrongfileextension', 'core_form', $a);
}
return;
}
}
/**
@@ -230,4 +230,45 @@ public function export_for_template(renderer_base $output) {
$context['html'] = $this->toHtml();
return $context;
}
/**
* Check that the file has the allowed type.
*
* @param array $value Draft item id with the uploaded files.
* @return string|null Validation error message or null.
*/
public function validateSubmitValue($value) {
$filetypesutil = new \core_form\filetypes_util();
$whitelist = $filetypesutil->normalize_file_types($this->_options['accepted_types']);
if (empty($whitelist) || $whitelist === ['*']) {
// Any file type is allowed, nothing to check here.
return;
}
$draftfiles = file_get_drafarea_files($value);
$wrongfiles = array();
if (empty($draftfiles)) {
// No file uploaded, nothing to check here.
return;
}
foreach ($draftfiles->list as $file) {
if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
$wrongfiles[] = $file->filename;
}
}
if ($wrongfiles) {
$a = array(
'whitelist' => implode(', ', $whitelist),
'wrongfiles' => implode(', ', $wrongfiles),
);
return get_string('err_wrongfileextension', 'core_form', $a);
}
return;
}
}
@@ -34,4 +34,15 @@ Feature: There is a form element allowing to select filetypes
Then I should see "Unknown file types: .doesnoexist"
And I should see "These file types are not allowed here: .doc, .docx, .rtf"
And I should see "It is not allowed to select 'All file types' here"
And I should not see "Unknown file types: .neverminditdoesnotexist"
And I should not see "Unknown file types: .neverminditdoesnotexist"
@javascript @_file_upload
Scenario: File manager element implicitly validates submitted files
# We can't directly upload the invalid file here as the upload repository would throw an exception.
# So instead we try to trick the filemanager, to be finally stopped by the implicit validation.
And I upload "lib/tests/fixtures/empty.txt" file to "Picky file manager" filemanager
And I follow "empty.txt"
And I set the field "Name" to "renamed.exe"
And I press "Update"
When I press "Save changes"
Then I should see "Some files (renamed.exe) cannot be uploaded. Only file types .txt are allowed."
@@ -57,6 +57,8 @@ public function definition() {
$mform->addElement('filetypes', 'filetypes3', 'Unknown file types are allowed here',
['allowunknown' => true]);
$mform->addElement('filemanager', 'fileman1', 'Picky file manager', null, ['accepted_types' => '.txt']);
$this->add_action_buttons(false);
}
}
@@ -120,41 +120,4 @@ protected function definition_inner(&$mform) {
public function is_editable() {
return !$this->_form->isFrozen();
}
/**
* Validate assessment form data.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (isset($data['feedbackauthorattachment_filemanager']) and isset($this->workshop->overallfeedbackfiletypes)) {
$filetypesutil = new \core_form\filetypes_util();
$whitelist = $filetypesutil->normalize_file_types($this->workshop->overallfeedbackfiletypes);
if ($whitelist) {
$draftfiles = file_get_drafarea_files($data['feedbackauthorattachment_filemanager']);
if ($draftfiles) {
$wrongfiles = array();
foreach ($draftfiles->list as $file) {
if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
$wrongfiles[] = $file->filename;
}
}
if ($wrongfiles) {
$a = array(
'whitelist' => implode(', ', $whitelist),
'wrongfiles' => implode(', ', $wrongfiles),
);
$errors['feedbackauthorattachment_filemanager'] = get_string('err_wrongfileextension', 'mod_workshop', $a);
}
}
}
}
return $errors;
}
}
@@ -1,2 +1,3 @@
err_unknownfileextension,mod_workshop
err_wrongfileextension,mod_workshop
yourassessment,mod_workshop
@@ -107,7 +107,6 @@
$string['editsubmission'] = 'Edit submission';
$string['err_multiplesubmissions'] = 'While editing this form, another version of the submission has been saved. Multiple submissions per user are not allowed.';
$string['err_removegrademappings'] = 'Unable to remove the unused grade mappings';
$string['err_wrongfileextension'] = 'Some files ({$a->wrongfiles}) cannot be uploaded. Only file types {$a->whitelist} are allowed.';
$string['evaluategradeswait'] = 'Please wait until the assessments are evaluated and the grades are calculated';
$string['evaluation'] = 'Grading evaluation';
$string['evaluationmethod'] = 'Grading evaluation method';
@@ -375,3 +374,4 @@
// Deprecated since Moodle 3.4.
$string['err_unknownfileextension'] = 'Unknown file extension: {$a}';
$string['err_wrongfileextension'] = 'Some files ({$a->wrongfiles}) cannot be uploaded. Only file types {$a->whitelist} are allowed.';
@@ -94,29 +94,6 @@ function validation($data, $files) {
$errors['attachment_filemanager'] = get_string('submissionrequiredfile', 'mod_workshop');
}
if (isset($data['attachment_filemanager']) and isset($this->_customdata['workshop']->submissionfiletypes)) {
$filetypesutil = new \core_form\filetypes_util();
$whitelist = $filetypesutil->normalize_file_types($this->_customdata['workshop']->submissionfiletypes);
if ($whitelist) {
$draftfiles = file_get_drafarea_files($data['attachment_filemanager']);
if ($draftfiles) {
$wrongfiles = array();
foreach ($draftfiles->list as $file) {
if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
$wrongfiles[] = $file->filename;
}
}
if ($wrongfiles) {
$a = array(
'whitelist' => implode(', ', $whitelist),
'wrongfiles' => implode(', ', $wrongfiles),
);
$errors['attachment_filemanager'] = get_string('err_wrongfileextension', 'mod_workshop', $a);
}
}
}
}
return $errors;
}
}

0 comments on commit a2b326c

Please sign in to comment.