Skip to content

A free open source IT asset/license management system

Skip counts for top box on dashboard if the user isn’t an admin or higher

snipe9e252769e967
diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php
index 5a1b554056..8456226165 100755
--- a/app/Http/Controllers/DashboardController.php
+++ b/app/Http/Controllers/DashboardController.php
@@ -12,6 +12,7 @@ use App\Models\License;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Artisan;
+use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Session;
/**
@@ -25,7 +26,7 @@ use Illuminate\Support\Facades\Session;
class DashboardController extends Controller
{
/**
- * Check authorization and display admin dashboard, otherwise display
+ * Check authorization and display the dashboard, otherwise display
* the user's checked-out assets.
*
* @author [A. Gianotto] [<[email protected]>]
@@ -34,11 +35,19 @@ class DashboardController extends Controller
*/
public function index(): View|RedirectResponse
{
- // Show the page
- if (auth()->user()->hasAccess('admin')) {
- $asset_stats = null;
+ if (! Gate::allows('canViewUsersAndCheckoutables')) {
+ Session::reflash();
+
+ return Helper::safeIntended('account/view-assets');
+ }
- $counts = [];
+ // Top-boxes + empty-inventory shortcut are admin-only in the
+ // dashboard blade, so the six count queries only need to run
+ // for admins. Non-admins get an empty array and the view's
+ // hasAccess('admin') gates short-circuit before touching any
+ // $counts key.
+ $counts = [];
+ if (auth()->user()->hasAccess('admin')) {
$counts['asset'] = Asset::count();
$counts['accessory'] = Accessory::count();
$counts['license'] = License::assetcount();
@@ -46,20 +55,15 @@ class DashboardController extends Controller
$counts['component'] = Component::count();
$counts['user'] = Company::scopeCompanyables(auth()->user())->count();
$counts['grand_total'] = $counts['asset'] + $counts['accessory'] + $counts['license'] + $counts['consumable'];
+ }
- if ((! file_exists(storage_path().'/oauth-private.key')) || (! file_exists(storage_path().'/oauth-public.key'))) {
- Artisan::call('migrate', ['--force' => true]);
- Artisan::call('passport:install', ['--no-interaction' => true]);
- }
-
- return view('dashboard')
- ->with('asset_stats', $asset_stats)
- ->with('counts', $counts);
- } else {
- Session::reflash();
-
- // Redirect to the profile page
- return Helper::safeIntended('account/view-assets');
+ if ((! file_exists(storage_path().'/oauth-private.key')) || (! file_exists(storage_path().'/oauth-public.key'))) {
+ Artisan::call('migrate', ['--force' => true]);
+ Artisan::call('passport:install', ['--no-interaction' => true]);
}
+
+ return view('dashboard')
+ ->with('asset_stats', null)
+ ->with('counts', $counts);
}
}