Skip to content

A free open source IT asset/license management system

add checks that the target is not null, and redirects back with error messages

Godfrey M100db2321057
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php
index 0e44fd4705..3a9dc58c82 100755
--- a/app/Http/Controllers/Assets/AssetsController.php
+++ b/app/Http/Controllers/Assets/AssetsController.php
@@ -188,14 +188,31 @@ class AssetsController extends Controller
// Validate the asset before saving
if ($asset->isValid() && $asset->save()) {
- if (request('assigned_user')) {
- $target = User::find(request('assigned_user'));
- $location = $target?->location_id;
- } elseif (request('assigned_asset')) {
- $target = Asset::find(request('assigned_asset'));
- $location = $target?->location_id;
- } elseif (request('assigned_location')) {
- $target = Location::find(request('assigned_location'));
+ $target = null;
+ $location = null;
+
+ if ($userId = request('assigned_user')) {
+ $target = User::find($userId);
+
+ if (!$target) {
+ return redirect()->back()->with('error', trans('admin/hardware/message.create.target_not_found.user'));
+ }
+ $location = $target->location_id;
+
+ } elseif ($assetId = request('assigned_asset')) {
+ $target = Asset::find($assetId);
+
+ if (!$target) {
+ return redirect()->back()->with('error', trans('admin/hardware/message.create.target_not_found.asset'));
+ }
+ $location = $target->location_id;
+
+ } elseif ($locationId = request('assigned_location')) {
+ $target = Location::find($locationId);
+
+ if (!$target) {
+ return redirect()->back()->with('error', trans('admin/hardware/message.create.target_not_found.location'));
+ }
$location = $target->id;
}
diff --git a/resources/lang/en-US/admin/hardware/message.php b/resources/lang/en-US/admin/hardware/message.php
index c19bc63647..4421fadaee 100644
--- a/resources/lang/en-US/admin/hardware/message.php
+++ b/resources/lang/en-US/admin/hardware/message.php
@@ -19,6 +19,11 @@ return [
'success_linked' => 'Asset with tag :tag was created successfully. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.',
'multi_success_linked' => 'Asset with tag :links was created successfully.|:count assets were created succesfully. :links.',
'partial_failure' => 'An asset was unable to be created. Reason: :failures|:count assets were unable to be created. Reasons: :failures',
+ 'target_not_found' => [
+ 'user' => 'The assigned user could not be found.',
+ 'asset' => 'The assigned asset could not be found.',
+ 'location' => 'The assigned location could not be found.',
+ ],
],
'update' => [