Merged in feature/PMCORE-1619 (pull request #7376)

PMCORE-1619 Validations in the upload files related to cases notes before to move appDocument.

Approved-by: Julio Cesar Laura Avendaño <contact@julio-laura.com>
This commit is contained in:
Roly Rudy Gutierrez Pinto
2020-06-13 01:00:29 +00:00
committed by Julio Cesar Laura Avendaño
9 changed files with 240 additions and 31 deletions

View File

@@ -41,6 +41,7 @@ use ProcessMaker\BusinessModel\Task as BmTask;
use ProcessMaker\BusinessModel\User as BmUser;
use ProcessMaker\Core\System;
use ProcessMaker\Exception\UploadException;
use ProcessMaker\Exception\CaseNoteUploadFile;
use ProcessMaker\Model\Application as ModelApplication;
use ProcessMaker\Model\AppNotes as Notes;
use ProcessMaker\Model\Delegation;
@@ -49,6 +50,7 @@ use ProcessMaker\Plugins\PluginRegistry;
use ProcessMaker\Services\OAuth2\Server;
use ProcessMaker\Util\DateTime as UtilDateTime;
use ProcessMaker\Validation\ExceptionRestApi;
use ProcessMaker\Validation\ValidationUploadedFiles;
use ProcessMaker\Validation\Validator as FileValidator;
use ProcessPeer;
use ProcessUser;
@@ -3960,6 +3962,21 @@ class Cases
}
}
//rules validation
foreach ($files as $key => $value) {
$entry = [
"filename" => $value['name'],
"path" => $value['tmp_name']
];
$validator = ValidationUploadedFiles::getValidationUploadedFiles()
->runRulesForPostFilesOfNote($entry);
if ($validator->fails()) {
Notes::where('NOTE_ID', '=', $noteId)->delete();
$messageError = G::LoadTranslation('ID_THE_FILE_COULDNT_BE_UPLOADED');
throw new CaseNoteUploadFile($messageError . ' ' . $validator->getMessage());
}
}
// Get the delIndex related to the case
$cases = new ClassesCases();
$delIndex = $cases->getCurrentDelegation($appUid);
@@ -4007,8 +4024,6 @@ class Cases
throw new UploadException($fileName['error']);
}
}
} else {
throw new Exception(G::LoadTranslation('ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR'));
}
return $response;

View File

@@ -0,0 +1,21 @@
<?php
namespace ProcessMaker\Exception;
use Exception;
use Throwable;
class CaseNoteUploadFile extends Exception
{
/**
* Constructor method.
* @param string $message
* @param int $code
* @param Throwable $previous
*/
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@@ -27,6 +27,16 @@ class ValidationUploadedFiles
*/
private $fails = [];
/**
* Return this constant when rule is invalid.
*/
private const INVALID = true;
/**
* Return this constant when rule is valid.
*/
private const VALID = false;
/**
* Check if the loaded files comply with the validation rules, add here if you
* want more validation rules.
@@ -280,6 +290,110 @@ class ValidationUploadedFiles
return $validator->validate();
}
/**
* Check if the loaded files comply with the validation rules, add here if you
* want more validation rules.
* Accept per argument an array or object that contains a "filename" and "path" values.
* The rules are verified in the order in which they have been added.
*
* @param array|object $file
* @return Validator
*/
public function runRulesForPostFilesOfNote($file)
{
$validator = new Validator();
//rule: file exists
$rule = $validator->addRule();
$rule->validate($file, function($file) use($rule) {
$path = isset($file->path) ? $file->path : "";
$filesystem = new Filesystem();
if (!$filesystem->exists($path)) {
$rule->message(G::LoadTranslation('ID_NOT_EXISTS_FILE'));
return self::INVALID;
}
return self::VALID;
})
->status(400)
->log(function($rule) {
/**
* Levels supported by MonologProvider is:
* 100 "DEBUG"
* 200 "INFO"
* 250 "NOTICE"
* 300 "WARNING"
* 400 "ERROR"
* 500 "CRITICAL"
* 550 "ALERT"
* 600 "EMERGENCY"
*/
Bootstrap::registerMonologPhpUploadExecution('phpUpload', $rule->getStatus(), $rule->getMessage(), $rule->getData()->filename);
});
//rule: extensions
$rule = $validator->addRule();
$rule->validate($file, function($file) use($rule) {
$filesystem = new Filesystem();
$extension = strtolower($filesystem->extension($file->filename));
$extensions = [
'pdf', 'gif', 'jpg', 'png', 'doc', 'docx', 'xls', 'xlsx', 'txt', 'mp4', 'mpv', 'mpeg', 'mpg', 'mov'
];
if (!in_array($extension, $extensions)) {
$rule->message(G::LoadTranslation('ID_YOU_UPLOADED_AN_UNSUPPORTED_FILE_EXTENSION'));
return self::INVALID;
}
return self::VALID;
})
->status(400)
->log(function($rule) {
/**
* Levels supported by MonologProvider is:
* 100 "DEBUG"
* 200 "INFO"
* 250 "NOTICE"
* 300 "WARNING"
* 400 "ERROR"
* 500 "CRITICAL"
* 550 "ALERT"
* 600 "EMERGENCY"
*/
Bootstrap::registerMonologPhpUploadExecution('phpUpload', $rule->getStatus(), $rule->getMessage(), $rule->getData()->filename);
});
//rule: file size
$rule = $validator->addRule();
$rule->validate($file, function($file) use($rule) {
$path = isset($file->path) ? $file->path : "";
$filesystem = new Filesystem();
$limitSize = '10M';
$size = $filesystem->size($path);
$phpShorthandByte = new PhpShorthandByte();
$postMaxSizeBytes = $phpShorthandByte->valueToBytes($limitSize);
if ($size > $postMaxSizeBytes) {
$rule->message(G::LoadTranslation('ID_YOUR_FILE_HAS_EXCEEDED', [$limitSize]));
return self::INVALID;
}
return self::VALID;
})
->status(400)
->log(function($rule) {
/**
* Levels supported by MonologProvider is:
* 100 "DEBUG"
* 200 "INFO"
* 250 "NOTICE"
* 300 "WARNING"
* 400 "ERROR"
* 500 "CRITICAL"
* 550 "ALERT"
* 600 "EMERGENCY"
*/
Bootstrap::registerMonologPhpUploadExecution('phpUpload', $rule->getStatus(), $rule->getMessage(), $rule->getData()->filename);
});
return $validator->validate();
}
/**
* Get the first error and call the argument function.
*