Files
ladill-hosting/app/Http/Controllers/Hosting/SearchController.php
T
isaaccladandClaude Opus 4.8 107013bd04
Deploy Ladill Hosting / deploy (push) Successful in 23s
Hosting dev-access: resolve via central identity API (shadow mode)
Consolidate the inline HostingAccountMember lookups (Overview/Search/Hosting
Product/Afia controllers) into HostingAccessResolver, which can source hosting
developer-access from the monolith's central /identity/hosting/developer-access
(correlating cPanel usernames to local account ids). Shadow mode by default
(HOSTING_CENTRAL_DEV_ACCESS=false): local hosting_account_members stays
authoritative and divergence from central is logged; flip the flag to cut over.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 23:42:08 +00:00

106 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers\Hosting;
use App\Http\Controllers\Controller;
use App\Models\CustomerHostingOrder;
use App\Models\HostingAccount;
use App\Models\HostingProduct;
use App\Services\Hosting\HostingAccessResolver;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class SearchController extends Controller
{
public function __invoke(Request $request): JsonResponse|View
{
$q = trim((string) $request->query('q', ''));
$results = mb_strlen($q) >= 2 ? $this->resultsForUser($request->user(), $q) : [];
if ($request->expectsJson() || $request->ajax() || $request->wantsJson()) {
return response()->json(['results' => $results]);
}
return view('search', [
'query' => $q,
'results' => $results,
]);
}
/** @return list<array{type:string,title:string,subtitle:string,url:string}> */
private function resultsForUser($user, string $q): array
{
$like = '%'.$q.'%';
$results = [];
$seenUrls = [];
$push = function (array $item) use (&$results, &$seenUrls): void {
if (isset($seenUrls[$item['url']])) {
return;
}
$seenUrls[$item['url']] = true;
$results[] = $item;
};
$sharedTypes = [
HostingProduct::TYPE_SINGLE_DOMAIN,
HostingProduct::TYPE_MULTI_DOMAIN,
HostingProduct::TYPE_WORDPRESS,
];
$sharedAccountIds = app(HostingAccessResolver::class)->sharedAccountIds($user);
HostingAccount::query()
->where(function ($query) use ($user, $sharedAccountIds) {
$query->where('user_id', $user->id);
if ($sharedAccountIds->isNotEmpty()) {
$query->orWhereIn('id', $sharedAccountIds);
}
})
->whereHas('product', fn ($query) => $query->whereIn('type', $sharedTypes))
->where(function ($query) use ($like) {
$query->where('username', 'like', $like)
->orWhere('primary_domain', 'like', $like)
->orWhereHas('sites', fn ($sites) => $sites->where('domain', 'like', $like));
})
->with(['product', 'sites'])
->orderByDesc('updated_at')
->limit(8)
->get()
->each(function (HostingAccount $account) use ($push, $user): void {
$sharedDeveloperAccess = (int) $account->user_id !== (int) $user->id;
$push([
'type' => 'hosting',
'title' => $account->linkedDomainLabel() ?? $account->username,
'subtitle' => ($account->product?->name ?? 'Hosting').' · '.ucfirst((string) $account->status),
'url' => $sharedDeveloperAccess
? route('hosting.panel.index', $account)
: route('hosting.accounts.show', $account),
]);
});
CustomerHostingOrder::query()
->forUser($user->id)
->whereHas('product', fn ($query) => $query->whereIn('type', $sharedTypes))
->where(function ($query) use ($like) {
$query->where('domain_name', 'like', $like)
->orWhere('server_ip', 'like', $like);
})
->with('product')
->orderByDesc('updated_at')
->limit(5)
->get()
->each(function (CustomerHostingOrder $order) use ($push): void {
$push([
'type' => 'order',
'title' => $order->domain_name ?: ($order->product?->name ?? 'Hosting order #'.$order->id),
'subtitle' => 'Hosting order · '.ucwords(str_replace('_', ' ', (string) $order->status)),
'url' => route('hosting.orders.show', $order),
]);
});
return array_slice($results, 0, 12);
}
}