feat: Agent 用户身份识别 + RBAC 登录修复 + 模型配置同步#193
Merged
yhjun1026 merged 6 commits intoderisk-ai:mainfrom Apr 26, 2026
Merged
Conversation
… management OAuth2 登录后用户无 RBAC 角色导致无权限的系列 bug 修复: 1. 已有用户重新登录时不分配 RBAC 角色 - 提取 _ensure_user_has_role() 函数,新老用户登录时均检查并补分配角色 - 角色分配失败日志级别从 warning 升级为 error 2. 启动时 default_role 丢失 - _sync_oauth2_config_from_db() 补上 default_role 字段 3. 存量用户无角色 - ensure_default_roles() 启动时为无 RBAC 角色的存量用户补分配 viewer 4. 用户管理读接口需 admin 权限 - permissions/api.py: 用户列表/详情/权限、角色列表/详情/权限、权限定义等读接口降为仅需认证 - users_api.py: 修复 GET/PATCH 无认证的安全漏洞,读接口加认证,写接口需 admin 5. 无账密登录方式 - UserEntity 新增 password_hash 字段 - 新增 POST /auth/login 本地登录端点 (bcrypt) - seed.py 为 admin 用户设置默认密码 admin - /oauth/status 返回 local_login_enabled 字段 - 前端登录页添加账密登录表单 - pyproject.toml 新增 bcrypt 依赖 6. 账号角色与 RBAC 角色不一致 - update_user 设置 admin 时自动同步 RBAC admin 角色 - 前端 UserManagement 移除重复的"账号角色"列 - "设为管理员"按钮同时操作 RBAC 和 legacy role 7. MySQL 不自动同步新增列 - _add_missing_columns_sqlite 重命名为 _add_missing_columns,支持 MySQL - MySQL 分支启动时也自动创建表和补新增列
问题:启动时 _sync_app_config_to_system_app() 读取 ~/.derisk/derisk.json 中的 agent_llm.providers(默认为空数组),覆盖了 TOML 配置文件中已配置 的模型提供者,导致 Agent 调用时报 "No model service available" 错误。 修复: - 当 derisk.json 的 providers 为空但 TOML 有配置时,自动将 TOML 的 providers 同步写入 derisk.json(一次性 bootstrap) - TOML 中的 api_key 自动存入加密 secrets,provider 改用 api_key_ref 引用,避免明文密钥写入 JSON 配置 - 新增 _convert_toml_agent_llm_to_json_format() 函数处理 TOML→JSON 格式转换(provider→providers, model→models) - JSON 配置已有 providers 时不覆盖(JSON 优先,支持 Web UI 热更新)
问题原因: 1. 之前 _sync_app_config_to_system_app 仅在 derisk.json 的 providers 为空时才从 TOML 同步,当 JSON 已有旧 providers 时不会更新,导致修改 TOML 后重启服务 前端模型列表仍显示旧模型(如 Kimi-k2 返回 404) 2. 模型列表 API 端点优先读取 app_config.agent_llm(PRIORITY 1),但启动时 app_config 中的 agent_llm 未随 TOML 同步更新,即使 agent.llm 已更新, 前端仍读到旧数据 3. 配置文件中 Kimi-k2 模型名大小写错误,百炼 API 模型 ID 区分大小写 改动内容: - 重构 _bootstrap_toml_providers_to_json 为独立函数,TOML 有 providers 时始终 同步到 derisk.json(不再仅限 JSON 为空的情况) - 同步后额外更新 app_config.agent_llm,确保模型列表端点读到最新数据 - 修正 Kimi-k2 → kimi-k2.5/kimi-k2.6(百炼正确模型 ID) - 新增热门模型:deepseek-v3.1、qwen3.5-plus、qwen3.5-flash、qwen3-max、 qwen3-vl-plus(多模态)、qwq-plus(推理) - derisk-proxy-openai.toml 替换为 OpenAI 实际可用模型(gpt-4o、gpt-4o-mini、 o1、o3-mini),移除不兼容的百炼模型 - 多模态模型标记 is_multimodal = true 改动好处: - 用户修改 TOML 配置后重启服务,模型列表自动更新,无需手动清理 derisk.json - 前端模型选择器和模型管理页面能正确显示所有配置的模型 - 模型 ID 与百炼 API 一致,不再出现 404 错误 - 覆盖更多热门模型,用户可直接选用最新模型 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Agent 现在可以通过内置工具感知当前对话用户的身份和权限: - 用户问"我是谁"时,Agent 调用 get_user_info 返回用户名、邮箱、角色、头像 - 用户问"我能做什么"时,Agent 调用 get_user_permissions 返回角色列表及权限详情 实现方式为内置 Tool(非 System Prompt 注入),按需调用、零模板侵入。 数据链路: UserRequest (get_user_from_headers) → build_user_context() → ext_info["user_context"] → AgentContext.extra → ToolAction._execute_tool() → tool context → GetUserInfoTool / GetUserPermissionsTool 主要改动: - 新增 get_user_info / get_user_permissions 内置工具 (BUILTIN_CORE_TOOLS) - ToolContext 新增 user_context 字段 - ToolAction._execute_tool() 修复 context 传递,从 agent_context.extra 提取 user_context - auth.py 新增 build_user_context() / format_permissions_summary() - api_v1.py 聊天端点注入 user_context - 34 个单元测试全部通过
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Agent 支持识别当前用户身份与权限
新增内置工具 get_user_info 和 get_user_permissions,Agent 可按需感知对话用户的身份和权限。用户询问"我是谁"时返回基本信息,询问"我能做什么"时返回角色与权限详情。采用 Tool 方式实现(非 System Prompt 注入),按需调用、零模板侵入,RBAC
未启用时自动降级为管理员模式。
TOML 模型配置自动同步
修复 TOML 配置的模型提供者在重启后不生效的问题。当 derisk.json 的 providers 为空时自动从 TOML bootstrap;进一步重构为 TOML 有 providers 时始终同步到 JSON,确保修改配置后重启即可生效,无需手动清理。同时修正了模型 ID 大小写、补充了热门模型列表。
OAuth2 用户 RBAC 同步修复 + 本地登录
修复 OAuth2 登录后用户无 RBAC 角色导致无权限的系列问题:新老用户登录时均自动分配角色、启动时为存量用户补分配角色、读接口权限从 admin 降为认证即可。新增本地账密登录(bcrypt),前端支持账密登录表单。统一账号角色与 RBAC
角色的同步逻辑,修复 MySQL 不自动同步新增列的问题。
How Has This Been Tested?
Snapshots:
Include snapshots for easier review.
Checklist: