权限系统 #55

Merged
Passthem merged 11 commits from feature/permsystem into master 2026-03-07 17:55:28 +08:00
Owner

PR Type

Enhancement, Tests, Documentation


Description

  • 新增权限系统核心模块与数据库支持

  • 创建权限管理插件 konaperm 供管理员使用

  • 补充单元测试与代码覆盖率工具配置

  • 更新项目文档与 README 使用说明


Diagram Walkthrough

flowchart LR
  Bot[Bot 启动] --> Init[权限系统初始化]
  Init --> DB[(SQLite 数据库)]
  Plugin[业务插件] --> Rule[权限规则检查]
  Rule --> Manager[PermManager]
  Manager --> DB
  Admin[管理员] --> Cmd[konaperm 命令]
  Cmd --> Manager

File Walkthrough

Relevant files
Enhancement
6 files
bot.py
集成权限系统启动钩子                                                                                             
+19/-8   
__init__.py
实现权限管理器与启动逻辑                                                                                         
+108/-0 
entity.py
定义权限实体与事件链转换                                                                                         
+69/-0   
repo.py
实现权限数据库仓库层                                                                                             
+242/-0 
__init__.py
实现数据库迁移管理逻辑                                                                                           
+81/-0   
__init__.py
创建权限管理命令插件                                                                                             
+112/-0 
Tests
2 files
test_permsys.py
添加权限系统单元测试                                                                                             
+105/-0 
conftest.py
配置 pytest 与 nonebug 环境                                                                     
+28/-0   
Documentation
3 files
permsys.md
编写权限系统技术文档                                                                                             
+235/-0 
konaperm.txt
编写权限命令使用说明                                                                                             
+212/-0 
README.md
更新测试与覆盖率说明                                                                                             
+17/-2   
Configuration changes
5 files
justfile
添加测试与覆盖率命令                                                                                             
+5/-0     
.sqls.yml
添加权限数据库连接配置                                                                                           
+6/-0     
mu1_create_permsys_table.sql
创建权限系统数据表结构                                                                                           
+30/-0   
update_perm_info.sql
实现权限信息更新脚本                                                                                             
+4/-0     
check_migrate_version_exists.sql
检查迁移版本表存在性                                                                                             
+7/-0     
Dependencies
1 files
pyproject.toml
更新依赖与 pytest 配置                                                                                   
+15/-5   
Bug fix
1 files
.drone.yml
修复 CI 脚本缩进问题                                                                                         
+4/-10   
Formatting
2 files
__init__.py
清理数据库管理器代码                                                                                             
+22/-9   
test_database.py
修复数据库测试格式问题                                                                                           
+18/-18 
Additional files
7 files
create_migrate_version_table.sql +3/-0     
get_migrate_version.sql +4/-0     
md1_remove_permsys_table.sql +2/-0     
update_migrate_version.sql +2/-0     
create_entity.sql +11/-0   
get_entity_id.sql +8/-0     
get_perm_info.sql +7/-0     

