Files
smeta-web/static/admin.html
T
anorandClaude Opus 4.6 288cbe17c5 feat: управление пользователями в админке
- Заблокировать/разблокировать (blocked поле + проверка при логине)
- Удалить (с confirm, нельзя удалить себя)
- Изменить роль (admin ↔ user)
- Ручная верификация email
- Миграция: ALTER TABLE users ADD blocked
- UI: кнопки действий с цветными статусами

Тест: register → verify → block(401) → unblock(200) → delete ✅

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 16:15:07 +10:00

150 lines
8.3 KiB
HTML

<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Admin — Сессии</title>
<style>
:root { --bg: #f5f0eb; --card: #fff; --text: #3a3a3a; --muted: #6b6b6b; --accent: #c4463a; --border: #d4c5b2; }
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Inter', -apple-system, sans-serif; background: var(--bg); color: var(--text); padding: 2rem; }
.container { max-width: 900px; margin: 0 auto; }
h1 { font-size: 1.2rem; 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); }
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; }
a:hover { text-decoration: underline; }
.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; }
</style>
</head>
<body>
<div class="container">
<h1>Dashboard</h1>
<div id="stats" style="display:flex;gap:1rem;margin-bottom:1.5rem;flex-wrap:wrap"></div>
<div id="daily" style="margin-bottom:1.5rem"></div>
<div style="display:flex;gap:.5rem;margin-bottom:1.5rem">
<button onclick="createInvite()" style="padding:.5rem 1rem;background:#c4463a;color:#fff;border:none;border-radius:6px;cursor:pointer;font-weight:600">Создать приглашение</button>
</div>
<div id="invite-result" style="margin-bottom:1rem;font-size:.85rem"></div>
<h2 style="font-size:1rem;margin-bottom:.5rem">Приглашения</h2>
<table id="invites" style="margin-bottom:1.5rem">
<tr><th>Ссылка</th><th>Создано</th><th>Использовано</th></tr>
</table>
<h2 style="font-size:1rem;margin-bottom:.5rem">Пользователи</h2>
<table id="users" style="margin-bottom:1.5rem">
<tr><th>Email</th><th>Роль</th><th>Статус</th><th>Создан</th><th>Действия</th></tr>
</table>
<h2 style="font-size:1rem;margin-bottom:.5rem">Сессии</h2>
<table id="sessions">
<tr><th>Время</th><th>Пользователь</th><th>Сообщений</th><th>Последний вопрос</th><th></th></tr>
</table>
</div>
<script>
const token = localStorage.getItem('token');
if (!token) window.location.href = '/';
const H = {'Authorization': 'Bearer ' + token};
// Create invite
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 = '<b>Ссылка:</b> <a href="' + d.url + '" target="_blank">' + d.url + '</a> <button onclick="navigator.clipboard.writeText(\'' + d.url + '\');this.textContent=\'Скопировано\'" style="padding:2px 8px;font-size:.75rem;cursor:pointer">Копировать</button>';
loadInvites();
} else {
document.getElementById('invite-result').textContent = d.detail || 'Ошибка';
}
}
// Load invites
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></tr>';
for (const i of invites) {
const d = new Date(i.created_at < 1e12 ? i.created_at*1000 : i.created_at);
const tr = document.createElement('tr');
const url = 'https://smeta.kazin.ru/register?invite=' + i.token;
const used = i.used_by || '—';
tr.innerHTML = '<td style="font-size:.75rem;word-break:break-all;max-width:300px"><a href="'+url+'" target="_blank">'+i.token.slice(0,12)+'...</a></td><td class="time">'+d.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'})+'</td><td style="font-size:.8rem">'+used+'</td>';
t.appendChild(tr);
}
});
}
loadInvites();
// User actions
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:#2d8a56">Активен</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 onclick="'+fn+'" style="padding:2px 6px;font-size:.7rem;margin:1px;cursor:pointer;border:1px solid var(--border);border-radius:3px;background:var(--card)">'+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 style="font-size:.85rem">'+u.email+'</td><td style="font-size:.8rem">'+u.role+'</td><td style="font-size:.8rem">'+status+'</td><td class="time">'+ts+'</td><td>'+actions+'</td>';
t.appendChild(tr);
}
});
}
loadUsers();
// Stats
fetch('/api/admin/stats', {headers: H})
.then(r => r.json())
.then(data => {
const t = data.totals;
const box = (label, val) => `<div style="background:var(--card);padding:1rem;border-radius:8px;box-shadow:0 1px 4px rgba(0,0,0,.06);min-width:120px"><div style="font-size:1.5rem;font-weight:700">${val}</div><div style="font-size:.75rem;color:var(--muted)">${label}</div></div>`;
document.getElementById('stats').innerHTML = box('Сообщений', t.messages) + box('Сессий', t.sessions) + box('Пользователей', t.users);
// Daily chart
if (data.daily && data.daily.length > 0) {
let html = '<table style="width:100%;border-collapse:collapse;background:var(--card);border-radius:8px;overflow:hidden;box-shadow:0 1px 4px rgba(0,0,0,.06)"><tr><th style="background:#ede5db;padding:6px;font-size:.75rem">Дата</th><th style="background:#ede5db;padding:6px;font-size:.75rem">Вопросов</th><th style="background:#ede5db;padding:6px;font-size:.75rem">Ответов</th></tr>';
for (const d of data.daily) {
html += `<tr><td style="padding:4px 8px;border-top:1px solid #eee;font-size:.8rem">${d.day}</td><td style="padding:4px 8px;border-top:1px solid #eee;font-size:.8rem">${d.questions}</td><td style="padding:4px 8px;border-top:1px solid #eee;font-size:.8rem">${d.answers}</td></tr>`;
}
html += '</table>';
document.getElementById('daily').innerHTML = html;
}
});
// 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);
}
});
</script>
</body>
</html>