mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
delete done
This commit is contained in:
parent
698434dcf8
commit
c0ea1fbe78
|
@ -2,11 +2,37 @@
|
||||||
|
|
||||||
namespace App\Actions\Assets;
|
namespace App\Actions\Assets;
|
||||||
|
|
||||||
|
use App\Events\CheckoutableCheckedIn;
|
||||||
|
use App\Models\Asset;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
class DestroyAssetAction
|
class DestroyAssetAction
|
||||||
{
|
{
|
||||||
public static function run()
|
public static function run(Asset $asset)
|
||||||
{
|
{
|
||||||
|
if ($asset->assignedTo) {
|
||||||
|
|
||||||
|
$target = $asset->assignedTo;
|
||||||
|
$checkin_at = date('Y-m-d H:i:s');
|
||||||
|
$originalValues = $asset->getRawOriginal();
|
||||||
|
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on delete', $checkin_at, $originalValues));
|
||||||
|
DB::table('assets')
|
||||||
|
->where('id', $asset->id)
|
||||||
|
->update(['assigned_to' => null]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ($asset->image) {
|
||||||
|
try {
|
||||||
|
Storage::disk('public')->delete('assets'.'/'.$asset->image);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::debug($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$asset->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Exceptions;
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
@ -73,6 +74,14 @@ class Handler extends ExceptionHandler
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'Invalid JSON'), 422);
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Invalid JSON'), 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if ($e instanceof ModelNotFoundException) {
|
||||||
|
// $class = get_class($e);
|
||||||
|
// match($class) {
|
||||||
|
//
|
||||||
|
// };
|
||||||
|
// return redirect()->route()->with('error', trans('general.model_not_found', ['model' => $e]));
|
||||||
|
//}
|
||||||
|
|
||||||
// Handle SCIM exceptions
|
// Handle SCIM exceptions
|
||||||
if ($e instanceof SCIMException) {
|
if ($e instanceof SCIMException) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Actions\Assets\DestroyAssetAction;
|
||||||
use App\Actions\Assets\StoreAssetAction;
|
use App\Actions\Assets\StoreAssetAction;
|
||||||
use App\Events\CheckoutableCheckedIn;
|
use App\Events\CheckoutableCheckedIn;
|
||||||
use App\Exceptions\CheckoutNotAllowed;
|
use App\Exceptions\CheckoutNotAllowed;
|
||||||
|
@ -735,30 +736,18 @@ class AssetsController extends Controller
|
||||||
* @param int $assetId
|
* @param int $assetId
|
||||||
* @since [v4.0]
|
* @since [v4.0]
|
||||||
*/
|
*/
|
||||||
public function destroy($id) : JsonResponse
|
public function destroy(Asset $asset): JsonResponse
|
||||||
{
|
{
|
||||||
$this->authorize('delete', Asset::class);
|
//this is probably wrong
|
||||||
|
//$this->authorize('delete', Asset::class);
|
||||||
if ($asset = Asset::find($id)) {
|
$this->authorize('delete', $asset);
|
||||||
$this->authorize('delete', $asset);
|
try {
|
||||||
|
DestroyAssetAction::run($asset);
|
||||||
if ($asset->assignedTo) {
|
|
||||||
|
|
||||||
$target = $asset->assignedTo;
|
|
||||||
$checkin_at = date('Y-m-d H:i:s');
|
|
||||||
$originalValues = $asset->getRawOriginal();
|
|
||||||
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on delete', $checkin_at, $originalValues));
|
|
||||||
DB::table('assets')
|
|
||||||
->where('id', $asset->id)
|
|
||||||
->update(['assigned_to' => null]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$asset->delete();
|
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.delete.success')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.delete.success')));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
report($e);
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'something went wrong: '));
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,18 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers\Assets;
|
namespace App\Http\Controllers\Assets;
|
||||||
|
|
||||||
|
use App\Actions\Assets\DestroyAssetAction;
|
||||||
use App\Actions\Assets\StoreAssetAction;
|
use App\Actions\Assets\StoreAssetAction;
|
||||||
use App\Events\CheckoutableCheckedIn;
|
use App\Events\CheckoutableCheckedIn;
|
||||||
use App\Exceptions\CheckoutNotAllowed;
|
use App\Exceptions\CheckoutNotAllowed;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\Assets\DestroyAssetRequest;
|
||||||
use App\Http\Requests\ImageUploadRequest;
|
use App\Http\Requests\ImageUploadRequest;
|
||||||
use App\Http\Requests\Assets\StoreAssetRequest;
|
use App\Http\Requests\Assets\StoreAssetRequest;
|
||||||
use App\Models\Actionlog;
|
use App\Models\Actionlog;
|
||||||
use App\Http\Requests\UploadFileRequest;
|
use App\Http\Requests\UploadFileRequest;
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use App\Models\AssetModel;
|
use App\Models\AssetModel;
|
||||||
|
@ -107,7 +110,7 @@ class AssetsController extends Controller
|
||||||
$custom_fields = $request->collect()->filter(function ($value, $key) {
|
$custom_fields = $request->collect()->filter(function ($value, $key) {
|
||||||
return starts_with($key, '_snipeit_');
|
return starts_with($key, '_snipeit_');
|
||||||
});
|
});
|
||||||
|
|
||||||
//DB::transaction(function () use ($request, $asset_tags, $serials, $custom_fields) {
|
//DB::transaction(function () use ($request, $asset_tags, $serials, $custom_fields) {
|
||||||
foreach ($asset_tags as $key => $asset_tag) {
|
foreach ($asset_tags as $key => $asset_tag) {
|
||||||
$asset = StoreAssetAction::run(
|
$asset = StoreAssetAction::run(
|
||||||
|
@ -360,39 +363,16 @@ class AssetsController extends Controller
|
||||||
* @param int $assetId
|
* @param int $assetId
|
||||||
* @since [v1.0]
|
* @since [v1.0]
|
||||||
*/
|
*/
|
||||||
public function destroy(Request $request, $assetId) : RedirectResponse
|
public function destroy(Asset $asset): RedirectResponse
|
||||||
{
|
{
|
||||||
// Check if the asset exists
|
|
||||||
if (is_null($asset = Asset::find($assetId))) {
|
|
||||||
// Redirect to the asset management page with error
|
|
||||||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->authorize('delete', $asset);
|
$this->authorize('delete', $asset);
|
||||||
|
try {
|
||||||
if ($asset->assignedTo) {
|
DestroyAssetAction::run($asset);
|
||||||
|
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.delete.success'));
|
||||||
$target = $asset->assignedTo;
|
} catch (\Exception $e) {
|
||||||
$checkin_at = date('Y-m-d H:i:s');
|
report($e);
|
||||||
$originalValues = $asset->getRawOriginal();
|
return redirect()->back()->withInput()->withErrors($e->getMessage());
|
||||||
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on delete', $checkin_at, $originalValues));
|
|
||||||
DB::table('assets')
|
|
||||||
->where('id', $asset->id)
|
|
||||||
->update(['assigned_to' => null]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($asset->image) {
|
|
||||||
try {
|
|
||||||
Storage::disk('public')->delete('assets'.'/'.$asset->image);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
Log::debug($e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$asset->delete();
|
|
||||||
|
|
||||||
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.delete.success'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
29
app/Http/Requests/Assets/DestroyAssetRequest.php
Normal file
29
app/Http/Requests/Assets/DestroyAssetRequest.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Assets;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
|
||||||
|
class DestroyAssetRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return Gate::allows('delete', $this->asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -573,15 +573,16 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
|
||||||
|
|
||||||
Route::put('/hardware/{asset}', [Api\AssetsController::class, 'update'])->name('api.assets.put-update');
|
Route::put('/hardware/{asset}', [Api\AssetsController::class, 'update'])->name('api.assets.put-update');
|
||||||
|
|
||||||
|
Route::delete('/hardware/{asset}', [Api\AssetsController::class, 'destroy'])->name('api.assets.destroy');
|
||||||
|
|
||||||
Route::resource('hardware',
|
Route::resource('hardware',
|
||||||
Api\AssetsController::class,
|
Api\AssetsController::class,
|
||||||
['names' => [
|
['names' => [
|
||||||
'index' => 'api.assets.index',
|
'index' => 'api.assets.index',
|
||||||
'show' => 'api.assets.show',
|
'show' => 'api.assets.show',
|
||||||
'store' => 'api.assets.store',
|
'store' => 'api.assets.store',
|
||||||
'destroy' => 'api.assets.destroy',
|
|
||||||
],
|
],
|
||||||
'except' => ['create', 'edit', 'update'],
|
'except' => ['create', 'edit', 'update', 'destroy'],
|
||||||
'parameters' => ['asset' => 'asset_id'],
|
'parameters' => ['asset' => 'asset_id'],
|
||||||
]
|
]
|
||||||
); // end assets API routes
|
); // end assets API routes
|
||||||
|
|
|
@ -163,6 +163,8 @@ Route::group(
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::delete('/hardware/{asset}', [AssetsController::class, 'destroy'])->name('hardware.destroy');
|
||||||
|
|
||||||
Route::resource('hardware',
|
Route::resource('hardware',
|
||||||
AssetsController::class,
|
AssetsController::class,
|
||||||
[
|
[
|
||||||
|
@ -172,6 +174,7 @@ Route::resource('hardware',
|
||||||
'show' => 'view',
|
'show' => 'view',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
'except' => ['destroy'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Route::get('ht/{any?}',
|
Route::get('ht/{any?}',
|
||||||
|
|
Loading…
Reference in a new issue