fix(guest): 清除 18 处 'user-1' 兜底,堵住游客数据泄漏与越权写
- 游客直连 created tab 不再返回 user-1 的自办活动 - 游客 latest/详情 isJoined 不再按 user-1 报名行误标,且显式化为布尔 - PUT /user/profile、DELETE /user/account、POST /user/avatar 补登录守卫(LOGIN_REQUIRED) - 此前游客无头直连即可改写/注销 user-1(管理员种子账号)资料 - avatar 守卫前置到 multer 之前,避免孤儿文件落盘 - 守卫内(requireEmail/requireActivityAdmin) 6 处兜底为死代码,一并规范化为空串 E2E 21/21(3011):游客 10 端点零泄漏不破坏 + 3 写操作拦截 + 8 项登录用户不回归
This commit is contained in:
@@ -192,7 +192,7 @@ const storage = multer.diskStorage({
|
||||
cb(null, dir);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const ext = path.extname(file.originalname) || '.jpg';
|
||||
cb(null, `${userId}_${Date.now()}${ext}`);
|
||||
}
|
||||
@@ -965,7 +965,7 @@ app.post('/auth/logout', (req, res) => {
|
||||
// 获取用户资料
|
||||
app.get('/user/profile', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const user = getUserWithCompanies(userId);
|
||||
if (!user) {
|
||||
return res.json({ success: false, message: '用户不存在' });
|
||||
@@ -980,7 +980,8 @@ app.get('/user/profile', (req, res) => {
|
||||
// 更新用户资料(email 为源头身份锚点不可修改;未传字段保持原值)
|
||||
app.put('/user/profile', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
if (!userId) return res.json({ success: false, code: 'LOGIN_REQUIRED', message: '未登录' });
|
||||
const { nickname, position, chaishu_level, industry, hometown, expertise, weixin, phone, companies } = req.body;
|
||||
const now = new Date().toISOString();
|
||||
|
||||
@@ -1006,7 +1007,7 @@ app.put('/user/profile', (req, res) => {
|
||||
// 获取用户统计
|
||||
app.get('/user/stats', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const createdCount = db.prepare('SELECT COUNT(*) as count FROM activities WHERE creator_id = ?').get(userId);
|
||||
// 参与数与「我参与的活动」同口径:排除自己发起的活动(创建时自动生成的参与行)
|
||||
const joinedCount = db.prepare(`SELECT COUNT(*) as count FROM activity_participants ap
|
||||
@@ -1032,7 +1033,8 @@ const recalcParticipantCount = (activityId) => {
|
||||
// 注销账号
|
||||
app.delete('/user/account', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
if (!userId) return res.json({ success: false, code: 'LOGIN_REQUIRED', message: '未登录' });
|
||||
const affectedActivities = db.prepare('SELECT DISTINCT activity_id FROM activity_participants WHERE user_id = ?').all(userId).map(r => r.activity_id);
|
||||
db.prepare('DELETE FROM user_companies WHERE user_id = ?').run(userId);
|
||||
db.prepare('DELETE FROM activity_participants WHERE user_id = ?').run(userId);
|
||||
@@ -1048,6 +1050,7 @@ app.delete('/user/account', (req, res) => {
|
||||
// 上传头像
|
||||
// 上传用户头像
|
||||
app.post('/user/avatar', (req, res, next) => {
|
||||
if (!req.headers['x-user-id']) return res.json({ success: false, code: 'LOGIN_REQUIRED', message: '未登录' });
|
||||
req.uploadSubDir = 'avatars';
|
||||
next();
|
||||
}, upload.single('file'), (req, res) => {
|
||||
@@ -1055,7 +1058,7 @@ app.post('/user/avatar', (req, res, next) => {
|
||||
if (!req.file) {
|
||||
return res.json({ success: false, message: '请选择要上传的图片' });
|
||||
}
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const avatarUrl = `/uploads/avatars/${req.file.filename}`;
|
||||
|
||||
// 更新用户头像
|
||||
@@ -1071,7 +1074,7 @@ app.post('/user/avatar', (req, res, next) => {
|
||||
// 获取资料完善度
|
||||
app.get('/user/profile/completion', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const user = db.prepare('SELECT * FROM users WHERE id = ?').get(userId);
|
||||
|
||||
if (!user) {
|
||||
@@ -1103,7 +1106,7 @@ app.get('/user/profile/completion', (req, res) => {
|
||||
app.get('/activities', (req, res) => {
|
||||
try {
|
||||
const { tab = 'latest', page, pageSize } = req.query;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const pageNum = parseInt(page) || 1;
|
||||
const pageSizeNum = parseInt(pageSize) || 10;
|
||||
const offset = (pageNum - 1) * pageSizeNum;
|
||||
@@ -1178,7 +1181,7 @@ app.get('/activities', (req, res) => {
|
||||
createdAt: activity.created_at,
|
||||
updatedAt: activity.updated_at,
|
||||
tags: tags.map(t => t.tag_name),
|
||||
isJoined: participant && participant.status !== 'cancelled'
|
||||
isJoined: !!(participant && participant.status !== 'cancelled')
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1192,7 +1195,7 @@ app.get('/activities', (req, res) => {
|
||||
// 获取活动模板 - 必须在 /activities/:id 之前
|
||||
app.get('/activities/templates', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const activities = db.prepare(`
|
||||
SELECT DISTINCT a.id, a.title, a.cover_url, a.description, a.location, a.start_time, a.duration, a.max_participants, a.fee, a.organizer, a.checkin_enabled
|
||||
@@ -1239,7 +1242,7 @@ app.post('/activities/upload/cover', requireActivityAdmin, (req, res, next) => {
|
||||
app.get('/activities/:id', (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const activity = db.prepare('SELECT * FROM activities WHERE id = ?').get(id);
|
||||
if (!activity) {
|
||||
@@ -1303,7 +1306,7 @@ app.get('/activities/:id', (req, res) => {
|
||||
position: creator.position,
|
||||
chaishuLevel: creator.chaishu_level || ''
|
||||
} : null,
|
||||
isJoined: userParticipant && userParticipant.status !== 'cancelled',
|
||||
isJoined: !!(userParticipant && userParticipant.status !== 'cancelled'),
|
||||
};
|
||||
|
||||
res.json({ success: true, data: activityDetail });
|
||||
@@ -1316,7 +1319,7 @@ app.get('/activities/:id', (req, res) => {
|
||||
// 创建活动
|
||||
app.post('/activities', requireActivityAdmin, (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const { title, cover_url, description, location, start_time, duration, max_participants, tags, checkin_enabled, fee, organizer } = req.body;
|
||||
const activityId = uuidv4();
|
||||
const now = new Date().toISOString();
|
||||
@@ -1351,7 +1354,7 @@ app.post('/activities', requireActivityAdmin, (req, res) => {
|
||||
app.put('/activities/:id', requireActivityAdmin, (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
const { title, cover_url, description, location, start_time, duration, max_participants, tags, checkin_enabled, fee, organizer } = req.body;
|
||||
|
||||
const activity = db.prepare('SELECT * FROM activities WHERE id = ?').get(id);
|
||||
@@ -1389,7 +1392,7 @@ app.put('/activities/:id', requireActivityAdmin, (req, res) => {
|
||||
app.post('/activities/:id/share', (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const activity = db.prepare('SELECT * FROM activities WHERE id = ?').get(id);
|
||||
if (!activity) {
|
||||
@@ -1410,7 +1413,7 @@ app.post('/activities/:id/share', (req, res) => {
|
||||
app.post('/activities/:id/join', requireEmail, (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const activity = db.prepare('SELECT * FROM activities WHERE id = ?').get(id);
|
||||
if (!activity) {
|
||||
@@ -1450,7 +1453,7 @@ app.post('/activities/:id/join', requireEmail, (req, res) => {
|
||||
app.delete('/activities/:id/join', requireEmail, (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const participant = db.prepare('SELECT * FROM activity_participants WHERE activity_id = ? AND user_id = ?').get(id, userId);
|
||||
if (!participant || participant.status === 'cancelled') {
|
||||
@@ -1472,7 +1475,7 @@ app.delete('/activities/:id/join', requireEmail, (req, res) => {
|
||||
app.get('/activities/:id/checkin-status', (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
// 获取活动信息
|
||||
const activity = db.prepare('SELECT * FROM activities WHERE id = ?').get(id);
|
||||
@@ -1564,7 +1567,7 @@ app.get('/activities/:id/checkin-status', (req, res) => {
|
||||
app.post('/activities/:id/checkin', requireEmail, (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const participant = db.prepare('SELECT * FROM activity_participants WHERE activity_id = ? AND user_id = ?').get(id, userId);
|
||||
if (!participant) {
|
||||
@@ -1589,7 +1592,7 @@ app.post('/activities/:id/checkin', requireEmail, (req, res) => {
|
||||
app.delete('/activities/:id', requireActivityAdmin, (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const userId = req.headers['x-user-id'] || '';
|
||||
|
||||
const activity = db.prepare('SELECT * FROM activities WHERE id = ?').get(id);
|
||||
if (!activity) {
|
||||
|
||||
Reference in New Issue
Block a user