Skip to content

A free open source IT asset/license management system

Handle redirects for better UX

snipe138d212c504c
diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php
index 45636b17da..bf1b008d0a 100755
--- a/app/Http/Controllers/Accessories/AccessoriesController.php
+++ b/app/Http/Controllers/Accessories/AccessoriesController.php
@@ -275,12 +275,10 @@ class AccessoriesController extends Controller
return redirect()->back()->with('error', $error);
}
- // Land on the item's history tab so the operator sees the log
- // entry they just wrote as confirmation. The `history` fragment
- // matches x-tabs.history-tab's `name="history"`.
- return redirect()
- ->route('accessories.show', $accessory)
- ->withFragment('history')
- ->with('success', trans('general.adjust_quantity_success'));
+ // adjustQuantityRedirect keeps show-page opens landing on
+ // #history for the confirmation loop, and sends index-page
+ // opens back to the index so bulk adjust flows aren't broken
+ // by a forced navigation to the item detail page.
+ return $this->adjustQuantityRedirect($request, $accessory, route('accessories.show', $accessory));
}
}
diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php
index 8352c167e6..8f002c2205 100644
--- a/app/Http/Controllers/Components/ComponentsController.php
+++ b/app/Http/Controllers/Components/ComponentsController.php
@@ -271,9 +271,8 @@ class ComponentsController extends Controller
return redirect()->back()->with('error', $error);
}
- return redirect()
- ->route('components.show', $component)
- ->withFragment('history')
- ->with('success', trans('general.adjust_quantity_success'));
+ // See AccessoriesController::adjustQuantity — helper picks
+ // between show-page-#history and back-to-referer.
+ return $this->adjustQuantityRedirect($request, $component, route('components.show', $component));
}
}
diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php
index 05dc66ae47..1d1eeb1c29 100644
--- a/app/Http/Controllers/Consumables/ConsumablesController.php
+++ b/app/Http/Controllers/Consumables/ConsumablesController.php
@@ -273,9 +273,8 @@ class ConsumablesController extends Controller
return redirect()->back()->with('error', $error);
}
- return redirect()
- ->route('consumables.show', $consumable)
- ->withFragment('history')
- ->with('success', trans('general.adjust_quantity_success'));
+ // See AccessoriesController::adjustQuantity — helper picks
+ // between show-page-#history and back-to-referer.
+ return $this->adjustQuantityRedirect($request, $consumable, route('consumables.show', $consumable));
}
}
diff --git a/app/Http/Traits/HandlesAdjustQuantity.php b/app/Http/Traits/HandlesAdjustQuantity.php
index 737144c945..c2af00019b 100644
--- a/app/Http/Traits/HandlesAdjustQuantity.php
+++ b/app/Http/Traits/HandlesAdjustQuantity.php
@@ -2,6 +2,7 @@
namespace App\Http\Traits;
+use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\AdjustQuantityRequest;
use App\Http\Requests\UploadFileRequest;
@@ -9,6 +10,7 @@ use App\Models\Order;
use App\Models\OrderItem;
use DomainException;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
/**
@@ -59,6 +61,28 @@ trait HandlesAdjustQuantity
return null;
}
+ /**
+ * Land the post-save redirect on the page the operator was on when
+ * they opened the modal. From the item's show page → the show page
+ * with the `#history` fragment so the newly-written log entry is
+ * visible as confirmation. From an index / listing page → back to
+ * that listing so bulk-adjust flows don't force the operator to
+ * back out and re-navigate for every item. Referer is validated
+ * as same-origin to prevent open-redirect abuse; missing or
+ * cross-origin referers fall back to the item show page.
+ */
+ protected function adjustQuantityRedirect(Request $request, Model $model, string $itemShowUrl): RedirectResponse
+ {
+ $referer = Helper::sameOriginUrl($request->headers->get('referer'));
+ $success = trans('general.adjust_quantity_success');
+
+ if ($referer && $referer !== $itemShowUrl && ! str_starts_with($referer, $itemShowUrl.'#')) {
+ return redirect()->to($referer)->with('success', $success);
+ }
+
+ return redirect()->to($itemShowUrl)->withFragment('history')->with('success', $success);
+ }
+
/**
* Find or create an Order from the request payload and append one
* OrderItem line for the model / delta being adjusted. Returns the