### **PR Type** Enhancement, Tests, Documentation ___ ### **Description** - 新增权限系统核心模块与数据库支持 - 创建权限管理插件 konaperm 供管理员使用 - 补充单元测试与代码覆盖率工具配置 - 更新项目文档与 README 使用说明 ___ ### Diagram Walkthrough ```mermaid flowchart LR Bot[Bot 启动] --> Init[权限系统初始化] Init --> DB[(SQLite 数据库)] Plugin[业务插件] --> Rule[权限规则检查] Rule --> Manager[PermManager] Manager --> DB Admin[管理员] --> Cmd[konaperm 命令] Cmd --> Manager ``` <details> <summary><h3> File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><details><summary>6 files</summary><table> <tr> <td><strong>bot.py</strong><dd><code>集成权限系统启动钩子</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/bot.py">+19/-8</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>__init__.py</strong><dd><code>实现权限管理器与启动逻辑</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/__init__.py">+108/-0</a>&nbsp; </td> </tr> <tr> <td><strong>entity.py</strong><dd><code>定义权限实体与事件链转换</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/entity.py">+69/-0</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>repo.py</strong><dd><code>实现权限数据库仓库层</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/repo.py">+242/-0</a>&nbsp; </td> </tr> <tr> <td><strong>__init__.py</strong><dd><code>实现数据库迁移管理逻辑</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/__init__.py">+81/-0</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>__init__.py</strong><dd><code>创建权限管理命令插件</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/plugins/perm_manage/__init__.py">+112/-0</a>&nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Tests</strong></td><td><details><summary>2 files</summary><table> <tr> <td><strong>test_permsys.py</strong><dd><code>添加权限系统单元测试</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/tests/test_permsys.py">+105/-0</a>&nbsp; </td> </tr> <tr> <td><strong>conftest.py</strong><dd><code>配置 pytest 与 nonebug 环境</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/tests/conftest.py">+28/-0</a>&nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Documentation</strong></td><td><details><summary>3 files</summary><table> <tr> <td><strong>permsys.md</strong><dd><code>编写权限系统技术文档</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/docs/permsys.md">+235/-0</a>&nbsp; </td> </tr> <tr> <td><strong>konaperm.txt</strong><dd><code>编写权限命令使用说明</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/docs/sys/konaperm.txt">+212/-0</a>&nbsp; </td> </tr> <tr> <td><strong>README.md</strong><dd><code>更新测试与覆盖率说明</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/README.md">+17/-2</a>&nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Configuration changes</strong></td><td><details><summary>5 files</summary><table> <tr> <td><strong>justfile</strong><dd><code>添加测试与覆盖率命令</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/justfile">+5/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>.sqls.yml</strong><dd><code>添加权限数据库连接配置</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/.sqls.yml">+6/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>mu1_create_permsys_table.sql</strong><dd><code>创建权限系统数据表结构</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/mu1_create_permsys_table.sql">+30/-0</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>update_perm_info.sql</strong><dd><code>实现权限信息更新脚本</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/sql/update_perm_info.sql">+4/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>check_migrate_version_exists.sql</strong><dd><code>检查迁移版本表存在性</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/check_migrate_version_exists.sql">+7/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Dependencies</strong></td><td><details><summary>1 files</summary><table> <tr> <td><strong>pyproject.toml</strong><dd><code>更新依赖与 pytest 配置</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/pyproject.toml">+15/-5</a>&nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Bug fix</strong></td><td><details><summary>1 files</summary><table> <tr> <td><strong>.drone.yml</strong><dd><code>修复 CI 脚本缩进问题</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/.drone.yml">+4/-10</a>&nbsp; &nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Formatting</strong></td><td><details><summary>2 files</summary><table> <tr> <td><strong>__init__.py</strong><dd><code>清理数据库管理器代码</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/database/__init__.py">+22/-9</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>test_database.py</strong><dd><code>修复数据库测试格式问题</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/tests/test_database.py">+18/-18</a>&nbsp; </td> </tr> </table></details></td></tr><tr><td><strong>Additional files</strong></td><td><details><summary>7 files</summary><table> <tr> <td><strong>create_migrate_version_table.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/create_migrate_version_table.sql">+3/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>get_migrate_version.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/get_migrate_version.sql">+4/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>md1_remove_permsys_table.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/md1_remove_permsys_table.sql">+2/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>update_migrate_version.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/migrates/update_migrate_version.sql">+2/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>create_entity.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/sql/create_entity.sql">+11/-0</a>&nbsp; &nbsp; </td> </tr> <tr> <td><strong>get_entity_id.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/sql/get_entity_id.sql">+8/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> <tr> <td><strong>get_perm_info.sql</strong></td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/sql/get_perm_info.sql">+7/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></details></td></tr></tr></tbody></table> </details> ___
Passthem added 9 commits 2026-03-07 17:43:49 +08:00
Collaborator

