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>
This commit is contained in:
anor
2026-03-27 16:15:07 +10:00
co-authored by Claude Opus 4.6
parent ee3d3e8c24
commit 288cbe17c5
4 changed files with 119 additions and 17 deletions
+34 -11
View File
@@ -35,7 +35,7 @@ a:hover { text-decoration: underline; }
<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></tr>
<tr><th>Email</th><th>Роль</th><th>Статус</th><th>Создан</th><th>Действия</th></tr>
</table>
<h2 style="font-size:1rem;margin-bottom:.5rem">Сессии</h2>
@@ -78,16 +78,39 @@ function loadInvites() {
}
loadInvites();
// Load users
fetch('/api/admin/users', {headers: H}).then(r=>r.json()).then(users => {
const t = document.getElementById('users');
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');
tr.innerHTML = '<td>'+u.email+'</td><td>'+u.role+'</td><td>'+(u.verified?'Да':'Нет')+'</td><td class="time">'+d.toLocaleString('ru-RU',{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit'})+'</td>';
t.appendChild(tr);
}
});
// 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})