Skip to content

A free open source IT asset/license management system

Make getActivityReport report for non-admins and non-reports.view

snipe3730238f69e3
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index 62c63efa8d..8fa1b5f12a 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -271,9 +271,19 @@ class ReportsController extends Controller
*/
public function getActivityReport(): View
{
- $this->authorize('reports.view');
-
- return view('reports/activity');
+ // Two entry points to this page:
+ // - reports.view holders reach it through the main Reports
+ // nav and get the full endpoint (api.activity.index) with
+ // search / filter / sort intact and the CSV export button.
+ // - Scoped viewers (canViewUsersAndCheckoutables but no
+ // activity.view) reach it via the dashboard Recent
+ // Activity widget's View-all button. The blade points
+ // them at api.dashboard.activity (narrow, type-filtered)
+ // and hides admin-shaped UI via $canManageReports.
+ $hasReportsView = Gate::allows('reports.view');
+ abort_unless($hasReportsView || Gate::allows('canViewUsersAndCheckoutables'), 403);
+
+ return view('reports/activity', ['canManageReports' => $hasReportsView]);
}
/**
@@ -286,11 +296,34 @@ class ReportsController extends Controller
public function postActivityReport(Request $request): StreamedResponse
{
ini_set('max_execution_time', 12000);
- $this->authorize('reports.view');
+ // Two entry points, same as getActivityReport() above:
+ // - reports.view holders get the full unfiltered CSV.
+ // - Scoped viewers arriving via the dashboard widget's
+ // View-all + Download can export a CSV of what they'd see
+ // on the page (item_type / target_type they can view).
+ $hasReportsView = Gate::allows('reports.view');
+ abort_unless($hasReportsView || Gate::allows('canViewUsersAndCheckoutables'), 403);
+
+ // Build the viewable-type filter once so the streaming chunk
+ // callback below can add it without recomputing per chunk.
+ // Only applied when the caller lacks reports.view. Mirrors
+ // Api\DashboardController::activity so the CSV export shows
+ // the same row set the page shows for scoped viewers.
+ $viewableTypeFilter = null;
+ if (! $hasReportsView) {
+ $candidateTypes = array_merge(
+ \App\Models\CalendarEvent::sourceModels(),
+ [\App\Models\Accessory::class, \App\Models\Consumable::class, \App\Models\Component::class],
+ );
+ $viewableTypeFilter = array_values(array_filter(
+ $candidateTypes,
+ fn ($class) => Gate::allows('view', $class),
+ ));
+ }
$this->disableDebugbar();
- $response = new StreamedResponse(function () {
+ $response = new StreamedResponse(function () use ($viewableTypeFilter) {
Log::debug('Starting streamed response');
Log::debug('CSV escaping is set to: '.config('app.escape_formulas'));
@@ -322,6 +355,10 @@ class ReportsController extends Controller
Log::debug('Added headers: '.$executionTime);
$actionlogs = Actionlog::with('item', 'user', 'target', 'location', 'adminuser')
+ ->when($viewableTypeFilter !== null, fn ($q) => $q->where(function ($inner) use ($viewableTypeFilter) {
+ $inner->whereIn('item_type', $viewableTypeFilter)
+ ->orWhereIn('target_type', $viewableTypeFilter);
+ }))
->orderBy('created_at', 'DESC')
->chunk(500, function ($actionlogs) use ($handle) {
$executionTime = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
@@ -1365,7 +1402,21 @@ class ReportsController extends Controller
*/
public function getAssetAcceptanceReport($deleted = false): View
{
- $this->authorize('reports.view');
+ // Reports.view still opens the full report with reminder /
+ // delete actions. Non-report checkoutable viewers reach this
+ // page from the dashboard's Needs Attention widget, where an
+ // "Unaccepted acceptances" row is shown to anyone who can
+ // view at least one checkoutable type. Without widening
+ // here, that link 403s for scoped viewers. The list is
+ // filtered per-viewer below (viewable types + FMCS), and
+ // the reminder / delete action buttons stay gated behind
+ // reports.view in the blade so this widening is read-only.
+ $hasReportsView = Gate::allows('reports.view');
+ $viewableAcceptanceTypes = array_filter(
+ [Asset::class, LicenseSeat::class, Accessory::class, Component::class, Consumable::class],
+ fn (string $type) => Gate::allows('view', $type === LicenseSeat::class ? License::class : $type),
+ );
+ abort_unless($hasReportsView || $viewableAcceptanceTypes !== [], 403);
$this->disableDebugbar();
@@ -1393,6 +1444,15 @@ class ReportsController extends Controller
$itemsForReport = $query->get()
->filter(fn ($unaccepted) => $unaccepted->checkoutable)
+ // Type filter for scoped-viewer access. Only show
+ // acceptances whose checkoutable type this viewer can
+ // read. Skipped for reports.view holders since they see
+ // every type on the report by definition (matches
+ // pre-widen behavior). Scoped viewers get their own
+ // types, matching pendingAcceptancesCount in
+ // App\Livewire\NeedsAttention::mount().
+ ->filter(fn ($unaccepted) => $hasReportsView
+ || in_array($unaccepted->checkoutable::class, $viewableAcceptanceTypes, true))
// FMCS scope, mirrors sentAssetAcceptanceReminder + deleteAssetAcceptance.
// CheckoutAcceptance has no company_id column and does not use
// CompanyableTrait / CompanyableChildTrait, so it is not covered
@@ -1404,7 +1464,9 @@ class ReportsController extends Controller
->filter(fn ($unaccepted) => $this->currentUserCanAccessAcceptance($unaccepted))
->map(fn ($unaccepted) => Checkoutable::fromAcceptance($unaccepted));
- return view('reports/unaccepted_assets', compact('itemsForReport', 'showDeleted'));
+ $canManageAcceptances = $hasReportsView;
+
+ return view('reports/unaccepted_assets', compact('itemsForReport', 'showDeleted', 'canManageAcceptances'));
}
/**