PR Reviewer Guide 🔍

(Review updated until commit 24457ff7cd)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵
🧪 PR contains tests
🔒 Security concerns

敏感权限授予:在 konabot/common/permsys/__init__.py 第 86-91 行,启动时会自动将 *(所有权限)授予配置中的 admin_qq_account 账号。这是一个高权限后门,需确保该配置项的安全性,防止未授权访问。
动态 SQL 构造:在 konabot/common/permsys/repo.py 中,部分 SQL 语句(如 IN 子句和 CASE 语句)根据输入列表长度动态构建结构。虽然使用了参数化查询防止注入,但仍需确保输入数据的合法性,避免逻辑错误。

 Recommended focus areas for review

SQL 边界情况

list_perm_info_batch 方法中,当 ordered_ids 为空列表时,order_by_cases 将为空字符串,导致生成的 SQL 语句 ORDER BY (CASE entity_id END) 语法错误。建议添加空列表检查或默认处理逻辑。

order_by_cases = " ".join([f"WHEN ? THEN {i}" for i in range(len(ordered_ids))])

pagecount_sql = f"SELECT COUNT(*) FROM perm_info WHERE entity_id IN ({placeholders}) AND value IS NOT NULL;"
count_cursor = await self.conn.execute(pagecount_sql, tuple(ordered_ids))
total_count = (await count_cursor.fetchone() or (0,))[0]

sql = f"""
    SELECT entity_id, config_key, value
    FROM perm_info
    WHERE entity_id IN ({placeholders})
      AND value IS NOT NULL
    ORDER BY
        (CASE entity_id {order_by_cases} END) ASC,
        config_key ASC
    LIMIT ?
    OFFSET ?;
"""
启动依赖风险

bot.py 现在直接导入并调用 permsys.create_startup()。如果权限系统初始化失败(例如数据库锁死或迁移错误),会导致整个 Bot 无法启动。建议增加异常捕获或降级处理机制。

from konabot.common import permsys

permsys.create_startup()
资源关闭冲突

此文件在 create_startup 中注册了 on_shutdown 钩子来关闭数据库连接,而 bot.py 中也注册了全局的 on_shutdown 钩子。需确认这两个钩子是否存在资源竞争或重复关闭连接的风险,尤其是如果它们管理相同的底层资源。

@driver.on_shutdown
async def _():
    try:
        await db.close_all_connections()
    except Exception:
        pass
