header('X-Tenant')) { $tenantId = $request->header('X-Tenant'); } if ($tenantId) { $isModuleEnabled = CentralTenantModule::where('tenant_id', $tenantId) ->where('module_key', $moduleKey) ->where('enabled', true) ->exists(); // If central tenant_modules table is empty for this tenant, default to allowing or fallback $hasAnyModulesConfigured = CentralTenantModule::where('tenant_id', $tenantId)->exists(); if ($hasAnyModulesConfigured && !$isModuleEnabled) { if ($request->wantsJson() || $request->is('api/*')) { return response()->json([ 'success' => false, 'message' => "The '{$moduleKey}' module is not enabled for your company subscription.", ], 403); } abort(403, "The '{$moduleKey}' module is not enabled for your company subscription."); } } return $next($request); } }