record instanceof Tenant ? $this->record : null; } public function scanNow(): void { $user = auth()->user(); if (! $user instanceof User) { abort(403); } $tenant = $this->resolveTenant(); if (! $tenant instanceof Tenant) { abort(404); } if (! $user->canAccessTenant($tenant)) { abort(404); } if (! $user->can(Capabilities::ENTRA_ROLES_MANAGE, $tenant)) { abort(403); } ScanEntraAdminRolesJob::dispatch( tenantId: (int) $tenant->getKey(), workspaceId: (int) $tenant->workspace_id, initiatorUserId: (int) $user->getKey(), ); Notification::make() ->title('Entra admin roles scan queued') ->body('The scan will run in the background. Results appear once complete.') ->success() ->send(); } /** * @return array */ protected function getViewData(): array { $tenant = $this->resolveTenant(); if (! $tenant instanceof Tenant) { return $this->emptyState(); } $user = auth()->user(); $isTenantMember = $user instanceof User && $user->canAccessTenant($tenant); $canView = $isTenantMember && $user->can(Capabilities::ENTRA_ROLES_VIEW, $tenant); $canManage = $isTenantMember && $user->can(Capabilities::ENTRA_ROLES_MANAGE, $tenant); $report = StoredReport::query() ->where('tenant_id', (int) $tenant->getKey()) ->where('report_type', StoredReport::REPORT_TYPE_ENTRA_ADMIN_ROLES) ->orderByDesc('created_at') ->first(); if (! $report instanceof StoredReport) { return [ 'tenant' => $tenant, 'reportSummary' => null, 'lastScanAt' => null, 'highPrivilegeCount' => 0, 'canManage' => $canManage, 'canView' => $canView, 'viewReportUrl' => null, ]; } $payload = is_array($report->payload) ? $report->payload : []; $totals = is_array($payload['totals'] ?? null) ? $payload['totals'] : []; $highPrivilegeCount = (int) ($totals['high_privilege_assignments'] ?? 0); return [ 'tenant' => $tenant, 'reportSummary' => $totals, 'lastScanAt' => $report->created_at?->diffForHumans() ?? '—', 'highPrivilegeCount' => $highPrivilegeCount, 'canManage' => $canManage, 'canView' => $canView, 'viewReportUrl' => null, ]; } /** * @return array */ private function emptyState(): array { return [ 'tenant' => null, 'reportSummary' => null, 'lastScanAt' => null, 'highPrivilegeCount' => 0, 'canManage' => false, 'canView' => false, 'viewReportUrl' => null, ]; } }