## PR Reviewer Guide 🔍 #### (Review updated until commit https://gitea.service.jazzwhom.top/mttu-developers/konabot/commit/24457ff7cdeaecd8d99905adaed65940b61427a5) Here are some key observations to aid the review process: <table> <tr><td>⏱️&nbsp;<strong>Estimated effort to review</strong>: 3 🔵🔵🔵⚪⚪</td></tr> <tr><td>🧪&nbsp;<strong>PR contains tests</strong></td></tr> <tr><td>🔒&nbsp;<strong>Security concerns</strong><br><br> 敏感权限授予:在 `konabot/common/permsys/__init__.py` 第 86-91 行,启动时会自动将 `*`(所有权限)授予配置中的 `admin_qq_account` 账号。这是一个高权限后门,需确保该配置项的安全性,防止未授权访问。 动态 SQL 构造:在 `konabot/common/permsys/repo.py` 中,部分 SQL 语句(如 `IN` 子句和 `CASE` 语句)根据输入列表长度动态构建结构。虽然使用了参数化查询防止注入,但仍需确保输入数据的合法性,避免逻辑错误。</td></tr> <tr><td>⚡&nbsp;<strong>Recommended focus areas for review</strong><br><br> <details><summary><a href='https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/repo.py#L206-L222'><strong>SQL 边界情况</strong></a> 在 `list_perm_info_batch` 方法中,当 `ordered_ids` 为空列表时,`order_by_cases` 将为空字符串,导致生成的 SQL 语句 `ORDER BY (CASE entity_id END)` 语法错误。建议添加空列表检查或默认处理逻辑。 </summary> ```python order_by_cases = " ".join([f"WHEN ? THEN {i}" for i in range(len(ordered_ids))]) pagecount_sql = f"SELECT COUNT(*) FROM perm_info WHERE entity_id IN ({placeholders}) AND value IS NOT NULL;" count_cursor = await self.conn.execute(pagecount_sql, tuple(ordered_ids)) total_count = (await count_cursor.fetchone() or (0,))[0] sql = f""" SELECT entity_id, config_key, value FROM perm_info WHERE entity_id IN ({placeholders}) AND value IS NOT NULL ORDER BY (CASE entity_id {order_by_cases} END) ASC, config_key ASC LIMIT ? OFFSET ?; """ ``` </details> <details><summary><a href='https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/bot.py#L59-L61'><strong>启动依赖风险</strong></a> `bot.py` 现在直接导入并调用 `permsys.create_startup()`。如果权限系统初始化失败(例如数据库锁死或迁移错误),会导致整个 Bot 无法启动。建议增加异常捕获或降级处理机制。 </summary> ```python from konabot.common import permsys permsys.create_startup() ``` </details> <details><summary><a href='https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/__init__.py#L93-L98'><strong>资源关闭冲突</strong></a> 此文件在 `create_startup` 中注册了 `on_shutdown` 钩子来关闭数据库连接,而 `bot.py` 中也注册了全局的 `on_shutdown` 钩子。需确认这两个钩子是否存在资源竞争或重复关闭连接的风险,尤其是如果它们管理相同的底层资源。 </summary> ```python @driver.on_shutdown async def _(): try: await db.close_all_connections() except Exception: pass ``` </details> </td></tr> </table>
Collaborator

PR Code Suggestions

Latest suggestions up to 24457ff
Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
添加空列表边界检查

ordered_ids 为空列表时,placeholders 会生成空字符串,导致 SQL 语法错误。建议在构建 SQL 前检查列表是否为空并提前返回。

konabot/common/permsys/repo.py [201-205]

     async def list_perm_info_batch(
         self, entities: list[PermEntity], pager: PagerQuery
     ) -> PagerResult[tuple[PermEntity, str, bool]]:
         entity_to_id = await self.get_entity_id_batch(entities)
         id_to_entity = {v: k for k, v in entity_to_id.items()}
         ordered_ids = [entity_to_id[e] for e in entities if e in entity_to_id]
 
+        if not ordered_ids:
+            return PagerResult(
+                data=[],
+                success=True,
+                message="",
+                page_count=0,
+                query=pager,
+            )
+
         placeholders = ", ".join("?" * len(ordered_ids))
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies a valid edge case where empty ordered_ids would cause SQL syntax errors. The improved_code properly handles this by returning early. However, as an error handling suggestion, it receives a moderate score.

Medium
General
协调重复的关闭处理程序

bot.pypermsys/__init__.py 都注册了关闭钩子来关闭数据库连接,可能导致重复关闭。建议确认是否需要合并或协调这两个关闭处理程序。

bot.py [63-68]

+    # 注意:permsys 模块已注册独立的 shutdown 钩子关闭其数据库连接
+    # 此处仅关闭全局数据库管理器,避免重复关闭
     @driver.on_shutdown
     async def _():
-        # 关闭全局数据库管理器
         db_manager = get_global_db_manager()
         await db_manager.close_all_connections()
 
-    nonebot.run()
-
-
-if __name__ == "__main__":
-    main()
-
Suggestion importance[1-10]: 4

__

Why: The improved_code only adds a comment without any functional change. Having multiple shutdown hooks is valid in NoneBot and not actually problematic. Per scoring guidelines, suggestions that only add comments should not score above 7.

Low

Previous suggestions

