refactor: use user_servers junction table instead of JSON column
This commit is contained in:
@@ -80,7 +80,6 @@ async function initSchema(connection) {
|
||||
active TINYINT(1) NOT NULL DEFAULT 0,
|
||||
email_verified TINYINT(1) NOT NULL DEFAULT 0,
|
||||
source ENUM('netease','skin') NOT NULL DEFAULT 'netease',
|
||||
admin_servers JSON,
|
||||
verify_token VARCHAR(255),
|
||||
verify_expires DATETIME,
|
||||
reset_token VARCHAR(255),
|
||||
@@ -243,6 +242,14 @@ async function initSchema(connection) {
|
||||
UNIQUE KEY uk_group_server (group_name, server_name)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
|
||||
|
||||
await createIfNotExists('user_servers', `CREATE TABLE user_servers (
|
||||
user_id INT NOT NULL,
|
||||
group_name VARCHAR(100) NOT NULL,
|
||||
server_name VARCHAR(100) NOT NULL,
|
||||
UNIQUE KEY uk_user_server (user_id, group_name, server_name),
|
||||
INDEX idx_user (user_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
|
||||
|
||||
await createIfNotExists('audit_logs', `CREATE TABLE audit_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT,
|
||||
@@ -265,7 +272,7 @@ async function migrateAdditions(db) {
|
||||
try { await db.execute("ALTER TABLE tickets MODIFY type ENUM('report','suggestion','appeal','result_appeal') NOT NULL"); } catch {}
|
||||
try { await db.execute("ALTER TABLE tickets MODIFY status ENUM('pending','processing','awaiting_info','appealing','resolved','rejected','closed') NOT NULL"); } catch {}
|
||||
try { await db.execute("ALTER TABLE users ADD COLUMN source ENUM('netease','skin') NOT NULL DEFAULT 'netease'"); } catch {}
|
||||
try { await db.execute("ALTER TABLE users ADD COLUMN admin_servers JSON"); } catch {}
|
||||
try { await db.execute(`CREATE TABLE IF NOT EXISTS user_servers (user_id INT NOT NULL, group_name VARCHAR(100) NOT NULL, server_name VARCHAR(100) NOT NULL, UNIQUE KEY uk_user_server (user_id, group_name, server_name), INDEX idx_user (user_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`); } catch {}
|
||||
|
||||
try { await db.execute(`CREATE TABLE IF NOT EXISTS polls (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, description TEXT, options JSON NOT NULL, group_name VARCHAR(100) NOT NULL DEFAULT '', server_name VARCHAR(100) NOT NULL DEFAULT '', start_time DATETIME, end_time DATETIME, active TINYINT(1) NOT NULL DEFAULT 1, created_by INT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, INDEX idx_active (active), INDEX idx_group_server (group_name, server_name)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`); } catch {}
|
||||
try { await db.execute(`CREATE TABLE IF NOT EXISTS poll_votes (id INT AUTO_INCREMENT PRIMARY KEY, poll_id INT NOT NULL, user_id INT NOT NULL, option_index INT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY uk_poll_user (poll_id, user_id), INDEX idx_poll (poll_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`); } catch {}
|
||||
|
||||
@@ -6,15 +6,20 @@ const { authenticate, requireRole } = require('../middleware/auth');
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
res.json(await query('SELECT id, username, email, game_name, game_uid, source, role, active, admin_servers, email_verified, created_at FROM users ORDER BY created_at DESC'));
|
||||
res.json(await query('SELECT id, username, email, game_name, game_uid, source, role, active, email_verified, created_at FROM users ORDER BY created_at DESC'));
|
||||
});
|
||||
|
||||
router.get('/:id', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
const u = await getRow('SELECT id, username, email, game_name, game_uid, source, role, active, email_verified, created_at FROM users WHERE id = ?', [req.params.id]);
|
||||
if (!u) return res.status(404).json({ error: '用户不存在' });
|
||||
u.servers = await query('SELECT group_name, server_name FROM user_servers WHERE user_id = ?', [req.params.id]);
|
||||
res.json(u);
|
||||
});
|
||||
|
||||
router.get('/:id/servers', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
res.json(await query('SELECT group_name, server_name FROM user_servers WHERE user_id = ?', [req.params.id]));
|
||||
});
|
||||
|
||||
router.post('/', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
const { username, password, email, game_name, game_uid, role, source } = req.body;
|
||||
if (!username||!password||!email||!game_name||!game_uid||!role) return res.status(400).json({ error: '所有字段必填' });
|
||||
@@ -43,6 +48,18 @@ router.put('/:id', authenticate, requireRole('owner','admin'), async (req, res)
|
||||
if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
|
||||
const sets = Object.keys(fields).map(k => `${k} = ?`).join(', ');
|
||||
await query(`UPDATE users SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]);
|
||||
|
||||
if (req.body.admin_servers !== undefined) {
|
||||
await query('DELETE FROM user_servers WHERE user_id = ?', [req.params.id]);
|
||||
if (Array.isArray(req.body.admin_servers)) {
|
||||
for (const s of req.body.admin_servers) {
|
||||
if (s.group_name && s.server_name) {
|
||||
await query('INSERT IGNORE INTO user_servers(user_id, group_name, server_name) VALUES (?,?,?)', [req.params.id, s.group_name, s.server_name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await query("INSERT INTO audit_logs(user_id, username, action, entity_type, entity_id, details) VALUES (?,?,?,?,?,?)", [req.user.id, req.user.username, 'update_user', 'user', req.params.id, '更新用户']);
|
||||
res.json({ message: '更新成功' });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user