Files

46 lines
1.7 KiB
Python
Raw Permalink Normal View History

2026-08-14 16:04:39 +08:00
"""
Auth Worker 插件 — 用户管理(注册 / 资料 / 改密)
2026-08-14 16:04:39 +08:00
auth 域拆分后本插件只保留用户管理功能(可替换的业务部分);
登录 / token 签发 / admin 种子已内置在网关(chongming_auth 包 +
chongming.app.routes.auth),不依赖本插件是否加载。
基于 Plugin 基类统一生命周期:数据库由基类初始化(auth.db,与网关内置
登录共用同一张 users 表),on_cleanup 无需重复 dispose。
2026-08-14 16:04:39 +08:00
"""
import logging
2026-07-23 17:45:44 +08:00
2026-08-14 16:04:39 +08:00
from chongming_worker.plugin import Plugin
2026-07-23 17:45:44 +08:00
2026-08-14 16:04:39 +08:00
from .router import * # noqa: F401,F403 触发 @rpc_method 注册
2026-07-23 17:45:44 +08:00
2026-08-18 13:37:30 +08:00
# 模块加载时即注册 SQLModel 元数据 —— 基类 _bootstrap 建表(create_all)只创建
# 已导入的表,若拖到 on_init 才导入会导致全新库缺少 users 表(no such table)。
# Users 权威定义在 chongming_auth 包,此处 re-export 注册元数据。
2026-08-18 13:37:30 +08:00
from .models.auth import Users # noqa: F401
2026-08-14 16:04:39 +08:00
logger = logging.getLogger(__name__)
2026-07-23 17:45:44 +08:00
2026-08-14 16:04:39 +08:00
class AuthPlugin(Plugin):
"""Auth 插件 — 统一生命周期。"""
2026-07-23 17:45:44 +08:00
2026-08-14 16:04:39 +08:00
type = "auth"
db_name = "auth.db" # 自动在 data_dir 下建 SQLite 库(与网关内置登录共用)
2026-08-14 16:04:39 +08:00
auto_bind = True # 首次初始化时自动 auto_bind_models("auth")
2026-07-23 17:45:44 +08:00
2026-08-14 16:04:39 +08:00
async def on_init(self):
"""首次 create_workers 时调用一次。"""
2026-08-14 16:04:39 +08:00
# 写入全局变量供 router 使用(数据库由基类在 cleanup 时统一 dispose)
self.set_var("db", self.db)
2026-08-14 16:04:39 +08:00
async def on_cleanup(self):
"""cleanup 时调用(数据库由基类处理)。"""
logger.info("[auth] 清理完成")
# 暴露给 app lifespan 的模块级入口(bound method,签名与旧写法完全一致)
plugin = AuthPlugin()
create_workers = plugin.create_workers
cleanup = plugin.cleanup