Suggestions up to commit 24457ff
CategorySuggestion                                                                                                                                    Impact
General
使用 elif 替代独立 if 语句

这三个条件判断应该使用 elif 而非独立的 if 语句。虽然当前逻辑不会导致多次更新(因为值互斥),但使用 elif
能更清晰地表达互斥逻辑,避免不必要的后续条件检查,提高代码可读性和执行效率。

konabot/plugins/perm_manage/init.py [106-111]

 if any(i == val.lower() for i in ("y", "yes", "allow", "true", "t")):
     await pm.update_permission(ec[0], perm, True)
-if any(i == val.lower() for i in ("n", "no", "deny", "false", "f")):
+elif any(i == val.lower() for i in ("n", "no", "deny", "false", "f")):
     await pm.update_permission(ec[0], perm, False)
-if any(i == val.lower() for i in ("null", "none")):
+elif any(i == val.lower() for i in ("null", "none")):
     await pm.update_permission(ec[0], perm, None)
Suggestion importance[1-10]: 4

__

Why: 这是一个合理的代码风格改进建议。使用 elif 能更清晰地表达互斥逻辑,但当前代码功能正确,影响较小。

Low
Possible issue
添加异常处理避免重复关闭

create_startup() 中注册的关闭钩子会关闭数据库连接,但 bot.pymain()
函数中也注册了关闭钩子来关闭全局数据库管理器。这可能导致重复关闭或连接管理冲突。建议确保数据库关闭逻辑只在一处处理,或添加状态检查避免重复关闭。

konabot/common/permsys/init.py [92-94]

 @driver.on_shutdown
 async def _():
-    await db.close_all_connections()
+    try:
+        await db.close_all_connections()
+    except Exception:
+        pass  # 避免重复关闭导致的错误
Suggestion importance[1-10]: 3

__

Why: 该建议基于对代码的误解。permsys/__init__.py 中的 db 是独立的 DatabaseManager 实例(管理 perm.sqlite3),与 bot.py 中的全局数据库管理器不同,不会冲突。但添加异常处理作为防御性编程仍有一定价值。

