Skip to content

Commit

Permalink
Merge pull request #26 from d3yii2/improve_upload_action
Browse files Browse the repository at this point in the history
Actions adapted for procesing more one model
  • Loading branch information
uldisn authored Nov 14, 2019
2 parents 28ef379 + be0ea57 commit 288082d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
25 changes: 15 additions & 10 deletions components/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Yii;
use yii\base\Action;
use yii\web\HttpException;
use yii\web\Response;
use d3yii2\d3files\models\D3files;
use d3yii2\d3files\models\D3filesModel;
Expand All @@ -20,10 +21,23 @@ class DeleteAction extends Action

public $modelName;

public function run($id)
public function run(int $id, string $model_name): string
{
Yii::$app->response->format = Response::FORMAT_JSON;

if(!Yii::$app->getModule('d3files')->disableController){
if (is_array($this->modelName) && !in_array($model_name, $this->modelName, true)) {
throw new HttpException(422, 'Can not upload file for requested model');
}

if (!is_array($this->modelName) && $model_name !== $this->modelName) {
throw new HttpException(422, 'Can not upload file for requested model');
}
}

$this->modelName = $model_name;


if (!$fileModel = D3filesModel::findOne(['id' => $id, 'deleted' => 0])) {
throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
}
Expand All @@ -32,15 +46,6 @@ public function run($id)
throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
}

/**
* validate model name
*/
if (Yii::$app->getModule('d3files')->disableController) {
if ($fileModelName->name !== $this->modelName) {
throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
}
}

// Check access rights to the record the file is attached to
D3files::performReadValidation($fileModelName->name, $fileModel->model_id);

Expand Down
37 changes: 20 additions & 17 deletions components/DownloadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use d3yii2\d3files\models\D3files;
use d3yii2\d3files\models\D3filesModel;
use d3yii2\d3files\models\D3filesModelName;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;

/**
Expand All @@ -21,39 +22,41 @@ class DownloadAction extends Action

public $downloadType = 'download';

const THE_REQUESTED_FILE_DOES_NOT_EXIST = 'The requested file does not exist.';
public const THE_REQUESTED_FILE_DOES_NOT_EXIST = 'The requested file does not exist.';

public function run($id)
public function run(int $id, string $model_name): void
{

if(!Yii::$app->getModule('d3files')->disableController){
if (is_array($this->modelName) && !in_array($model_name, $this->modelName, true)) {
throw new HttpException(422, 'Can not upload file for requested model');
}

if (!is_array($this->modelName) && $model_name !== $this->modelName) {
throw new HttpException(422, 'Can not upload file for requested model');
}
}

$this->modelName = $model_name;

if (!$fileModel = D3filesModel::findOne([
'id' => $id,
'deleted' => 0
])) {
\Yii::error( 'Can not find D3filesModel. id='.$id);
Yii::error( 'Can not find D3filesModel. id='.$id);
throw new NotFoundHttpException(Yii::t('d3files', self::THE_REQUESTED_FILE_DOES_NOT_EXIST));
}

if (!$file = D3files::findOne($fileModel->d3files_id)) {
\Yii::error( 'Can not find D3files. id='.$fileModel->d3files_id);
Yii::error( 'Can not find D3files. id='.$fileModel->d3files_id);
throw new NotFoundHttpException(Yii::t('d3files', self::THE_REQUESTED_FILE_DOES_NOT_EXIST));
}

if (!$fileModelName = D3filesModelName::findOne($fileModel->model_name_id)) {
\Yii::error( 'Can not find D3filesModelName. id=' . $fileModel->model_name_id);
Yii::error( 'Can not find D3filesModelName. id=' . $fileModel->model_name_id);
throw new NotFoundHttpException(Yii::t('d3files', self::THE_REQUESTED_FILE_DOES_NOT_EXIST));
}

/**
* validate modelname
*/
if (Yii::$app->getModule('d3files')->disableController) {
if ($fileModelName->name !== $this->modelName) {
\Yii::error( 'Incorrect model name. $fileModelName->name=' . $fileModelName->name . ' ;$this->modelNameid=' . $this->modelName);
throw new NotFoundHttpException(Yii::t('d3files', self::THE_REQUESTED_FILE_DOES_NOT_EXIST));
}
}

// Check access rights to the record the file is attached to
D3files::performReadValidation($fileModelName->name, $fileModel->model_id);

Expand All @@ -65,11 +68,11 @@ public function run($id)
//'deleted' => 0,
'is_file' => 1
])) {
\Yii::error( 'No found $realFileModel d3files_id=' . $fileModel->d3files_id);
Yii::error( 'No found $realFileModel d3files_id=' . $fileModel->d3files_id);
throw new NotFoundHttpException(Yii::t('d3files', self::THE_REQUESTED_FILE_DOES_NOT_EXIST));
}
if (!$realfileModelName = D3filesModelName::findOne($realFileModel->model_name_id)) {
\Yii::error( 'No found $realfileModelName id=' . $realFileModel->model_name_id);
Yii::error( 'No found $realfileModelName id=' . $realFileModel->model_name_id);
throw new NotFoundHttpException(Yii::t('d3files', self::THE_REQUESTED_FILE_DOES_NOT_EXIST));
}

Expand Down
24 changes: 15 additions & 9 deletions components/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,35 @@ class UploadAction extends Action
{

/**
* @var string parent model name (with namespace)
* @var string|string[] parent model name (with namespace)
* $_POST['model_name'] is used if controller actions are not disabled
*/
public $modelName;

public function run($id)
public function run(int $id): string
{
// $id here is id for model to which will be attached attachments

Yii::$app->response->format = Response::FORMAT_JSON;

$postModelName = Yii::$app->request->post('model_name');

if(!Yii::$app->getModule('d3files')->disableController){
if (is_array($this->modelName) && !in_array($postModelName, $this->modelName, true)) {
throw new HttpException(422, 'Can not upload file for requested model');
}

if (!is_array($this->modelName) && $postModelName !== $this->modelName) {
throw new HttpException(422, 'Can not upload file for requested model');
}
}

if (!isset($_FILES['upload_file'])) {
throw new NotFoundHttpException(Yii::t('d3files', 'File not uploaded.'));
}

// If controller actions are not disabled, use $_POST['model_name']
if (!Yii::$app->getModule('d3files')->disableController) {
$this->modelName = Yii::$app->request->post('model_name');
}
$this->modelName = $postModelName;

if (empty($this->modelName)) {
throw new HttpException(422, Yii::t('d3files', 'mandatory POST parameter modelName is not set'));
}

// Check access rights to the record the file is attached to
D3files::performReadValidation($this->modelName, $id);
Expand Down
9 changes: 7 additions & 2 deletions views/d3files/_list_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
'<i class="fa fa-cloud-download text-primary"></i> ' . $file['file_name'],
Url::to([
$urlPrefix . 'd3filesdownload',
'id' => $file['file_model_id']
'id' => $file['file_model_id'],
'model_name' => $model_name
]),
[
'data-title' => Yii::t('d3files', 'Download'),
Expand All @@ -40,7 +41,11 @@
if (!$readOnly) {
echo Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
[$urlPrefix . 'd3filesdelete', 'id' => $file['file_model_id']],
[
$urlPrefix . 'd3filesdelete',
'id' => $file['file_model_id'],
'model_name' => $model_name
],
[
'data-title' => Yii::t('d3files', 'Delete'),
'data-placement' => 'top',
Expand Down

0 comments on commit 288082d

Please sign in to comment.