Files
hms/wiki/permissions.md
iven 432c5d96f2 chore: 清理 .gitignore + 添加 wiki/permissions.md
1. .gitignore: 补充临时调试文件(_*.txt/server_*.txt/tmp_*.txt 等)、
   graphify-out/、apps/mp-native/ 的忽略规则
2. wiki/permissions.md: 角色权限体系文档
2026-05-29 17:20:45 +08:00

417 lines
20 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 角色权限体系
updated: 2026-05-22
status: active
tags: [permissions, roles, rbac, frontend, miniprogram]
---
> 2026-05-22 更新:补齐 patient 角色小程序端 manage 权限15 项),注册 `system.analytics.submit` 幽灵权限,新增 §6.4 医生端小程序权限矩阵。
# 角色权限体系
> 从 [[index]] 导航。关联: [[architecture]] [[erp-health]] [[frontend]] [[miniprogram]]
## 1. 概述
HMS 采用 **RBAC基于角色的访问控制** 模型,包含 7 个系统角色,约 141+ 权限码覆盖 auth / config / workflow / message / plugin / health / ai / copilot / dialysis / system 十大模块。
权限执行层分两端:
- **Web 管理后台**:权限码守卫(`routeConfig.ts` 声明 `permissions: [...]`),后端 handler 层 `require_permission` 强制校验
- **微信小程序**:角色码守卫(`isMedicalStaff()` / `isDoctor()` / `isNurse()` / `isHealthManager()`),患者端按角色分流
### 数据范围data_scope
| 角色 | data_scope | 说明 |
|------|-----------|------|
| admin | `all` | 全部数据 |
| viewer | `all` | 全部数据(只读) |
| doctor | `all` | 全部患者数据 |
| nurse | `all` | 全部患者数据 |
| health_manager | `all` | 全部患者数据 |
| operator | `all` | 全部数据 |
| patient | `self` | 仅本人数据 |
---
## 2. 角色定义
| 角色码 | 名称 | 定位 | 典型用户 |
|--------|------|------|----------|
| `admin` | 系统管理员 | 全部权限,系统配置与用户管理 | IT 管理员 |
| `viewer` | 查看者 | 只读权限,查看基础数据 | 上级领导、审计 |
| `doctor` | 医生 | 患者诊疗、诊断、随访、咨询、透析 | 临床医生 |
| `nurse` | 护士 | 患者护理、日常监测、体征录入、设备 | 临床护士 |
| `health_manager` | 健康管理师 | 全流程健康管理、告警规则、AI 分析 | 健康管理师 |
| `operator` | 运营人员 | 内容管理、积分、媒体、轮播图 | 运营/编辑 |
| `patient` | 患者 | 小程序自助服务(仅本人数据) | 患者端用户 |
---
## 3. 权限码总表
### 3.1 基础模块auth / config / workflow / message / plugin
| 模块 | 权限码 | 说明 |
|------|--------|------|
| **用户管理** | `user.list` `user.create` `user.read` `user.update` `user.delete` | 用户 CRUD |
| **角色管理** | `role.list` `role.create` `role.read` `role.update` `role.delete` | 角色 CRUD |
| **权限管理** | `permission.list` | 权限列表(只读) |
| **组织管理** | `organization.list` `organization.create` `organization.update` `organization.delete` | 组织架构 |
| **部门管理** | `department.list` `department.create` `department.update` `department.delete` | 部门 CRUD |
| **岗位管理** | `position.list` `position.create` `position.update` `position.delete` | 岗位 CRUD |
| **字典管理** | `dictionary.list` `dictionary.create` `dictionary.update` `dictionary.delete` | 数据字典 |
| **菜单管理** | `menu.list` `menu.update` | 菜单配置 |
| **系统设置** | `setting.read` `setting.update` `setting.delete` | 系统参数 |
| **编号规则** | `numbering.list` `numbering.create` `numbering.update` `numbering.delete` `numbering.generate` | 编号序列 |
| **主题** | `theme.read` `theme.update` | UI 主题 |
| **语言** | `language.list` `language.update` | 国际化 |
| **工作流** | `workflow.create` `workflow.list` `workflow.read` `workflow.update` `workflow.publish` `workflow.start` `workflow.approve` `workflow.delegate` | BPMN 流程 |
| **消息** | `message.list` `message.send` `message.template.list` `message.template.create` `message.template.manage` | 消息通知 |
| **插件** | `plugin.admin` `plugin.list` | WASM 插件管理 |
| **租户** | `tenant.manage` | 多租户管理 |
### 3.2 健康模块health
| 子域 | 权限码 | 说明 |
|------|--------|------|
| **患者** | `health.patient.list` `health.patient.manage` | 患者档案 |
| **健康数据** | `health.health-data.list` `health.health-data.manage` | 体征/化验 |
| **预约** | `health.appointment.list` `health.appointment.manage` | 预约排班 |
| **随访** | `health.follow-up.list` `health.follow-up.manage` | 随访任务 |
| **咨询** | `health.consultation.list` `health.consultation.manage` | 在线咨询 |
| **医生** | `health.doctor.list` `health.doctor.manage` | 医生管理 |
| **诊断** | `health.diagnosis.list` `health.diagnosis.manage` | 诊断记录 |
| **日常监测** | `health.daily-monitoring.list` `health.daily-monitoring.manage` | 每日监测 |
| **告警** | `health.alerts.list` `health.alerts.manage` | 告警列表 |
| **告警规则** | `health.alert-rules.list` `health.alert-rules.manage` | 告警规则配置 |
| **危急值** | `health.critical-alerts.list` `health.critical-alerts.manage` | 危急值告警 |
| **危急值阈值** | `health.critical-value-thresholds.list` `health.critical-value-thresholds.manage` | 阈值设置 |
| **随访模板** | `health.follow-up-templates.list` `health.follow-up-templates.manage` | 模板管理 |
| **知情同意** | `health.consent.list` `health.consent.manage` | 同意管理 |
| **用药记录** | `health.medication-records.list` `health.medication-records.manage` | 用药记录 |
| **用药提醒** | `health.medication-reminders.list` `health.medication-reminders.manage` | 提醒设置 |
| **行动收件箱** | `health.action-inbox.list` `health.action-inbox.manage` `health.action-inbox.team` | 待办任务 |
| **仪表盘** | `health.dashboard.manage` | 统计仪表盘 |
| **OAuth** | `health.oauth.list` `health.oauth.manage` | 第三方授权 |
| **关怀计划** | `health.care-plan.list` `health.care-plan.manage` | 关怀计划 |
| **排班** | `health.shifts.list` `health.shifts.manage` | 医护排班 |
| **BLE 网关** | `health.ble-gateways.list` `health.ble-gateways.manage` | 蓝牙网关 |
| **家庭代理** | `health.family-proxy.list` `health.family-proxy.manage` | 家属代管 |
| **媒体库** | `health.media.list` `health.media.manage` | 媒体文件 |
| **轮播图** | `health.banners.list` `health.banners.manage` | 首页轮播 |
| **标签** | `health.tags.list` `health.tags.manage` | 患者标签 |
| **设备** | `health.devices.list` `health.devices.manage` | 设备管理 |
| **设备读数** | `health.device-readings.list` `health.device-readings.manage` | 设备数据 |
| **透析** | `health.dialysis.list` `health.dialysis.manage` | 透析记录 |
| **透析处方** | `health.dialysis-prescription.list` `health.dialysis-prescription.manage` | 透析处方 |
| **透析统计** | `health.dialysis.stats` | 透析统计 |
| **线下活动** | `health.offline-events.list` `health.offline-events.manage` | 线下活动 |
| **文章** | `health.articles.list` `health.articles.manage` `health.articles.review` | 内容管理 |
| **积分** | `health.points.list` `health.points.manage` | 积分商城 |
| **统计** | `health.stats.list` | 健康统计 |
### 3.3 AI 模块ai
| 权限码 | 说明 |
|--------|------|
| `ai.analysis.list` `ai.analysis.manage` | AI 分析 |
| `ai.prompt.list` `ai.prompt.manage` | 提示词管理 |
| `ai.provider.manage` | AI Provider 配置 |
| `ai.suggestion.list` `ai.suggestion.manage` | AI 建议 |
| `ai.usage.list` | AI 用量统计 |
| `ai.chat.send` | AI 对话 |
| `ai.config.read` `ai.config.manage` | AI 配置 |
| `ai.knowledge.list` `ai.knowledge.manage` | 知识库 |
| `ai.admin.dashboard` `ai.admin.flags` | AI 管理后台 |
### 3.4 Copilot 模块
| 权限码 | 说明 |
|--------|------|
| `copilot.insights.list` `copilot.insights.manage` | Copilot 洞察 |
| `copilot.risk.view` | 风险查看 |
| `copilot.rules.list` `copilot.rules.manage` | Copilot 规则 |
### 3.5 透析模块dialysis
| 权限码 | 说明 |
|--------|------|
| `health.dialysis.list` `health.dialysis.manage` | 透析管理 |
| `health.dialysis-prescription.list` `health.dialysis-prescription.manage` | 透析处方 |
| `health.dialysis.stats` | 透析统计 |
---
## 4. 各角色权限矩阵
> `+` = 拥有,`-` = 不拥有。admin 拥有全部权限,不再逐一列出。
### 4.1 医生doctor
| 子域 | list | manage | 特殊 |
|------|:----:|:------:|------|
| 患者 | + | + | |
| 健康数据 | + | - | 仅查看 |
| 预约 | + | + | |
| 随访 | + | + | |
| 咨询 | + | + | |
| 医生 | + | + | |
| 诊断 | + | + | |
| 日常监测 | + | + | |
| 告警 | + | + | |
| 告警规则 | + | - | 仅查看 |
| 危急值 | + | - | 仅查看 |
| 知情同意 | + | + | |
| 随访模板 | + | + | |
| 行动收件箱 | + | + | |
| 关怀计划 | + | + | |
| 透析 | + | + | |
| 透析处方 | + | + | |
| 透析统计 | + | | |
| AI 分析 | + | - | 仅查看 |
| AI 建议 | + | - | 仅查看 |
| AI 提示词 | + | - | 仅查看 |
| AI 用量 | + | - | 仅查看 |
| 消息 | + | | |
| 工作流 | + | | `list` + `read` |
**无权访问:** 文章管理、积分、标签、媒体库、轮播图、AI 管理、设备管理、线下活动、copilot
### 4.2 护士nurse
| 子域 | list | manage | 特殊 |
|------|:----:|:------:|------|
| 患者 | + | + | |
| 健康数据 | + | - | 仅查看 |
| 预约 | + | + | |
| 随访 | + | + | |
| 咨询 | + | - | 仅查看 |
| 诊断 | + | - | 仅查看 |
| 日常监测 | + | + | |
| 告警 | + | - | 仅查看 |
| 危急值 | + | - | 仅查看 |
| 知情同意 | + | + | |
| 设备 | + | - | 仅查看 |
| 设备读数 | + | - | 仅查看 |
| 行动收件箱 | + | + | |
| 消息 | + | | |
**无权访问:** 医生管理、告警管理manage、告警规则、AI、文章、积分、标签、媒体库、轮播图、透析、copilot
### 4.3 健康管理师health_manager
| 子域 | list | manage | 特殊 |
|------|:----:|:------:|------|
| 患者 | + | + | |
| 健康数据 | + | + | |
| 医生 | + | - | 仅查看 |
| 随访 | + | + | |
| 咨询 | + | + | |
| 诊断 | + | + | |
| 日常监测 | + | + | |
| 告警 | + | + | |
| 告警规则 | + | + | |
| 危急值 | + | - | 仅查看 |
| 危急值阈值 | + | - | 仅查看 |
| 知情同意 | + | + | |
| 随访模板 | + | + | |
| 标签 | + | + | |
| 设备 | + | - | 仅查看 |
| 行动收件箱 | + | + | `team` 额外权限 |
| 仪表盘 | + | | `dashboard.manage` |
| AI 分析 | + | + | |
| AI 建议 | + | + | |
| AI 提示词 | + | - | 仅查看 |
| AI 用量 | + | - | 仅查看 |
| 消息 | + | | |
| 工作流 | + | | `list` + `read` + `start` |
**无权访问:** 预约、透析、文章、积分、媒体库、轮播图、线下活动、copilot、OAuth、关怀计划、排班
### 4.4 运营人员operator
| 子域 | list | manage | 特殊 |
|------|:----:|:------:|------|
| 患者 | + | - | 仅查看 |
| 标签 | + | + | |
| 文章 | + | + | `review` 额外权限 |
| 积分 | + | + | |
| 设备 | + | - | 仅查看 |
| 告警 | + | - | 仅查看 |
| 媒体库 | + | + | |
| 轮播图 | + | + | |
| 仪表盘 | + | | `dashboard.manage` |
| AI 用量 | + | - | 仅查看 |
| 消息 | + | | |
**无权访问:** 健康数据、预约、随访、咨询、诊断、日常监测、告警规则、危急值、知情同意、AI 分析、透析、copilot
### 4.5 患者patient
> data_scope = `self`,所有操作仅限本人数据。通过小程序访问(`m20260522_000162` 完成全量配置)。
| 子域 | list | manage | 说明 |
|------|:----:|:------:|------|
| 健康数据 | + | + | 录入体征、查看本人体征/化验 |
| 患者 | + | + | 查看/更新本人档案、绑定手机 |
| 预约 | + | + | 创建/取消本人预约 |
| 医生 | + | - | 预约时选择医生 |
| 随访 | + | + | 提交随访记录 |
| 咨询 | + | + | 创建咨询会话、发送消息 |
| 积分 | + | + | 签到、兑换商品 |
| 文章 | + | - | 阅读公开文章 |
| 告警 | + | - | 查看本人告警 |
| 日常监测 | + | + | 创建日常监测记录 |
| 设备读数 | + | + | 上传设备数据 |
| 设备 | + | - | 查看绑定设备 |
| 知情同意 | + | + | 授权/撤回本人同意 |
| 用药记录 | + | - | 查看本人用药 |
| 用药提醒 | + | + | CRUD 本人提醒 |
| 关怀计划 | + | - | 查看本人计划 |
| 行动收件箱 | + | - | 查看本人待办 |
| 透析 | + | - | 查看本人透析 |
| AI 分析 | + | - | 查看本人分析报告 |
| AI 建议 | + | - | 查看分析建议 |
| AI 对话 | + | - | `ai.chat.send` + 会话 list/manage |
| 消息 | + | - | 查看本人消息 |
| 埋点 | - | - | `system.analytics.submit`data_scope=self |
### 4.6 查看者viewer
基础模块只读权限auth / config / workflow / message / plugin 的 `list`/`read`)。无 health / AI / copilot 权限。
---
## 5. Web 前端路由权限
Web 管理后台通过 `routeConfig.ts` 声明每个路由所需权限码。用户登录后,路由守卫检查其角色是否拥有对应权限。
### 5.1 各角色可见菜单
#### admin
全部菜单可见。
#### doctor
首页、统计仪表盘、患者管理、日常监测、诊断记录、知情同意、咨询管理、随访任务、随访模板、行动收件箱、告警仪表盘、告警管理、AI 分析、AI 用量、AI 对话、消息
#### nurse
首页、统计仪表盘、患者管理、日常监测、诊断记录、知情同意、咨询管理、随访任务、行动收件箱、告警仪表盘、告警管理、消息
#### health_manager
首页、统计仪表盘、患者管理、日常监测、诊断记录、知情同意、咨询管理、标签管理、医生管理、随访任务、随访模板、行动收件箱、实时监测、告警仪表盘、告警管理、告警规则、设备管理、危急值阈值、AI 提示词、AI 分析、AI 知识库、AI 用量、AI 配置、AI 对话、消息
#### operator
首页、统计仪表盘、患者管理只读、标签管理、设备管理、告警仪表盘、告警管理只读、文章管理、积分规则、积分商品、积分订单、线下活动、媒体库、轮播图、AI 用量、消息
---
## 6. 小程序角色控制
小程序端采用**角色码**(非权限码)做前端控制,后端仍通过权限码校验 API 请求。
### 6.1 角色判断函数
```typescript
// stores/auth.ts
isMedicalStaff() roles doctor / nurse / admin / health_manager
isDoctor() roles doctor / admin
isNurse() roles nurse / admin
isHealthManager() roles health_manager / admin
hasRole(code) roles code / admin
```
### 6.2 角色与小程序页面映射
| 角色 | 可访问页面 |
|------|-----------|
| **patient** | 首页、健康、咨询、商城、我的5 TabBar+ 分包页面预约、随访、告警、积分、文章、设备、AI 对话) |
| **doctor / admin** | 首页 → 自动跳转医生端分包(`pkg-doctor-core`+ 患者管理、咨询、随访、行动收件箱 |
| **nurse** | 同 doctor但部分管理功能降级为只读 |
| **health_manager** | 同 doctor额外可管理告警规则、AI 分析 |
### 6.3 患者端patientAPI 权限
患者通过小程序访问以下 API均需 `patient` 角色且 `data_scope=self`)。完整权限配置见迁移 `m20260522_000162`
| 小程序页面 | API 路径 | 所需权限码 | 操作类型 |
|-----------|----------|-----------|---------|
| 健康总览 | `GET /health/health-data/summary` | `health.health-data.list` | 只读 |
| 体征录入 | `POST /health/health-data` | `health.health-data.manage` | 写入 |
| 健康趋势 | `GET /health/health-data/trend` | `health.health-data.list` | 只读 |
| 我的报告 | `GET /health/patients/{id}/lab-reports` | `health.health-data.list` | 只读 |
| AI 解读 | `GET /ai/analyses` | `ai.analysis.list` | 只读 |
| 健康档案 | `GET /health/health-records` | `health.health-data.list` | 只读 |
| 诊断记录 | `GET /health/diagnoses` | `health.health-data.list` | 只读 |
| 我的预约 | `GET /health/appointments` | `health.appointment.list` | 只读 |
| 创建预约 | `POST /health/appointments` | `health.appointment.manage` | 写入 |
| 医生列表 | `GET /health/doctors` | `health.doctor.list` | 只读 |
| 我的随访 | `GET /health/follow-up-tasks` | `health.follow-up.list` | 只读 |
| 提交随访 | `POST /health/follow-up-records` | `health.follow-up.manage` | 写入 |
| 在线咨询 | `POST /health/consultation-sessions` | `health.consultation.manage` | 写入 |
| 咨询消息 | `GET/POST /health/consultation-sessions/{id}/messages` | `health.consultation.list` + `manage` | 读写 |
| 告警列表 | `GET /health/alerts` | `health.alerts.list` | 只读 |
| 行动收件箱 | `GET /health/action-inbox` | `health.action-inbox.list` | 只读 |
| 设备同步 | `POST /health/device-readings` | `health.device-readings.manage` | 写入 |
| 药物提醒 | `GET/POST/PUT/DELETE /health/medication-reminders` | `health.medication-reminders.list` + `manage` | 读写 |
| 知情同意 | `POST/PUT /health/consents` | `health.consent.list` + `manage` | 读写 |
| 积分账户 | `GET /health/points/account` | `health.points.list` | 只读 |
| 积分签到 | `POST /health/points/checkin` | `health.points.manage` | 写入 |
| 积分兑换 | `POST /health/points/redeem` | `health.points.manage` | 写入 |
| 文章列表 | `GET /health/articles` | `health.articles.list` | 只读 |
| 消息通知 | `GET /messages` | `message.list` | 只读 |
| AI 对话 | `POST /ai/chat/send` | `ai.chat.send` | 写入 |
| AI 会话 | `GET/POST /ai/chat/sessions` | `ai.chat.session.list` + `manage` | 读写 |
| 埋点上报 | `POST /analytics/batch` | `system.analytics.submit` | 写入 |
| 就诊人管理 | `GET/PUT /health/patients/{id}` | `health.patient.list` + `manage` | 读写 |
| 日常监测 | `GET/POST /health/daily-monitoring` | `health.daily-monitoring.list` + `manage` | 读写 |
### 6.4 医生端小程序权限
医生/护士通过小程序医生端分包(`pkg-doctor-core`)访问以下 API。权限码与 Web 管理后台一致。
| 小程序页面 | API 路径 | 所需权限码 | 角色要求 |
|-----------|----------|-----------|---------|
| 医生首页 | `GET /health/doctor/dashboard` | `health.dashboard.manage` | doctor/nurse/hm |
| 患者管理 | `GET /health/patients` | `health.patient.list` | doctor/nurse/hm |
| 患者详情 | `GET /health/patients/{id}/health-summary` | `health.patient.list` | doctor/nurse/hm |
| 咨询列表 | `GET /health/consultation-sessions` | `health.consultation.list` | doctor/nurse/hm |
| 咨询回复 | `POST /health/consultation-sessions/{id}/messages` | `health.consultation.manage` | doctor/hm |
| 随访管理 | `GET/PUT /health/follow-up-tasks` | `health.follow-up.list` + `manage` | doctor/nurse/hm |
| 告警处理 | `POST /health/alerts/{id}/acknowledge` | `health.alerts.manage` | doctor/hm |
| 行动收件箱 | `GET /health/action-inbox` | `health.action-inbox.list` | doctor/nurse/hm |
| 团队概览 | `GET /health/action-inbox/team` | `health.action-inbox.team` | hm |
| 化验报告 | `GET /health/patients/{id}/lab-reports` | `health.health-data.list` | doctor/nurse/hm |
| 透析管理 | `GET/POST /health/dialysis-records` | `health.dialysis.list` + `manage` | doctor |
| 透析处方 | `GET/POST /health/dialysis-prescriptions` | `health.dialysis-prescription.list` + `manage` | doctor |
---
## 7. 权限配置维护
### 7.1 新增权限码流程
1. 在对应模块 `module.rs``PermissionDescriptor` 中声明权限码
2. 创建迁移文件 seed 权限到 `permissions`
3. 迁移中为需要该权限的角色添加 `role_permissions` 记录
4. 前端路由声明对应 `permissions` 数组
5. 更新本文档
### 7.2 关键迁移文件
| 迁移 | 说明 |
|------|------|
| `seed.rs` | 基础权限auth/config/workflow/message/plugin |
| `m20260506_000125` | 创建 doctor/nurse/health_manager/operator 角色及初始权限 |
| `m20260508_000131` | 权威修复:重新分配 doctor/nurse/operator 权限 |
| `m20260510_000133` | 创建 patient 角色data_scope=self18 个 .list 权限) |
| `m20260510_000137` | 媒体库/轮播图权限 + operator 补充 |
| `m20260516_000147` | AI 对话权限patient + admin |
| `m20260518_000149` | admin 全量权限修复 |
| `m20260521_000164` | 菜单体系重组 |
| `m20260522_000161` | patient 积分 manage 权限 |
| `m20260522_000162` | patient 全量小程序权限15 manage + 1 list + system.analytics.submit 注册) |
### 7.3 权限同步机制
系统启动时自动同步:模块通过 `ErpModule` trait 注册 `PermissionDescriptor``sync_module_permissions()` 将新权限码插入 `permissions`admin 角色自动获得所有新权限。