From f8311815eea930f70b91205f683cebb7f145ffe8 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Dec 2024 10:17:45 +0000 Subject: [PATCH 1/3] Check for valid asset before accessing properties Signed-off-by: snipe --- .../Transformers/AssetMaintenancesTransformer.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Http/Transformers/AssetMaintenancesTransformer.php b/app/Http/Transformers/AssetMaintenancesTransformer.php index 81b4a9eabbbe..7e8da002480c 100644 --- a/app/Http/Transformers/AssetMaintenancesTransformer.php +++ b/app/Http/Transformers/AssetMaintenancesTransformer.php @@ -26,17 +26,18 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance) 'id' => (int) $assetmaintenance->id, 'asset' => ($assetmaintenance->asset) ? [ 'id' => (int) $assetmaintenance->asset->id, - 'name'=> ($assetmaintenance->asset->name) ? e($assetmaintenance->asset->name) : null, + 'name'=> (($assetmaintenance->asset) && ($assetmaintenance->asset->name)) ? e($assetmaintenance->asset->name) : null, 'asset_tag'=> e($assetmaintenance->asset->asset_tag), 'serial'=> e($assetmaintenance->asset->serial), - 'deleted_at'=> e($assetmaintenance->asset->deleted_at), - 'created_at'=> e($assetmaintenance->asset->created_at), + 'deleted_at'=> ($assetmaintenance->asset) ? Helper::getFormattedDateObject($assetmaintenance->asset->deleted_at, 'datetime') : null, + 'created_at' => ($assetmaintenance->asset) ? Helper::getFormattedDateObject($assetmaintenance->asset->created_at, 'datetime') : null, + 'updated_at' => ($assetmaintenance->asset) ? Helper::getFormattedDateObject($assetmaintenance->asset->updated_at, 'datetime') : null, ] : null, 'model' => (($assetmaintenance->asset) && ($assetmaintenance->asset->model)) ? [ 'id' => (int) $assetmaintenance->asset->model->id, 'name'=> ($assetmaintenance->asset->model->name) ? e($assetmaintenance->asset->model->name).' '.e($assetmaintenance->asset->model->model_number) : null, ] : null, - 'status_label' => ($assetmaintenance->asset->assetstatus) ? [ + 'status_label' => (($assetmaintenance->asset) && ($assetmaintenance->asset->assetstatus)) ? [ 'id' => (int) $assetmaintenance->asset->assetstatus->id, 'name'=> e($assetmaintenance->asset->assetstatus->name), 'status_type'=> e($assetmaintenance->asset->assetstatus->getStatuslabelType()), @@ -79,7 +80,7 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance) ]; $permissions_array['available_actions'] = [ - 'update' => (Gate::allows('update', Asset::class) && ($assetmaintenance->asset->deleted_at=='')) ? true : false, + 'update' => (Gate::allows('update', Asset::class) && ((($assetmaintenance->asset) && $assetmaintenance->asset->deleted_at==''))) ? true : false, 'delete' => Gate::allows('delete', Asset::class), ]; From b38f9ad33cf7a6a6b639f8788234bc1d1d202e83 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Dec 2024 10:18:00 +0000 Subject: [PATCH 2/3] Updated return types Signed-off-by: snipe --- app/Http/Controllers/Api/AssetMaintenancesController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php index 3e02a56195a1..b4e9b441960b 100644 --- a/app/Http/Controllers/Api/AssetMaintenancesController.php +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -122,7 +122,7 @@ public function index(Request $request) : JsonResponse | array * @version v1.0 * @since [v1.8] */ - public function store(Request $request) : JsonResponse + public function store(Request $request) : JsonResponse | array { $this->authorize('update', Asset::class); // create a new model instance @@ -149,7 +149,7 @@ public function store(Request $request) : JsonResponse * @version v1.0 * @since [v4.0] */ - public function update(Request $request, $id) : JsonResponse + public function update(Request $request, $id) : JsonResponse | array { $this->authorize('update', Asset::class); @@ -186,7 +186,7 @@ public function update(Request $request, $id) : JsonResponse * @version v1.0 * @since [v4.0] */ - public function destroy($assetMaintenanceId) : JsonResponse + public function destroy($assetMaintenanceId) : JsonResponse | array { $this->authorize('update', Asset::class); // Check if the asset maintenance exists @@ -208,7 +208,7 @@ public function destroy($assetMaintenanceId) : JsonResponse * @version v1.0 * @since [v4.0] */ - public function show($assetMaintenanceId) : JsonResponse + public function show($assetMaintenanceId) : JsonResponse | array { $this->authorize('view', Asset::class); $assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId); From d410f168bd63db6707977328c0e5447614b71ad5 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 26 Dec 2024 10:28:27 +0000 Subject: [PATCH 3/3] Removed extra checks, since we already check higher up Signed-off-by: snipe --- app/Http/Transformers/AssetMaintenancesTransformer.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Http/Transformers/AssetMaintenancesTransformer.php b/app/Http/Transformers/AssetMaintenancesTransformer.php index 7e8da002480c..ab044260f782 100644 --- a/app/Http/Transformers/AssetMaintenancesTransformer.php +++ b/app/Http/Transformers/AssetMaintenancesTransformer.php @@ -26,12 +26,12 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance) 'id' => (int) $assetmaintenance->id, 'asset' => ($assetmaintenance->asset) ? [ 'id' => (int) $assetmaintenance->asset->id, - 'name'=> (($assetmaintenance->asset) && ($assetmaintenance->asset->name)) ? e($assetmaintenance->asset->name) : null, + 'name'=> ($assetmaintenance->asset->name) ? e($assetmaintenance->asset->name) : null, 'asset_tag'=> e($assetmaintenance->asset->asset_tag), 'serial'=> e($assetmaintenance->asset->serial), - 'deleted_at'=> ($assetmaintenance->asset) ? Helper::getFormattedDateObject($assetmaintenance->asset->deleted_at, 'datetime') : null, - 'created_at' => ($assetmaintenance->asset) ? Helper::getFormattedDateObject($assetmaintenance->asset->created_at, 'datetime') : null, - 'updated_at' => ($assetmaintenance->asset) ? Helper::getFormattedDateObject($assetmaintenance->asset->updated_at, 'datetime') : null, + 'deleted_at'=> Helper::getFormattedDateObject($assetmaintenance->asset->deleted_at, 'datetime'), + 'created_at' => Helper::getFormattedDateObject($assetmaintenance->asset->created_at, 'datetime'), + 'updated_at' => Helper::getFormattedDateObject($assetmaintenance->asset->updated_at, 'datetime'), ] : null, 'model' => (($assetmaintenance->asset) && ($assetmaintenance->asset->model)) ? [ 'id' => (int) $assetmaintenance->asset->model->id,