| 1 | /**
|
|---|
| 2 | * Admin: User management — Phase E.
|
|---|
| 3 | *
|
|---|
| 4 | * GET /admin/users -> list users
|
|---|
| 5 | * POST /admin/users/:id/role -> change role (member/admin/god)
|
|---|
| 6 | * POST /admin/users/:id/delete -> delete (refused if user has owned content)
|
|---|
| 7 | *
|
|---|
| 8 | * Safety rules:
|
|---|
| 9 | * - The system always keeps at least 1 god (you can't demote/delete the last one).
|
|---|
| 10 | * - You can't change your OWN role to non-god (avoid locking yourself out).
|
|---|
| 11 | * - Delete is refused if the user owns any sites or has any posts.
|
|---|
| 12 | * Leaves it to god to reassign content first.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | import express from 'express';
|
|---|
| 16 | import db from '../config/database.js';
|
|---|
| 17 | import { renderPage } from '../middleware/render.js';
|
|---|
| 18 | import { requireGod } from '../middleware/auth.js';
|
|---|
| 19 |
|
|---|
| 20 | const router = express.Router();
|
|---|
| 21 |
|
|---|
| 22 | const VALID_ROLES = new Set(['member', 'admin', 'god']);
|
|---|
| 23 |
|
|---|
| 24 | function godCount() {
|
|---|
| 25 | return db.prepare("SELECT COUNT(*) AS c FROM users WHERE role = 'god'").get().c;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | // ==================== LIST ====================
|
|---|
| 29 | router.get('/', requireGod, (req, res) => {
|
|---|
| 30 | const users = db.prepare(`
|
|---|
| 31 | SELECT u.id, u.username, u.email, u.role, u.created_at, u.avatar_url,
|
|---|
| 32 | (SELECT COUNT(*) FROM posts p WHERE p.author_id = u.id) AS post_count,
|
|---|
| 33 | (SELECT COUNT(*) FROM sites s WHERE s.owner_id = u.id) AS site_count
|
|---|
| 34 | FROM users u
|
|---|
| 35 | ORDER BY u.created_at DESC
|
|---|
| 36 | `).all();
|
|---|
| 37 |
|
|---|
| 38 | renderPage(req, res, 'pages/admin-users', {
|
|---|
| 39 | pageTitle: 'Users',
|
|---|
| 40 | bodyClass: 'on-admin',
|
|---|
| 41 | users,
|
|---|
| 42 | success: req.query.success || null,
|
|---|
| 43 | error: req.query.error || null,
|
|---|
| 44 | });
|
|---|
| 45 | });
|
|---|
| 46 |
|
|---|
| 47 | // ==================== CHANGE ROLE ====================
|
|---|
| 48 | router.post('/:id/role', requireGod, (req, res) => {
|
|---|
| 49 | const userId = req.params.id;
|
|---|
| 50 | const newRole = (req.body.role || '').toString();
|
|---|
| 51 | if (!VALID_ROLES.has(newRole)) {
|
|---|
| 52 | return res.redirect('/admin/users?error=Invalid+role');
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | const target = db.prepare('SELECT id, role FROM users WHERE id = ?').get(userId);
|
|---|
| 56 | if (!target) return res.redirect('/admin/users?error=User+not+found');
|
|---|
| 57 |
|
|---|
| 58 | // Prevent self-demotion away from god (lock-out protection)
|
|---|
| 59 | if (target.id === req.session.user.id && newRole !== 'god') {
|
|---|
| 60 | return res.redirect('/admin/users?error=' + encodeURIComponent('Cannot demote yourself'));
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | // Prevent removing the last god
|
|---|
| 64 | if (target.role === 'god' && newRole !== 'god' && godCount() <= 1) {
|
|---|
| 65 | return res.redirect('/admin/users?error=' + encodeURIComponent('Cannot demote the only remaining god'));
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | db.prepare('UPDATE users SET role = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?')
|
|---|
| 69 | .run(newRole, userId);
|
|---|
| 70 | res.redirect('/admin/users?success=' + encodeURIComponent('Role updated'));
|
|---|
| 71 | });
|
|---|
| 72 |
|
|---|
| 73 | // ==================== DELETE ====================
|
|---|
| 74 | router.post('/:id/delete', requireGod, (req, res) => {
|
|---|
| 75 | const userId = req.params.id;
|
|---|
| 76 | const target = db.prepare('SELECT id, role, username FROM users WHERE id = ?').get(userId);
|
|---|
| 77 | if (!target) return res.redirect('/admin/users?error=User+not+found');
|
|---|
| 78 |
|
|---|
| 79 | if (target.id === req.session.user.id) {
|
|---|
| 80 | return res.redirect('/admin/users?error=' + encodeURIComponent('Cannot delete yourself'));
|
|---|
| 81 | }
|
|---|
| 82 | if (target.role === 'god' && godCount() <= 1) {
|
|---|
| 83 | return res.redirect('/admin/users?error=' + encodeURIComponent('Cannot delete the only remaining god'));
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | const owned = db.prepare(`
|
|---|
| 87 | SELECT
|
|---|
| 88 | (SELECT COUNT(*) FROM posts WHERE author_id = ?) AS posts,
|
|---|
| 89 | (SELECT COUNT(*) FROM sites WHERE owner_id = ?) AS sites
|
|---|
| 90 | `).get(userId, userId);
|
|---|
| 91 |
|
|---|
| 92 | if (owned.posts > 0 || owned.sites > 0) {
|
|---|
| 93 | return res.redirect('/admin/users?error=' + encodeURIComponent(
|
|---|
| 94 | `Cannot delete: user owns ${owned.sites} site(s) and ${owned.posts} post(s). Reassign or delete those first.`
|
|---|
| 95 | ));
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | // Clean up dangling references
|
|---|
| 99 | db.prepare('DELETE FROM site_members WHERE user_id = ?').run(userId);
|
|---|
| 100 | db.prepare('DELETE FROM comments WHERE author_id = ?').run(userId);
|
|---|
| 101 | db.prepare('DELETE FROM users WHERE id = ?').run(userId);
|
|---|
| 102 |
|
|---|
| 103 | res.redirect('/admin/users?success=' + encodeURIComponent('User deleted: ' + target.username));
|
|---|
| 104 | });
|
|---|
| 105 |
|
|---|
| 106 | export default router;
|
|---|