- Таблица audit_log + middleware для логирования всех API-запросов - Раздел "Журнал" в админке с фильтрами по пользователю/действию - Переход на GLM-5.1 с отключённым reasoning (thinking: disabled) - max_tokens 2000→4096, timeout 60→120с - Домен smeta.kazin.ru → infosmeta.ru во всех файлах Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
281 lines
15 KiB
HTML
281 lines
15 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Админ-панель</title>
|
|
<style>
|
|
:root { --bg: #f5f0eb; --card: #fff; --text: #3a3a3a; --muted: #6b6b6b; --accent: #c4463a; --border: #d4c5b2; --sidebar: #2c2c2c; --sidebar-text: #e0d6cc; --sidebar-active: #c4463a; --green: #2d8a56; }
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body { font-family: 'Inter', -apple-system, sans-serif; background: var(--bg); color: var(--text); display: flex; min-height: 100vh; }
|
|
|
|
/* Sidebar */
|
|
.sidebar { width: 220px; background: var(--sidebar); color: var(--sidebar-text); padding: 1.5rem 0; flex-shrink: 0; }
|
|
.sidebar h1 { font-size: 1rem; padding: 0 1.2rem .8rem; border-bottom: 1px solid #444; margin-bottom: .5rem; }
|
|
.sidebar a { display: block; padding: .6rem 1.2rem; color: var(--sidebar-text); text-decoration: none; font-size: .85rem; border-left: 3px solid transparent; transition: all .15s; }
|
|
.sidebar a:hover { background: rgba(255,255,255,.05); }
|
|
.sidebar a.active { background: rgba(255,255,255,.08); border-left-color: var(--sidebar-active); color: #fff; font-weight: 600; }
|
|
.sidebar .back { margin-top: 1rem; padding-top: .8rem; border-top: 1px solid #444; font-size: .8rem; color: #888; }
|
|
|
|
/* Main */
|
|
.main { flex: 1; padding: 2rem; overflow-y: auto; }
|
|
.page { display: none; }
|
|
.page.active { display: block; }
|
|
h2 { font-size: 1.1rem; margin-bottom: 1rem; }
|
|
table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 8px; overflow: hidden; box-shadow: 0 1px 4px rgba(0,0,0,.06); margin-bottom: 1.5rem; }
|
|
th { background: #ede5db; text-align: left; padding: 8px 12px; font-size: .8rem; font-weight: 600; }
|
|
td { padding: 6px 12px; border-top: 1px solid #eee; font-size: .85rem; }
|
|
a { color: var(--accent); text-decoration: none; }
|
|
.time { color: var(--muted); font-size: .75rem; }
|
|
.q { color: var(--muted); font-size: .8rem; max-width: 350px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.stat-cards { display: flex; gap: 1rem; margin-bottom: 1.5rem; flex-wrap: wrap; }
|
|
.stat-card { background: var(--card); padding: 1rem 1.2rem; border-radius: 8px; box-shadow: 0 1px 4px rgba(0,0,0,.06); min-width: 130px; }
|
|
.stat-card .num { font-size: 1.6rem; font-weight: 700; }
|
|
.stat-card .label { font-size: .75rem; color: var(--muted); }
|
|
.btn { padding: .5rem 1rem; background: var(--accent); color: #fff; border: none; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: .85rem; }
|
|
.btn:hover { background: #a83830; }
|
|
.btn-sm { padding: 2px 8px; font-size: .7rem; margin: 1px; cursor: pointer; border: 1px solid var(--border); border-radius: 3px; background: var(--card); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<nav class="sidebar">
|
|
<h1>Админ-панель</h1>
|
|
<a href="#dashboard" class="active" onclick="showPage('dashboard')">Dashboard</a>
|
|
<a href="#users" onclick="showPage('users')">Пользователи</a>
|
|
<a href="#invites" onclick="showPage('invites')">Приглашения</a>
|
|
<a href="#sessions" onclick="showPage('sessions')">Сессии</a>
|
|
<a href="#audit" onclick="showPage('audit')">Журнал</a>
|
|
<a href="/" class="back">← Вернуться в чат</a>
|
|
</nav>
|
|
|
|
<div class="main">
|
|
|
|
<!-- Dashboard -->
|
|
<div class="page active" id="page-dashboard">
|
|
<h2>Dashboard</h2>
|
|
<div class="stat-cards" id="stats"></div>
|
|
<div id="daily"></div>
|
|
</div>
|
|
|
|
<!-- Users -->
|
|
<div class="page" id="page-users">
|
|
<h2>Пользователи</h2>
|
|
<table id="users">
|
|
<tr><th>Email</th><th>Роль</th><th>Статус</th><th>Создан</th><th>Действия</th></tr>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Invites -->
|
|
<div class="page" id="page-invites">
|
|
<h2>Приглашения</h2>
|
|
<button class="btn" onclick="createInvite()">Создать приглашение</button>
|
|
<div id="invite-result" style="margin:.8rem 0;font-size:.85rem"></div>
|
|
<table id="invites">
|
|
<tr><th>Токен</th><th>Создано</th><th>Использовано</th></tr>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Sessions -->
|
|
<div class="page" id="page-sessions">
|
|
<h2>Сессии чатов</h2>
|
|
<table id="sessions">
|
|
<tr><th>Время</th><th>Пользователь</th><th>Сообщений</th><th>Последний вопрос</th><th></th></tr>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Audit Log -->
|
|
<div class="page" id="page-audit">
|
|
<h2>Журнал действий</h2>
|
|
<div style="display:flex;gap:.5rem;margin-bottom:1rem;align-items:center;flex-wrap:wrap">
|
|
<select id="audit-user" style="padding:4px 8px;border:1px solid var(--border);border-radius:4px;font-size:.8rem">
|
|
<option value="">Все пользователи</option>
|
|
</select>
|
|
<select id="audit-action" style="padding:4px 8px;border:1px solid var(--border);border-radius:4px;font-size:.8rem">
|
|
<option value="">Все действия</option>
|
|
</select>
|
|
<button class="btn" onclick="loadAudit(0)" style="padding:4px 10px;font-size:.8rem">Фильтр</button>
|
|
</div>
|
|
<table id="audit">
|
|
<tr><th>Время</th><th>Пользователь</th><th>Действие</th><th>Путь</th><th>Детали</th><th>IP</th><th>Код</th></tr>
|
|
</table>
|
|
<button class="btn" id="audit-more" onclick="loadAuditMore()" style="margin-top:.5rem;display:none">Ещё</button>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<script>
|
|
let token = localStorage.getItem('token');
|
|
// Validate token on load
|
|
(async () => {
|
|
if (!token) { location.href = '/'; return; }
|
|
try {
|
|
const r = await fetch('/api/history?limit=1', {headers:{'Authorization':'Bearer '+token}});
|
|
if (r.ok) return;
|
|
} catch {}
|
|
const rt = localStorage.getItem('refresh_token');
|
|
if (rt) {
|
|
try {
|
|
const r = await fetch('/api/refresh', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({refresh_token:rt})});
|
|
if (r.ok) { const d = await r.json(); token = d.token; localStorage.setItem('token', token); return; }
|
|
} catch {}
|
|
}
|
|
localStorage.removeItem('token'); localStorage.removeItem('refresh_token');
|
|
location.href = '/';
|
|
})();
|
|
const H = {'Authorization': 'Bearer ' + token};
|
|
|
|
// Check token validity — redirect to login on 401
|
|
async function checkAuth() {
|
|
const r = await fetch('/api/admin/stats', {headers: H});
|
|
if (r.status === 401) { localStorage.removeItem('token'); window.location.href = '/'; }
|
|
}
|
|
checkAuth();
|
|
|
|
// Navigation
|
|
function showPage(name) {
|
|
document.querySelectorAll('.page').forEach(p => p.classList.remove('active'));
|
|
document.querySelectorAll('.sidebar a').forEach(a => a.classList.remove('active'));
|
|
document.getElementById('page-' + name).classList.add('active');
|
|
document.querySelector('.sidebar a[href="#' + name + '"]').classList.add('active');
|
|
}
|
|
// Handle hash on load
|
|
if (location.hash) showPage(location.hash.slice(1));
|
|
|
|
// === Dashboard ===
|
|
fetch('/api/admin/stats', {headers: H}).then(r=>r.json()).then(data => {
|
|
const t = data.totals;
|
|
const card = (label, val) => `<div class="stat-card"><div class="num">${val}</div><div class="label">${label}</div></div>`;
|
|
document.getElementById('stats').innerHTML = card('Сообщений', t.messages) + card('Сессий', t.sessions) + card('Пользователей', t.users);
|
|
if (data.daily && data.daily.length > 0) {
|
|
let html = '<table><tr><th>Дата</th><th>Вопросов</th><th>Ответов</th><th>Всего</th></tr>';
|
|
for (const d of data.daily) {
|
|
html += `<tr><td>${d.day}</td><td>${d.questions}</td><td>${d.answers}</td><td>${d.total}</td></tr>`;
|
|
}
|
|
document.getElementById('daily').innerHTML = html + '</table>';
|
|
}
|
|
}).catch(()=>{});
|
|
|
|
// === Users ===
|
|
async function userAction(action, email, data={}) {
|
|
if (action === 'delete' && !confirm('Удалить ' + email + '?')) return;
|
|
const r = await fetch('/api/admin/users/' + action, {method:'POST', headers:{...H,'Content-Type':'application/json'}, body:JSON.stringify({email, ...data})});
|
|
const d = await r.json();
|
|
if (d.ok) loadUsers(); else alert(d.detail || 'Ошибка');
|
|
}
|
|
|
|
function loadUsers() {
|
|
fetch('/api/admin/users', {headers: H}).then(r=>r.json()).then(users => {
|
|
const t = document.getElementById('users');
|
|
t.innerHTML = '<tr><th>Email</th><th>Роль</th><th>Статус</th><th>Создан</th><th>Действия</th></tr>';
|
|
for (const u of users) {
|
|
const d = new Date(u.created_at < 1e12 ? u.created_at*1000 : u.created_at);
|
|
const tr = document.createElement('tr');
|
|
const status = u.blocked ? '<span style="color:var(--accent)">Заблокирован</span>' : (u.verified ? '<span style="color:var(--green)">Активен</span>' : '<span style="color:var(--muted)">Не подтверждён</span>');
|
|
const ts = d.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'});
|
|
let actions = '';
|
|
const btn = (label, fn) => '<button class="btn-sm" onclick="'+fn+'">'+label+'</button>';
|
|
if (u.blocked) actions += btn('Разблокировать', "userAction('block','"+u.email+"',{blocked:false})");
|
|
else actions += btn('Заблокировать', "userAction('block','"+u.email+"',{blocked:true})");
|
|
if (!u.verified) actions += btn('Подтвердить', "userAction('verify','"+u.email+"')");
|
|
actions += btn(u.role==='admin'?'Сделать user':'Сделать admin', "userAction('role','"+u.email+"',{role:'"+(u.role==='admin'?'user':'admin')+"'})");
|
|
actions += btn('Удалить', "userAction('delete','"+u.email+"')");
|
|
tr.innerHTML = '<td>'+u.email+'</td><td>'+u.role+'</td><td>'+status+'</td><td class="time">'+ts+'</td><td>'+actions+'</td>';
|
|
t.appendChild(tr);
|
|
}
|
|
}).catch(()=>{});
|
|
}
|
|
loadUsers();
|
|
|
|
// === Invites ===
|
|
async function createInvite() {
|
|
const r = await fetch('/api/admin/invite', {method:'POST', headers: H});
|
|
const d = await r.json();
|
|
if (d.url) {
|
|
document.getElementById('invite-result').innerHTML = '<div style="background:var(--card);padding:.8rem;border-radius:6px;border:1px solid var(--border)"><b>Ссылка:</b><br><a href="'+d.url+'" target="_blank" style="word-break:break-all;font-size:.85rem">'+d.url+'</a><br><button class="btn-sm" style="margin-top:.5rem" onclick="navigator.clipboard.writeText(\''+d.url+'\');this.textContent=\'Скопировано!\'">Копировать ссылку</button></div>';
|
|
loadInvites();
|
|
} else {
|
|
document.getElementById('invite-result').textContent = d.detail || 'Ошибка';
|
|
}
|
|
}
|
|
|
|
function loadInvites() {
|
|
fetch('/api/admin/invites', {headers: H}).then(r=>r.json()).then(invites => {
|
|
const t = document.getElementById('invites');
|
|
t.innerHTML = '<tr><th>Токен</th><th>Создано</th><th>Использовано</th><th>Истекает</th></tr>';
|
|
for (const i of invites) {
|
|
const created = new Date(i.created_at < 1e12 ? i.created_at*1000 : i.created_at);
|
|
const expires = new Date(i.expires_at < 1e12 ? i.expires_at*1000 : i.expires_at);
|
|
const expired = Date.now() > expires.getTime();
|
|
const tr = document.createElement('tr');
|
|
const url = 'https://infosmeta.ru/register?invite=' + i.token;
|
|
const used = i.used_by || (expired ? '<span style="color:var(--muted)">Истёк</span>' : '<span style="color:var(--green)">Активен</span>');
|
|
tr.innerHTML = '<td style="font-size:.75rem"><a href="'+url+'" target="_blank">'+i.token.slice(0,16)+'...</a></td><td class="time">'+created.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'})+'</td><td style="font-size:.8rem">'+used+'</td><td class="time">'+expires.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'})+'</td>';
|
|
t.appendChild(tr);
|
|
}
|
|
}).catch(()=>{});
|
|
}
|
|
loadInvites();
|
|
|
|
// === Audit Log ===
|
|
let auditOffset = 0;
|
|
const AUDIT_LIMIT = 200;
|
|
|
|
async function loadAudit(offset) {
|
|
auditOffset = offset || 0;
|
|
const user = document.getElementById('audit-user').value;
|
|
const action = document.getElementById('audit-action').value;
|
|
let url = `/api/admin/audit?limit=${AUDIT_LIMIT}&offset=${auditOffset}`;
|
|
if (user) url += `&username=${encodeURIComponent(user)}`;
|
|
if (action) url += `&action=${encodeURIComponent(action)}`;
|
|
const r = await fetch(url, {headers: H});
|
|
if (!r.ok) return;
|
|
const rows = await r.json();
|
|
const t = document.getElementById('audit');
|
|
if (auditOffset === 0) t.innerHTML = '<tr><th>Время</th><th>Пользователь</th><th>Действие</th><th>Путь</th><th>Детали</th><th>IP</th><th>Код</th></tr>';
|
|
for (const row of rows) {
|
|
const d = new Date(row.timestamp < 1e12 ? row.timestamp*1000 : row.timestamp);
|
|
const ts = d.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',year:'2-digit',hour:'2-digit',minute:'2-digit',second:'2-digit'});
|
|
const tr = document.createElement('tr');
|
|
const detail = (row.detail||'').slice(0,80);
|
|
const sc = row.status_code;
|
|
const scStyle = sc >= 400 ? 'color:var(--accent)' : '';
|
|
tr.innerHTML = `<td class="time">${ts}</td><td>${row.username||'-'}</td><td><b>${row.action}</b></td><td style="font-size:.75rem">${row.path}</td><td class="q" title="${row.detail||''}">${detail}</td><td style="font-size:.75rem">${row.ip||''}</td><td style="${scStyle}">${sc||''}</td>`;
|
|
t.appendChild(tr);
|
|
}
|
|
document.getElementById('audit-more').style.display = rows.length >= AUDIT_LIMIT ? '' : 'none';
|
|
// Populate filter dropdowns from first load
|
|
if (auditOffset === 0 && rows.length > 0) {
|
|
const users = [...new Set(rows.map(r=>r.username).filter(Boolean))].sort();
|
|
const actions = [...new Set(rows.map(r=>r.action))].sort();
|
|
const uSel = document.getElementById('audit-user');
|
|
const aSel = document.getElementById('audit-action');
|
|
const curU = uSel.value, curA = aSel.value;
|
|
uSel.innerHTML = '<option value="">Все пользователи</option>' + users.map(u=>`<option value="${u}">${u}</option>`).join('');
|
|
aSel.innerHTML = '<option value="">Все действия</option>' + actions.map(a=>`<option value="${a}">${a}</option>`).join('');
|
|
uSel.value = curU; aSel.value = curA;
|
|
}
|
|
}
|
|
|
|
function loadAuditMore() {
|
|
auditOffset += AUDIT_LIMIT;
|
|
loadAudit(auditOffset);
|
|
}
|
|
loadAudit(0);
|
|
|
|
// === Sessions ===
|
|
fetch('/api/admin/sessions', {headers: H}).then(r=>r.json()).then(sessions => {
|
|
const table = document.getElementById('sessions');
|
|
for (const s of sessions) {
|
|
const d = new Date(s.last_ts < 1e12 ? s.last_ts * 1000 : s.last_ts);
|
|
const ts = d.toLocaleString('ru-RU', {day:'2-digit',month:'2-digit',year:'2-digit',hour:'2-digit',minute:'2-digit'});
|
|
const tr = document.createElement('tr');
|
|
const q = (s.last_question || '').slice(0, 80);
|
|
tr.innerHTML = `<td class="time">${ts}</td><td>${s.username}</td><td>${s.msg_count}</td><td class="q" title="${q}">${q}</td><td><a href="/?tab=${s.tab_id}" target="_blank">Открыть</a></td>`;
|
|
table.appendChild(tr);
|
|
}
|
|
}).catch(()=>{});
|
|
</script>
|
|
</body>
|
|
</html>
|