Files
2026-07-25 18:36:45 +04:00

97 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Concerns\BuildsPortalProps;
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateAccountAccessRequest;
use App\Services\AzerothCoreAccountService;
use App\Services\InviteCodeService;
use App\Support\WowData;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class AccountController extends Controller
{
use BuildsPortalProps;
public function index(
Request $request,
AzerothCoreAccountService $accounts,
InviteCodeService $inviteCodes,
): Response {
$search = trim((string) $request->query('search', ''));
$searchTerm = $search !== '' ? $search : null;
$activeTab = (string) $request->query('tab', AzerothCoreAccountService::ACCOUNT_TAB_PLAYERS);
if (! in_array($activeTab, [AzerothCoreAccountService::ACCOUNT_TAB_PLAYERS, AzerothCoreAccountService::ACCOUNT_TAB_BOTS], true)) {
$activeTab = AzerothCoreAccountService::ACCOUNT_TAB_PLAYERS;
}
$accountStats = $accounts->accountTypeStats($searchTerm);
$accountItems = $accounts->listAccounts($searchTerm, $activeTab)
->map(fn (array $account): array => [
...collect($account)->except('characters')->all(),
'email' => $account['email'] !== '' ? $account['email'] : 'email не указан',
'status_label' => $account['locked'] ? 'Заблокирован' : 'Активен',
'characters' => $account['characters']->values()->all(),
'update_access_url' => route('admin.accounts.access.update', $account['id']),
])
->values()
->all();
$invites = $inviteCodes->latest()
->map(fn ($invite): array => [
'id' => $invite->id,
'code' => $invite->code,
'created_by' => $invite->created_by_username ?: '—',
'created_at' => $invite->created_at?->format('d.m.Y H:i'),
'redeemed' => $invite->redeemed_at !== null,
'redeemed_by' => $invite->redeemed_by_username ?: 'ещё не использован',
])
->values()
->all();
return Inertia::render('Admin/Accounts', [
...$this->portalProps($request),
'accounts' => $accountItems,
'accountStats' => $accountStats,
'accountTabs' => [
AzerothCoreAccountService::ACCOUNT_TAB_PLAYERS => [
'label' => 'Игроки',
'count' => $accountStats[AzerothCoreAccountService::ACCOUNT_TAB_PLAYERS] ?? 0,
],
AzerothCoreAccountService::ACCOUNT_TAB_BOTS => [
'label' => 'Боты',
'count' => $accountStats[AzerothCoreAccountService::ACCOUNT_TAB_BOTS] ?? 0,
],
],
'activeTab' => $activeTab,
'inviteCodes' => $invites,
'inviteStats' => $inviteCodes->stats(),
'inviteStoreUrl' => route('admin.invite-codes.store'),
'registrationRequiresInvite' => (bool) config('moonwell.registration.require_invite_code'),
'search' => $search,
'accessLevels' => collect(WowData::securityLevels())
->map(fn (string $label, int $value): array => compact('value', 'label'))
->values()
->all(),
'old' => [
'quantity' => $request->old('quantity', 1),
],
]);
}
public function updateAccess(int $accountId, UpdateAccountAccessRequest $request, AzerothCoreAccountService $accounts): RedirectResponse
{
abort_unless($accounts->findUserById($accountId), 404);
$accounts->updateAccessLevel($accountId, (int) $request->input('gmlevel'));
return back()->with('status', 'Уровень доступа обновлён.');
}
}