Add user management for admin (CRUD + role + reset password)

Adds /api/users endpoints (admin only) plus /api/users/assignable
(admin + technician) used by the aftersales reassign picker. Guards
prevent self-demotion, self-deletion, and removing the last admin.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Frudrax Cheng
2026-05-26 10:57:53 +08:00
parent b1e3adaf6a
commit 3ddd4db126
6 changed files with 453 additions and 4 deletions
+24 -2
View File
@@ -34,7 +34,8 @@ backend-go/
│ ├── companies_controller.go # 企业管理接口
│ ├── employees_controller.go # 员工赋码接口
│ ├── helper.go # 控制器通用辅助函数
── serials_controller.go # 序列号管理接口
── serials_controller.go # 序列号管理接口
│ └── users_controller.go # 用户管理接口(仅管理员)
├── database/ # 数据库连接和操作
│ └── database.go # 数据库初始化、连接池配置
├── docs/ # Swagger API 文档(自动生成)
@@ -55,7 +56,8 @@ backend-go/
│ ├── companies_service.go # 企业管理业务逻辑
│ ├── employees_service.go # 员工赋码业务逻辑
│ ├── serials_service.go # 序列号业务逻辑
── services_test.go # 服务层单元测试
── services_test.go # 服务层单元测试
│ └── users_service.go # 用户管理业务逻辑
├── tests/ # 集成测试
│ └── main_test.go # 端到端测试
├── data/ # 数据目录(SQLite 数据库存储位置)
@@ -318,6 +320,26 @@ swag init -g main.go
- 工单状态机: `created``pending_confirmation``closed` / `rejected`,被退回后可重新提交
- 公开查询不返回手机号(脱敏)
### 用户管理(仅管理员)
| 方法 | 路径 | 描述 | 需要认证 | 角色 |
| ------ | ----------------------------------- | -------------------------- | -------- | ------------- |
| GET | `/api/users/assignable` | 可分配用户列表(用于售后) | 是 | 管理员/技术员 |
| POST | `/api/users` | 创建用户 | 是 | 管理员 |
| GET | `/api/users` | 用户列表(分页+筛选) | 是 | 管理员 |
| PATCH | `/api/users/:id` | 更新用户姓名/邮箱/角色 | 是 | 管理员 |
| POST | `/api/users/:id/reset-password` | 重置用户密码 | 是 | 管理员 |
| DELETE | `/api/users/:id` | 删除用户 | 是 | 管理员 |
**用户角色**:
- `admin`:完整权限,包括用户管理、强制关闭工单、重新分配技术员、删除工单
- `technician`:可创建/编辑自己负责的售后工单,可使用 `assignable` 查询同事
- `user`:保留角色(暂未实际启用)
**保护规则**:
- 不能删除自己;不能将自己的 admin 角色降级;不能删除最后一个 admin
- 默认创建用户密码 bcrypt 加密存储
## 测试
### 运行所有测试