Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions app/Http/Controllers/Api/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use League\Csv\Reader;
use Onnov\DetectEncoding\EncodingDetector;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
Expand Down Expand Up @@ -149,7 +150,9 @@ public function store() : JsonResponse
}

$date = date('Y-m-d-his');
$fixed_filename = str_slug($file->getClientOriginalName());

$fixed_filename = Str::of($file->getClientOriginalName())->basename('.csv').'.csv';

try {
$file->move($path, $date.'-'.$fixed_filename);
} catch (FileException $exception) {
Expand Down Expand Up @@ -211,36 +214,47 @@ public function process(ItemImportRequest $request, $import_id) : JsonResponse
$redirectTo = 'hardware.index';
switch ($request->get('import-type')) {
case 'asset':
$model_perms = 'App\Models\Asset';
$redirectTo = 'hardware.index';
break;
case 'assetModel':
$model_perms = 'App\Models\AssetModel';
$redirectTo = 'models.index';
break;
case 'accessory':
$model_perms = 'App\Models\Accessory';
$redirectTo = 'accessories.index';
break;
case 'consumable':
$model_perms = 'App\Models\Consumable';
$redirectTo = 'consumables.index';
break;
case 'component':
$model_perms = 'App\Models\Component';
$redirectTo = 'components.index';
break;
case 'license':
$model_perms = 'App\Models\License';
$redirectTo = 'licenses.index';
break;
case 'user':
$model_perms = 'App\Models\User';
$redirectTo = 'users.index';
break;
case 'location':
$model_perms = 'App\Models\Location';
$redirectTo = 'locations.index';
break;
case 'supplier':
$model_perms = 'App\Models\Supplier';
$redirectTo = 'suppliers.index';
break;
case 'manufacturer':
$model_perms = 'App\Models\Manufacturer';
$redirectTo = 'manufacturers.index';
break;
case 'category':
$model_perms = 'App\Models\Category';
$redirectTo = 'categories.index';
break;
}
Expand All @@ -251,7 +265,11 @@ public function process(ItemImportRequest $request, $import_id) : JsonResponse
//Flash message before the redirect
Session::flash('success', trans('admin/hardware/message.import.success'));

return response()->json(Helper::formatStandardApiResponse('success', null, ['redirect_url' => route($redirectTo)]));
if (auth()->user()->can('view', $model_perms)) {
return response()->json(Helper::formatStandardApiResponse('success', null, ['redirect_url' => route($redirectTo)]));
}

return response()->json(Helper::formatStandardApiResponse('success', null, ['redirect_url' => route('imports.index')]));
}

/**
Expand All @@ -261,9 +279,16 @@ public function process(ItemImportRequest $request, $import_id) : JsonResponse
*/
public function destroy($import_id) : JsonResponse
{
$this->authorize('create', Asset::class);
$this->authorize('import');

if ($import = Import::find($import_id)) {


if ((auth()->user()->id != $import->created_by) && (!auth()->user()->isSuperUser())) {
return response()->json(Helper::formatStandardApiResponse('warning', null, trans('admin/hardware/message.import.file_not_deleted_warning')));
}


try {
// Try to delete the file
Storage::delete('imports/'.$import->file_path);
Expand All @@ -280,4 +305,6 @@ public function destroy($import_id) : JsonResponse
}
return response()->json(Helper::formatStandardApiResponse('warning', null, trans('admin/hardware/message.import.file_not_deleted_warning')));
}


}
28 changes: 27 additions & 1 deletion app/Http/Controllers/UploadedFilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Http\Controllers;

use App\Helpers\Helper;
use App\Helpers\StorageHelper;
use App\Http\Requests\UploadFileRequest;
use App\Models\Actionlog;
use App\Models\Import;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand Down Expand Up @@ -155,7 +157,31 @@ public function destroy($object_type, $id, $file_id) : RedirectResponse
}

// The file doesn't seem to really exist, so report an error
return redirect()->back()->withFragment('files')->with('success', trans_choice('general.file_upload_status.delete.error', 1));
return redirect()->back()->withFragment('files')->with('error', trans_choice('general.file_upload_status.delete.error', 1));

}

public function downloadImport(Import $import) {

$this->authorize('import');

if ($import = Import::find($import->id)) {

if ((auth()->user()->id != $import->created_by) && (!auth()->user()->isSuperUser())) {
return redirect()->back()->with('error', trans('general.file_upload_status.file_not_found'));
}

if (config('filesystems.default') == 's3_private') {
return redirect()->away(Storage::disk('s3_private')->temporaryUrl('private_uploads/imports/' . $import->file_path, now()->addMinutes(5)));
}

if (Storage::exists('private_uploads/imports/' . $import->file_path)) {
return response()->download(config('app.private_uploads') . '/imports/' . $import->file_path);
}

}

return redirect()->back()->with('error', trans('general.file_upload_status.file_not_found'));

}

Expand Down
9 changes: 8 additions & 1 deletion app/Livewire/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,13 @@ public function destroy($id)
return;
}

