Files
37duAutomation/.trae/documents/平台登录权限控制.md
T
puddy 863f818740 feat(controlplane): 完成多页面管理台重构与平台权限控制
1. 重构控制面前端为多页面路由架构,拆分出概览/插件/账号/权限管理页面
2. 引入Element Plus组件库优化UI样式与交互
3. 新增平台登录权限校验,支持按账号授权HR/OA平台访问
4. 新增平台登出接口与个人中心功能
5. 完善账号管理、插件授权相关API与UI交互
6. 新增防火墙、启停、自启动脚本与部署文档
2026-09-10 15:03:21 +08:00

59 lines
5.0 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.
# 平台登录权限控制(HR/OA)
## Context
webhost 底座的 HR/OA 平台登录目前只要登录了统一账户即可调用 `/api/platforms/{pid}/login`,无账号级权限区分。用户要求:平台登录也需按账号授权,未授权账号不能登录——与现有插件权限(`account_plugins` 表 + 中心机白名单)采用同一套授权模型。同时用户已开通中心机 SMB 共享(`\\DESKTOP-06SS2IA\code\controlplane`,账号 `admin`),要求本轮一并部署新版中心机 exe(含上一轮 dept 改动)。
现状:
- 中心机已有 `account_plugins` 授权表、`/api/admin/account_plugins` 授权接口(INSERT OR IGNORE / DELETE)、`/api/plugins?account=` 白名单查询、`/api/admin/accounts`(返回每账号已授权 `plugins`)——平台授权完全复用此模式
- webhost 平台登录流程:[/api/meta](file:///d:/work/code/37duAutomation/webhost/backend/main.py#L236-L241) 返回 platforms、[login](file:///d:/work/code/37duAutomation/webhost/backend/main.py#L244-L251) 仅 `_require_login`
- 控制面管理台插件授权 UI 在 [ManagementView.vue L76-L163](file:///d:/work/code/37duAutomation/controlplane/frontend/src/components/ManagementView.vue)
## 方案
平台权限按「当前登录统一账户」判定(非底座共享账号 CONTROLPLANE_ACCOUNT),中心机为真源,webhost 实时校验。
### 中心机(controlplane/app.py)
1. **建表**:`_init_db` 的 executescript 追加 `account_platforms(account TEXT, platform_id TEXT, PRIMARY KEY(account, platform_id))`(参照 account_plugins,L112-L116)。
2. **授权接口** `POST /api/admin/account_platforms`:入参 `{account, platform_id, granted}`,逻辑照抄 `admin_grant`(L525-L553):`_require_admin`、账号存在性校验、INSERT OR IGNORE / DELETE。
3. **白名单查询** `GET /api/platforms/allowed?account=xxx`(webhost 用,非 admin):返回 `{"account":..., "allowed":["hr","oa"]}`;带 `_check_token` 兜底。
4. **accounts 接口**:`/api/admin/accounts` 每个账号联查 `account_platforms` 返回 `platforms`(与现有 `plugins` 并列),供管理 UI 载入已授权平台。
### 控制面管理 UI(controlplane/frontend/src)
5. **ManagementView.vue**:
- 账号表格加一列「可登录平台」显示 `a.platforms.join('、')`
- 新增「平台授权」区块(复刻插件授权区 L76-L87):平台常量 `[{id:'hr',label:'HR 系统'},{id:'oa',label:'OA 系统'}]`,输入账号 → 载入已授权(从 accounts.platforms)→ 勾选 → 保存(逐个调授权接口,增量差量更新,参照 saveGrant L150-L163)
- **api.js** 新增 `adminPlatformGrant({account, platform_id, granted})`
### webhost 后端(webhost/backend/main.py)
6. **meta 返回平台权限**:`/api/meta` 加 `request` 参数,取 `_sess_account(request)`;已登录则调中心机 `/api/platforms/allowed?account=` 得到 allowed 集合,platforms 每项加 `allowed`(未登录/中心机不可达时默认 `true`)。
7. **login 强校验**:`/api/platforms/{pid}/login` 在 `_require_login` 后校验当前账号 allowed 含 pid,否则 403「无该平台访问权限」;中心机不可达时拒绝(503「控制面不可达,无法校验平台权限」),不做降级放行。
### webhost 前端(App.vue)
8. `onLogin` 后重新 `fetchMeta()` 刷新 platforms 的 `allowed`(登录后 cookie 生效);chip 绑定 `:class="{ online, locked: p.allowed === false }"`,未授权时 disabled + title「无权限:需管理员在控制面授权」;`login()` 对 `allowed===false` 直接拦截。
### 打包与部署
9. 控制面前端 `npm run build` → 重新打包 [37du-center.spec](file:///d:/work/code/37duAutomation/controlplane/37du-center.spec) → 同步到 `controlplane\dist\37du-center.exe`
10. webhost 前端 `npm run build`
11. **中心机部署**(SMB):`net use \\DESKTOP-06SS2IA\code /user:admin jsyx123456` 挂载凭据 → 中心机先执行 `stop_center.bat` 停服务(否则 exe 被占用)→ 覆盖 `\\DESKTOP-06SS2IA\code\controlplane\37du-center.exe` → 中心机执行 `register_autostart.bat` 重启
## 涉及文件
- `controlplane/app.py`:建表 + 授权接口 + allowed 查询 + accounts 返回 platforms
- `controlplane/frontend/src/components/ManagementView.vue`、`api.js`:平台授权 UI
- `webhost/backend/main.py`:meta allowed + login 权限校验
- `webhost/frontend/src/App.vue`:未授权置灰 + 登录后刷新
## 验证
1. 中心机:`puddy` 授权 hr/oa 后 `/api/platforms/allowed?account=puddy` 返回 `["hr","oa"]`;`alice` 未授权返回空;`/api/admin/accounts` 返回每账号 `platforms`
2. webhost:`puddy` 登录 → `/api/meta` platforms 带 `allowed`;对已授权平台 login 成功、未授权平台 403
3. 管理台:账号表格显示可登录平台;alice 授权 hr 后置灰恢复可点
4. 前端:`npm run build` 通过(webhost + controlplane)
5. 部署后:中心机 `http://172.30.28.131:8800` 正常,webhost `me.dept` 返回部门