ForcePilot/backend/package/yuxi/channels/contract/dtos/service_account.py
Kris b10cb080de refactor: 多模块代码优化与功能增强
本次提交包含多项代码改进与功能增强:
1. 新增服务账号禁用错误类型与状态映射
2. 优化出站管道阶段顺序与上下文字段
3. 完善会话与事务管理逻辑,修复并发冲突处理
4. 调整钉钉与微信插件的适配逻辑
5. 优化内容校验与限流降级策略
6. 新增渠道降级投递事件与围栏回滚机制
7. 修正配置schema与权限校验逻辑
8. 完善审计与监控相关的日志与钩子处理
2026-07-09 13:51:58 +08:00

81 lines
3.0 KiB
Python
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.

"""服务账号 DTO。
定义服务账号及其创建命令的跨层共享不可变值对象。服务账号是渠道账户绑定的
系统 Useruser_type=service作为 Agent 执行的鉴权主体,满足会话归属
与用户存在性校验。所有 DTO 均为 ``dataclass(frozen=True)``,仅依赖标准库
与契约层内部类型。
"""
from __future__ import annotations
from dataclasses import dataclass
from yuxi.channels.contract.dtos.channel import ChannelType
from yuxi.channels.contract.errors import ValidationError
@dataclass(frozen=True)
class ServiceAccount:
"""服务账号 DTO契约层共享值对象
服务账号是渠道账户绑定的系统 Useruser_type=service作为 Agent
执行的鉴权主体,满足会话归属与用户存在性校验。
字段:
uid: 系统用户 UIDUser 表主键标识)。
username: 用户名(系统登录名)。
channel_type: 渠道类型,标识该服务账号所属渠道。
account_id: 渠道账户 ID关联 ChannelAccount。
department_id: 所属部门 ID可选
status: 服务账号状态(默认 ``active````disabled`` 表示账号已被禁用。
"""
uid: str
username: str
channel_type: ChannelType
account_id: str
department_id: int | None = None
status: str = "active"
@dataclass(frozen=True)
class CreateServiceAccountCmd:
"""创建服务账号命令 DTO。
在 ``__post_init__`` 中校验必填字段与业务规则,在构造时即拦截非法值,
避免空值传播到仓储层后才暴露INV-8
字段:
channel_type: 渠道类型,标识服务账号所属渠道。
account_id: 渠道账户 ID关联 ChannelAccount。
department_id: 所属部门 ID可选
uid: 服务账号唯一标识,格式 ``svc:channel:{channel_type}:{account_id}``。
由应用层调 ``ServiceAccount.create()`` 聚合根工厂生成后传入,
适配器直接消费不再 import core.model§4.6 / INV-8
username: 服务账号用户名,格式
``svc_channel_{channel_type}_{account_id_short}``
account_id_short 为 account_id 的 sha256 前 8 位。
"""
channel_type: ChannelType
account_id: str
uid: str
username: str
department_id: int | None = None
def __post_init__(self) -> None:
"""校验必填字段非空。
``channel_type`` 不可为 ``None````account_id`` / ``uid`` /
``username`` 必须为非空字符串;违规抛 ``ValidationError``,在构造时
即拦截INV-8
"""
if self.channel_type is None:
raise ValidationError("channel_type", "must not be None")
if not self.account_id:
raise ValidationError("account_id", "must not be empty")
if not self.uid:
raise ValidationError("uid", "must not be empty")
if not self.username:
raise ValidationError("username", "must not be empty")