if ((auth()->user()->id != $import->created_by) && (!auth()->user()->isSuperUser())) {
$this->message = trans('general.generic_model_not_found', ['model' => trans('general.import')]);
$this->message_type = 'danger';

return;
}

if (Storage::delete('private_uploads/imports/' . $import->file_path)) {
$import->delete();
$this->message = trans('admin/hardware/message.import.file_delete_success');
Expand All @@ -683,7 +690,7 @@ public function destroy($id)
return;
}

$this->message = trans('admin/hardware/message.import.file_delete_error');
$this->message = trans('general.generic_model_not_found', ['model' => trans('general.import')]);
$this->message_type = 'danger';
}

Expand Down
4 changes: 4 additions & 0 deletions resources/views/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,10 @@
border-top: 0px !important;
}

h4#progress-text {
color: white !important;
}

</style>

{{-- Custom CSS --}}
Expand Down
45 changes: 34 additions & 11 deletions resources/views/livewire/importer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@
</div>
<div class="row">
<div class="col-md-12 table-responsive" style="padding-top: 30px;">
<table
data-id-table="upload-table"

<table data-id-table="upload-table"
data-side-pagination="client"
id="upload-table"
class="col-md-12 table table-striped snipe-table">
Expand All @@ -126,21 +124,46 @@ class="col-md-12 table table-striped snipe-table">
@foreach($this->files as $currentFile)

<tr style="{{ ($this->activeFile && ($currentFile->id == $this->activeFile->id)) ? 'font-weight: bold' : '' }}" class="{{ ($this->activeFile && ($currentFile->id == $this->activeFile->id)) ? '' : '' }}">
<td>{{ $currentFile->file_path }}</td>
<td>
@if ((auth()->user()->id == $currentFile->adminuser->id) || (auth()->user()->isSuperUser()))
<a href="{{ route('imports.download', $currentFile) }}">{{ $currentFile->file_path }}</a>
@else
{{ $currentFile->file_path }}
@endif
</td>
<td>{{ Helper::getFormattedDateObject($currentFile->created_at, 'datetime', false) }}</td>
<td>{{ ($currentFile->adminuser) ? $currentFile->adminuser->present()->fullName : '--'}}</td>
<td>
@if ($currentFile->adminuser)
@can('view', $currentFile->adminuser)
<a href="{{ route('users.show', $currentFile->adminuser) }}">{{ $currentFile->adminuser->display_name }}</a>
@else
{{ $currentFile->adminuser->display_name }}
@endcan
@else
---
@endif

</td>
<td>{{ Helper::formatFilesizeUnits($currentFile->filesize) }}</td>
<td class="col-md-1 text-right" style="white-space: nowrap;">
<button class="btn btn-sm btn-info" wire:click="selectFile({{ $currentFile->id }})" data-tooltip="true" data-title="{{ trans('general.import_this_file') }}">
<i class="fa-solid fa-list-check" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.import') }}</span>
</button>
<a href="#" wire:click.prevent="$set('activeFileId',null)" data-tooltip="true" data-title="{{ trans('general.delete') }}">
<button class="btn btn-sm btn-danger" wire:click="destroy({{ $currentFile->id }})">
<i class="fas fa-trash icon-white" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.delete') }}</span>
</button>
</a>
@if ((auth()->user()->id == $currentFile->adminuser->id) || (auth()->user()->isSuperUser()))
<a href="#" wire:click.prevent="$set('activeFileId',null)" data-tooltip="true" data-title="{{ trans('general.delete') }}">
<button class="btn btn-sm btn-danger" wire:click="destroy({{ $currentFile->id }})">
<i class="fas fa-trash icon-white" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.delete') }}</span>
</button>
</a>
@else
<a data-tooltip="true" class="btn btn-sm btn-danger disabled" data-title="{{ trans('general.delete') }}">
<i class="fas fa-trash icon-white" aria-hidden="true"></i>
<span class="sr-only">{{ trans('general.delete') }}</span>
</a>
@endif

</td>
</tr>

Expand Down
25 changes: 19 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\Account;
use App\Http\Controllers\ActionlogController;
use App\Http\Controllers\Api\ImportController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\ResetPasswordController;
Expand Down Expand Up @@ -333,12 +334,24 @@
|
*/

Route::get('/import', Importer::class)
->middleware('auth')
->name('imports.index')
->breadcrumbs(fn (Trail $trail) =>
$trail->parent('home')
->push(trans('general.import'), route('imports.index')));
Route::group(['prefix' => 'import', 'middleware' => ['auth']], function () {

Route::get('download/{import}',
[
UploadedFilesController::class,
'downloadImport'
]
)->name('imports.download');

Route::get('/', Importer::class)
->middleware('auth')
->name('imports.index')
->breadcrumbs(fn (Trail $trail) =>
$trail->parent('home')
->push(trans('general.import'), route('imports.index')));

});


/*
|--------------------------------------------------------------------------
Expand Down
Loading