Files
MC_Report/backend/routes/sources.js
canglan 8c5ce78fd0 chore: add AGPLv3 copyright header to all source files
- 52 JS files (backend + public/js): header with
  Copyright (C) 2026 Sea Network Technology Studio
  Author: CangLan <admin@sea-studio.top>
  + AGPLv3 notice
- idempotent (skips if header present), all syntax-checked
2026-08-19 20:27:05 +08:00

74 lines
3.6 KiB
JavaScript

/*
* MC Report System
* Copyright (C) 2026 Sea Network Technology Studio
* Author: CangLan <admin@sea-studio.top>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express');
const { query, getRow } = require('../db');
const { authenticate, requireRole } = require('../middleware/auth');
const { logSystem } = require('../logger');
const router = express.Router();
// ---- 来源列表(公开: 注册页/绑定身份页需要) ----
router.get('/', async (req, res) => {
try {
const rows = await query('SELECT code, label, sort_order FROM sources WHERE enabled = 1 ORDER BY sort_order, id');
res.json(rows);
} catch (e) { res.status(500).json({ error: e.message }); }
});
// ---- 管理(owner) ----
router.post('/', authenticate, requireRole('owner'), async (req, res) => {
try {
const { code, label, sort_order } = req.body;
if (!code || !label) return res.status(400).json({ error: 'code 和 label 必填' });
if (!/^[a-z0-9_]{1,20}$/.test(code)) return res.status(400).json({ error: 'code 仅允许小写字母数字下划线(≤20)' });
const dup = await getRow('SELECT id FROM sources WHERE code = ?', [code]);
if (dup) return res.status(400).json({ error: '该来源已存在' });
const r = await query('INSERT INTO sources(code, label, sort_order) VALUES (?,?,?)', [code, label, parseInt(sort_order) || 0]);
await logSystem('info', 'source', `新增来源: ${code}(${label})`);
res.status(201).json({ id: r.insertId, message: '已添加' });
} catch (e) { res.status(500).json({ error: e.message }); }
});
router.put('/:code', authenticate, requireRole('owner'), async (req, res) => {
try {
const { label, enabled, sort_order } = req.body;
const row = await getRow('SELECT id FROM sources WHERE code = ?', [req.params.code]);
if (!row) return res.status(404).json({ error: '来源不存在' });
await query('UPDATE sources SET label = ?, enabled = ?, sort_order = ? WHERE code = ?',
[label || req.params.code, enabled ? 1 : 0, parseInt(sort_order) || 0, req.params.code]);
await logSystem('info', 'source', `更新来源: ${req.params.code}`, { label, enabled: !!enabled });
res.json({ message: '已更新' });
} catch (e) { res.status(500).json({ error: e.message }); }
});
router.delete('/:code', authenticate, requireRole('owner'), async (req, res) => {
try {
// 有关联数据时禁止删除(防误删)
const usedUsers = await getRow('SELECT COUNT(*) as c FROM users WHERE source = ?', [req.params.code]);
const usedIdent = await getRow('SELECT COUNT(*) as c FROM user_identities WHERE source = ?', [req.params.code]);
if (usedUsers.c > 0 || usedIdent.c > 0) return res.status(400).json({ error: `该来源已被 ${usedUsers.c + usedIdent.c} 条身份数据使用, 请先停用` });
await query('DELETE FROM sources WHERE code = ?', [req.params.code]);
await logSystem('info', 'source', `删除来源: ${req.params.code}`);
res.json({ message: '已删除' });
} catch (e) { res.status(500).json({ error: e.message }); }
});
module.exports = router;