Low
## PR Code Suggestions ✨ <!-- 24457ff --> Latest suggestions up to 24457ff Explore these optional code suggestions: <table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>Possible issue</td> <td> <details><summary>添加空列表边界检查</summary> ___ **当 `ordered_ids` 为空列表时,`placeholders` 会生成空字符串,导致 SQL 语法错误。建议在构建 SQL 前检查列表是否为空并提前返回。** [konabot/common/permsys/repo.py [201-205]](https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/repo.py#L201-L205) ```diff async def list_perm_info_batch( self, entities: list[PermEntity], pager: PagerQuery ) -> PagerResult[tuple[PermEntity, str, bool]]: entity_to_id = await self.get_entity_id_batch(entities) id_to_entity = {v: k for k, v in entity_to_id.items()} ordered_ids = [entity_to_id[e] for e in entities if e in entity_to_id] + if not ordered_ids: + return PagerResult( + data=[], + success=True, + message="", + page_count=0, + query=pager, + ) + placeholders = ", ".join("?" * len(ordered_ids)) ``` <details><summary>Suggestion importance[1-10]: 7</summary> __ Why: The suggestion correctly identifies a valid edge case where empty `ordered_ids` would cause SQL syntax errors. The `improved_code` properly handles this by returning early. However, as an error handling suggestion, it receives a moderate score. </details></details></td><td align=center>Medium </td></tr><tr><td rowspan=1>General</td> <td> <details><summary>协调重复的关闭处理程序</summary> ___ **`bot.py` 和 `permsys/__init__.py` 都注册了关闭钩子来关闭数据库连接,可能导致重复关闭。建议确认是否需要合并或协调这两个关闭处理程序。** [bot.py [63-68]](https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/bot.py#L63-L68) ```diff + # 注意:permsys 模块已注册独立的 shutdown 钩子关闭其数据库连接 + # 此处仅关闭全局数据库管理器,避免重复关闭 @driver.on_shutdown async def _(): - # 关闭全局数据库管理器 db_manager = get_global_db_manager() await db_manager.close_all_connections() - nonebot.run() - - -if __name__ == "__main__": - main() - ``` <details><summary>Suggestion importance[1-10]: 4</summary> __ Why: The `improved_code` only adds a comment without any functional change. Having multiple shutdown hooks is valid in NoneBot and not actually problematic. Per scoring guidelines, suggestions that only add comments should not score above 7. </details></details></td><td align=center>Low </td></tr></tr></tbody></table> ___ #### Previous suggestions <details><summary>Suggestions up to commit 24457ff</summary> <br><table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>General</td> <td> <details><summary>使用 elif 替代独立 if 语句</summary> ___ **这三个条件判断应该使用 <code>elif</code> 而非独立的 <code>if</code> 语句。虽然当前逻辑不会导致多次更新(因为值互斥),但使用 <code>elif</code> <br>能更清晰地表达互斥逻辑,避免不必要的后续条件检查,提高代码可读性和执行效率。** [konabot/plugins/perm_manage/__init__.py [106-111]](https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/plugins/perm_manage/__init__.py#L106-L111) ```diff if any(i == val.lower() for i in ("y", "yes", "allow", "true", "t")): await pm.update_permission(ec[0], perm, True) -if any(i == val.lower() for i in ("n", "no", "deny", "false", "f")): +elif any(i == val.lower() for i in ("n", "no", "deny", "false", "f")): await pm.update_permission(ec[0], perm, False) -if any(i == val.lower() for i in ("null", "none")): +elif any(i == val.lower() for i in ("null", "none")): await pm.update_permission(ec[0], perm, None) ``` <details><summary>Suggestion importance[1-10]: 4</summary> __ Why: 这是一个合理的代码风格改进建议。使用 `elif` 能更清晰地表达互斥逻辑,但当前代码功能正确,影响较小。 </details></details></td><td align=center>Low </td></tr><tr><td rowspan=1>Possible issue</td> <td> <details><summary>添加异常处理避免重复关闭</summary> ___ **<code>create_startup()</code> 中注册的关闭钩子会关闭数据库连接,但 <code>bot.py</code> 的 <code>main()</code> <br>函数中也注册了关闭钩子来关闭全局数据库管理器。这可能导致重复关闭或连接管理冲突。建议确保数据库关闭逻辑只在一处处理,或添加状态检查避免重复关闭。** [konabot/common/permsys/__init__.py [92-94]](https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/permsystem/konabot/common/permsys/__init__.py#L92-L94) ```diff @driver.on_shutdown async def _(): - await db.close_all_connections() + try: + await db.close_all_connections() + except Exception: + pass # 避免重复关闭导致的错误 ``` <details><summary>Suggestion importance[1-10]: 3</summary> __ Why: 该建议基于对代码的误解。`permsys/__init__.py` 中的 `db` 是独立的 `DatabaseManager` 实例(管理 `perm.sqlite3`),与 `bot.py` 中的全局数据库管理器不同,不会冲突。但添加异常处理作为防御性编程仍有一定价值。 </details></details></td><td align=center>Low </td></tr></tr></tbody></table> </details>
Passthem added 2 commits 2026-03-07 17:51:50 +08:00
Collaborator

Persistent review updated to latest commit 24457ff7cd

**[Persistent review](https://gitea.service.jazzwhom.top/mttu-developers/konabot/pulls/55#issuecomment-296)** updated to latest commit https://gitea.service.jazzwhom.top/mttu-developers/konabot/commit/24457ff7cdeaecd8d99905adaed65940b61427a5
Passthem merged commit 7e8fa45f36 into master 2026-03-07 17:55:28 +08:00
Sign in to join this conversation.
No description provided.