Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for valid asset before accessing properties #16003

Merged
merged 3 commits into from
Dec 26, 2024
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
8 changes: 4 additions & 4 deletions app/Http/Controllers/Api/AssetMaintenancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Transformers/AssetMaintenancesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance)
'name'=> ($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'=> 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,
'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()),
Expand Down Expand Up @@ -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),
];

Expand Down
Loading