Skip to content

A free open source IT asset/license management system

Fixed 464q-gjj5: tighter checks on low inventory alert bell

snipe46b0b0d44b51
diff --git a/app/Http/Controllers/Api/LowStockController.php b/app/Http/Controllers/Api/LowStockController.php
index 301a8355b0..88b874dc53 100644
--- a/app/Http/Controllers/Api/LowStockController.php
+++ b/app/Http/Controllers/Api/LowStockController.php
@@ -35,20 +35,6 @@ class LowStockController extends Controller
*/
private const SORTABLE_COLUMNS = ['name', 'type', 'qty', 'min_amt', 'remaining', 'percent'];
- /**
- * Plural type discriminator from Helper::checkLowInventory() rows
- * mapped to the model class the row represents. Used to filter
- * results by per-type view permission so an accessory-only viewer
- * doesn't see consumable / component rows through this widget.
- */
- private const TYPE_TO_CLASS = [
- 'consumables' => Consumable::class,
- 'accessories' => Accessory::class,
- 'components' => Component::class,
- 'models' => AssetModel::class,
- 'licenses' => License::class,
- ];
-
public function index(Request $request): JsonResponse|array
{
// Base access gate. Anyone who can view any of the source types
@@ -66,18 +52,12 @@ class LowStockController extends Controller
abort(403);
}
- $rows = Helper::checkLowInventory();
-
// Filter rows to types the caller can view so a viewer with
// only e.g. accessories.view doesn't get consumable / component
- // rows leaked through the shared widget endpoint. Rows whose
- // plural type isn't in the map (future addition) fall through
- // gated on nothing, matching pre-filter behavior.
- $rows = array_values(array_filter(
- $rows,
- fn ($row) => ! isset(self::TYPE_TO_CLASS[$row['type']])
- || $viewer->can('view', self::TYPE_TO_CLASS[$row['type']]),
- ));
+ // rows leaked through the shared widget endpoint. Shared with
+ // AlertMenu so both surfaces agree on which rows a given user
+ // can see.
+ $rows = LowStockTransformer::viewableByCaller(Helper::checkLowInventory(), $viewer);
$search = trim((string) $request->input('search', ''));
if ($search !== '') {
diff --git a/app/Http/Transformers/LowStockTransformer.php b/app/Http/Transformers/LowStockTransformer.php
index 6b4a126ea9..952f3b3f64 100644
--- a/app/Http/Transformers/LowStockTransformer.php
+++ b/app/Http/Transformers/LowStockTransformer.php
@@ -2,11 +2,13 @@
namespace App\Http\Transformers;
+use App\Http\Controllers\Controller;
use App\Models\Accessory;
use App\Models\AssetModel;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\License;
+use App\Models\User;
use Illuminate\Support\Facades\Gate;
/**
@@ -121,4 +123,30 @@ class LowStockTransformer
License::class,
];
}
+
+ /**
+ * Drop rows from a Helper::checkLowInventory() result the given
+ * viewer can't `view` per the model's policy. A viewer with only
+ * accessories.view sees only accessory rows. Null viewer sees
+ * nothing. The plural type-to-class map lives on Controller as
+ * $map_object_type (shared across the app). Rows whose plural
+ * type isn't in that map fall through gated on nothing, matching
+ * pre-filter behavior.
+ *
+ * @param array<int, array{type: string}> $rows
+ * @return array<int, array<string, mixed>>
+ */
+ public static function viewableByCaller(array $rows, ?User $viewer): array
+ {
+ if ($viewer === null) {
+ return [];
+ }
+
+ $map = Controller::$map_object_type;
+
+ return array_values(array_filter(
+ $rows,
+ fn ($row) => ! isset($map[$row['type']]) || $viewer->can('view', $map[$row['type']]),
+ ));
+ }
}
diff --git a/app/Livewire/AlertMenu.php b/app/Livewire/AlertMenu.php
index eda1d36686..b8d93f6b3b 100644
--- a/app/Livewire/AlertMenu.php
+++ b/app/Livewire/AlertMenu.php
@@ -3,6 +3,8 @@
namespace App\Livewire;
use App\Helpers\Helper;
+use App\Http\Transformers\LowStockTransformer;
+use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Livewire\Attributes\Lazy;
use Livewire\Component;
@@ -54,8 +56,22 @@ class AlertMenu extends Component
public function render(): View
{
- $alert_items = Helper::checkLowInventory();
- $deprecations = Helper::deprecationCheck();
+ // Filter the shared low-inventory result to the categories this
+ // caller can view. Reuses the per-row filter the LowStock API
+ // uses so the bell and the dashboard widget never disagree on
+ // what a given user can see. Admins who can view no source
+ // type at all get an empty bell rather than the component
+ // throwing - this is a nav widget that mounts on every page.
+ $alert_items = LowStockTransformer::viewableByCaller(
+ Helper::checkLowInventory(),
+ auth()->user(),
+ );
+
+ // Deprecation notices leak operator config (currently a
+ // webhook-provider heads-up for admins to action), so gate on
+ // the admin policy. Normal users don't see the notice even
+ // when the admin's webhook config matches.
+ $deprecations = Gate::allows('admin') ? Helper::deprecationCheck() : [];
return view('livewire.alert-menu', [
'alert_items' => $alert_items,
diff --git a/tests/Feature/Livewire/AlertMenuTest.php b/tests/Feature/Livewire/AlertMenuTest.php
index b6a61d5c7e..4d7bdc4288 100644
--- a/tests/Feature/Livewire/AlertMenuTest.php
+++ b/tests/Feature/Livewire/AlertMenuTest.php
@@ -3,6 +3,9 @@
namespace Tests\Feature\Livewire;
use App\Livewire\AlertMenu;
+use App\Models\Accessory;
+use App\Models\Consumable;
+use App\Models\Setting;
use App\Models\User;
use Livewire\Livewire;
use Tests\TestCase;
@@ -16,6 +19,59 @@ class AlertMenuTest extends TestCase
->assertStatus(200);
}
+ public function test_normal_user_without_inventory_permissions_sees_no_low_stock_rows(): void
+ {
+ // Regression test for AlertMenu disclosure.
+ Accessory::factory()->create([
+ 'name' => 'CAND06-LOW-STOCK-SECRET',
+ 'qty' => 1,
+ 'min_amt' => 2,
+ ]);
+
+ Livewire::withoutLazyLoading()
+ ->actingAs(User::factory()->create())
+ ->test(AlertMenu::class)
+ ->assertSet('alert_count', 0)
+ ->assertDontSee('CAND06-LOW-STOCK-SECRET');
+ }
+
+ public function test_user_with_only_accessories_view_sees_only_accessory_rows(): void
+ {
+ Accessory::factory()->create([
+ 'name' => 'ALERTBELL-ACCESSORY',
+ 'qty' => 1,
+ 'min_amt' => 2,
+ ]);
+ Consumable::factory()->create([
+ 'name' => 'ALERTBELL-CONSUMABLE',
+ 'qty' => 1,
+ 'min_amt' => 2,
+ ]);
+
+ Livewire::withoutLazyLoading()
+ ->actingAs(User::factory()->viewAccessories()->create())
+ ->test(AlertMenu::class)
+ ->assertSee('ALERTBELL-ACCESSORY')
+ ->assertDontSee('ALERTBELL-CONSUMABLE');
+ }
+
+ public function test_normal_user_does_not_see_ms_teams_deprecation_notice(): void
+ {
+ // The MS Teams webhook-deprecation message leaks the admin's
+ // webhook_selected configuration. Gate on admin so a normal
+ // user doesn't learn which provider the admin configured.
+ $settings = Setting::getSettings();
+ $settings->webhook_selected = 'microsoft';
+ $settings->webhook_endpoint = 'https://outlook.office.com/webhook/legacy-non-workflows';
+ $settings->save();
+
+ Livewire::withoutLazyLoading()
+ ->actingAs(User::factory()->create())
+ ->test(AlertMenu::class)
+ ->assertDontSee('workflows')
+ ->assertDontSee('deprecated');
+ }
+
public function test_placeholder_reserves_bell_footprint(): void
{
$placeholder = (new AlertMenu)->placeholder();