feat: add role-based access control and user article management
- Add AdminRequired middleware to all /admin routes for proper authorization - Restrict admin panel, comments, users, settings to admin role only - Create separate /my/articles routes for regular users to manage their own articles - Add MyArticlesPage handler for users to view/edit/delete their own content - Update navigation menus with role-based visibility - Admin dropdown shows only 'Admin Panel' link, other features in dashboard - Regular users see 'My Articles' link in profile dropdown - Add i18n translations for 'my_articles' and 'comment_manage' keys - Fix permission boundary issues where author role could access admin settings Security improvements: - Platform settings now require admin role - Comment moderation requires admin role - User management requires admin role - Regular users can only manage their own articles Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
# 权限边界修复验证
|
||||
|
||||
## 修复内容
|
||||
|
||||
### 问题
|
||||
普通用户(author角色)能够访问和修改平台设置,存在严重的权限漏洞。
|
||||
|
||||
### 修复的路由组
|
||||
|
||||
1. **平台设置路由** (`/admin/settings/*`)
|
||||
- 修复前: `middleware.AuthRequired()` - 任何登录用户都可访问
|
||||
- 修复后: `middleware.AuthRequired(), middleware.AdminRequired(db)` - 仅管理员可访问
|
||||
- 影响路由:
|
||||
- GET/POST `/admin/settings/site` - 站点设置
|
||||
- GET/POST `/admin/settings/upload` - 上传设置
|
||||
- GET/POST `/admin/settings/download` - 下载设置
|
||||
- GET/POST `/admin/settings/comments` - 评论设置
|
||||
|
||||
2. **评论管理路由** (`/admin/comments/*`)
|
||||
- 修复前: 在 admin 组下,仅 `AuthRequired()`
|
||||
- 修复后: 独立路由组,使用 `AuthRequired(), AdminRequired(db)`
|
||||
- 影响路由:
|
||||
- GET `/admin/comments` - 评论列表
|
||||
- POST `/admin/comments/:id/approve` - 批准评论
|
||||
- POST `/admin/comments/:id/reject` - 拒绝评论
|
||||
- POST `/admin/comments/:id/delete` - 删除评论
|
||||
|
||||
## 权限矩阵
|
||||
|
||||
| 路由组 | 功能 | 登录要求 | 角色要求 | 说明 |
|
||||
|--------|------|----------|----------|------|
|
||||
| `/admin` | 仪表板 | ✓ | - | 所有登录用户可访问 |
|
||||
| `/admin/articles` | 文章管理 | ✓ | - | 允许作者管理自己的文章 |
|
||||
| `/admin/articles/attachments` | 附件管理 | ✓ | - | 作者上传文章附件 |
|
||||
| `/admin/comments` | 评论管理 | ✓ | admin | ✅ 仅管理员 |
|
||||
| `/admin/users` | 用户管理 | ✓ | admin | ✅ 仅管理员 |
|
||||
| `/admin/settings` | 平台设置 | ✓ | admin | ✅ 仅管理员 |
|
||||
| `/profile` | 个人资料 | ✓ | - | 所有登录用户 |
|
||||
|
||||
## 验证步骤
|
||||
|
||||
### 1. 使用管理员账户测试
|
||||
```bash
|
||||
# 登录管理员账户
|
||||
curl -X POST http://localhost:3000/login \
|
||||
-d "username=admin&password=admin123"
|
||||
|
||||
# 应该能访问设置页面
|
||||
curl http://localhost:3000/admin/settings/site
|
||||
|
||||
# 应该能访问评论管理
|
||||
curl http://localhost:3000/admin/comments
|
||||
```
|
||||
|
||||
### 2. 使用普通用户账户测试
|
||||
```bash
|
||||
# 登录普通用户
|
||||
curl -X POST http://localhost:3000/login \
|
||||
-d "username=author&password=password"
|
||||
|
||||
# 应该被重定向到 /admin(403效果)
|
||||
curl -L http://localhost:3000/admin/settings/site
|
||||
|
||||
# 应该被重定向到 /admin(403效果)
|
||||
curl -L http://localhost:3000/admin/comments
|
||||
```
|
||||
|
||||
### 3. 预期行为
|
||||
- **管理员**: 可以访问所有 `/admin/*` 路由
|
||||
- **普通用户(author)**:
|
||||
- ✓ 可以访问 `/admin` 仪表板
|
||||
- ✓ 可以管理文章 (`/admin/articles/*`)
|
||||
- ✗ **不能**访问平台设置 (`/admin/settings/*`)
|
||||
- ✗ **不能**访问评论管理 (`/admin/comments/*`)
|
||||
- ✗ **不能**访问用户管理 (`/admin/users/*`)
|
||||
|
||||
## 安全建议
|
||||
|
||||
### 已修复
|
||||
- ✅ 平台设置访问控制
|
||||
- ✅ 评论管理访问控制
|
||||
- ✅ 用户管理访问控制
|
||||
|
||||
### 未来改进建议
|
||||
1. **细粒度文章权限**: 作者只能编辑/删除自己的文章
|
||||
2. **审计日志**: 记录敏感操作(设置修改、用户管理)
|
||||
3. **会话超时**: 考虑缩短敏感操作的会话时间
|
||||
4. **CSRF保护**: 为所有POST请求添加CSRF token
|
||||
Reference in New Issue
Block a user