#[non_exhaustive]pub struct ActionContext {
pub request: Request,
pub request_meta: RequestMeta,
pub request_id: RequestId,
pub module: Option<String>,
/* private fields */
}Expand description
Action 执行上下文
包含 Action 执行所需的所有信息,包括请求数据、传输元数据、用户信息、应用资源和表配置。
标注 #[non_exhaustive]:未来新增运行期字段(如 trace_id)不构成破坏性变更,
调用方请用 ActionContext::new 构造,而非结构体字面量。
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.request: Request请求数据
request_meta: RequestMeta与具体 Web 框架无关的传输元数据。
request_id: RequestId本次派发的运行期标识,用于串联日志/span/metrics/审计。
由 new 默认生成;RequestIdMiddleware 在洋葱链
最外层会按上游 X-Request-Id 透传或重新生成并 span.record。
module: Option<String>本次派发所属模块名(由 Registry::dispatch 注入)。
仅用于可观测性标注(metrics module 标签、日志)。模块数量有界,作为 metrics
标签不构成高基数问题。未经路由直接构造的上下文为 None。
Implementations§
Source§impl ActionContext
impl ActionContext
Sourcepub fn http(&self) -> Result<&HttpClient, BaseError>
pub fn http(&self) -> Result<&HttpClient, BaseError>
获取当前应用配置的 HTTP 客户端(http feature)。
委托给 Tools::http:未配置时返回 BaseError::HttpClientNotInitialized,
Tools 生命周期已结束时返回对应错误。
Sourcepub fn request_context(&mut self) -> &mut RequestContext
pub fn request_context(&mut self) -> &mut RequestContext
返回当前请求独占的类型化上下文。
Sourcepub fn with_tenant(self, tenant: TenantContext) -> Self
pub fn with_tenant(self, tenant: TenantContext) -> Self
注入普通租户 capability。
Sourcepub fn tenant(&self) -> Result<TenantContext, BaseError>
pub fn tenant(&self) -> Result<TenantContext, BaseError>
返回不可选的普通租户 capability。
Sourcepub fn system_tenant(&self) -> Result<SystemTenantCapability, BaseError>
pub fn system_tenant(&self) -> Result<SystemTenantCapability, BaseError>
返回当前请求已获授的系统级租户 capability。
Sourcepub fn actor(&self) -> Result<ActorContext, BaseError>
pub fn actor(&self) -> Result<ActorContext, BaseError>
返回当前操作者上下文。
Sourcepub fn request_id(&self) -> RequestId
pub fn request_id(&self) -> RequestId
返回请求标识。
Sourcepub fn authenticated_user(&self) -> Option<&User>
pub fn authenticated_user(&self) -> Option<&User>
获取已认证用户的只读引用。
外部调用方只能读取认证结果,不能手动注入用户;用户注入仅由 crate 内受信任
中间件(如 TokenAuthMiddleware)完成。
Sourcepub fn ui_catalog(&self) -> Result<UiCatalog, BaseError>
pub fn ui_catalog(&self) -> Result<UiCatalog, BaseError>
按当前请求认证身份返回可访问的版本化 UI 目录。
目录过滤与真实 Action dispatch 复用同一份构建期授权策略。
Sourcepub fn with_request_id(self, request_id: RequestId) -> Self
pub fn with_request_id(self, request_id: RequestId) -> Self
设置本次派发的 request_id(链式调用)
通常由 RequestIdMiddleware 在洋葱链最外层调用以透传上游标识;
业务侧一般无需手动设置。
Sourcepub fn with_request_meta(self, request_meta: RequestMeta) -> Self
pub fn with_request_meta(self, request_meta: RequestMeta) -> Self
注入由传输适配器构造的请求元数据(链式调用)。
Sourcepub fn with_module(self, module: impl Into<String>) -> Self
pub fn with_module(self, module: impl Into<String>) -> Self
设置所属模块名(链式调用)。
由 Registry::dispatch 注入,用于 metrics module 标签等可观测性标注。
Sourcepub fn dispatch_target(&self) -> Option<(&str, &str)>
pub fn dispatch_target(&self) -> Option<(&str, &str)>
返回当前实际派发的 module 与 action。
该身份由 Registry 在进入中间件链前覆盖注入,可供安全中间件绑定目标;
未经 Registry 派发而直接构造的上下文返回 None。
Sourcepub fn with_table_definition(self, definition: TableDefinition) -> Self
pub fn with_table_definition(self, definition: TableDefinition) -> Self
设置表定义(链式调用)。
Sourcepub fn table_definition(&self) -> Result<&TableDefinition, BaseError>
pub fn table_definition(&self) -> Result<&TableDefinition, BaseError>
返回当前 Action 绑定的表定义。
Sourcepub fn table_query(&self) -> Result<TableQuery, BaseError>
pub fn table_query(&self) -> Result<TableQuery, BaseError>
创建默认失败关闭的表查询构建器。
带 tenant key 的表必须存在普通 TenantContext,并始终自动施加租户条件。
系统身份不会隐式绕过;全域访问必须显式调用
ActionContext::system_table_query。
Sourcepub fn system_table_query(
&self,
capability: SystemTenantCapability,
) -> Result<TableQuery, BaseError>
pub fn system_table_query( &self, capability: SystemTenantCapability, ) -> Result<TableQuery, BaseError>
使用显式、已绑定当前请求 actor 的系统 capability 创建全域表查询。
调用方必须先通过 ActionContext::system_tenant 取得 capability,再在具体
repository 旁路中显式传回;普通 CRUD 只调用 ActionContext::table_query,
因而不会自动获得全租户访问。
Sourcepub fn system_tables(
&self,
capability: SystemTenantCapability,
) -> Result<Tables, BaseError>
pub fn system_tables( &self, capability: SystemTenantCapability, ) -> Result<Tables, BaseError>
使用显式系统 capability 创建全域 Tables 入口。
Sourcepub async fn begin_transaction(&self) -> Result<Transaction, BaseError>
pub async fn begin_transaction(&self) -> Result<Transaction, BaseError>
开启一个数据库事务(受保护层多步写的原子作用域)
返回 yang_db::Transaction,可传入 TableQuery 的 *_in_tx 系列方法
(insert_in_tx/update_in_tx/delete_in_tx/select_in_tx 等),使受
权限/校验/软删保护的多步写在同一事务内原子提交或整体回滚。调用方负责
显式 commit();若 Transaction 在未提交时被 drop,sqlx 会尽力回滚。
仅在启用 mysql feature 时可用。
§返回
Ok(Transaction):活动事务Err(BaseError::DatabaseNotInitialized):当前应用未配置数据库Err(BaseError::DatabaseTransactionFailed):开启事务失败
§示例
let mut tx = ctx.begin_transaction().await?;
let id = ctx.table_query()?.insert_returning_id_in_tx(&mut tx, parent).await?.1;
ctx.table_query()?.insert_in_tx(&mut tx, child_with(id)).await?;
tx.commit().await?;Sourcepub fn user_roles(&self) -> Vec<String>
pub fn user_roles(&self) -> Vec<String>
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ActionContext
impl !UnwindSafe for ActionContext
impl Freeze for ActionContext
impl Send for ActionContext
impl Sync for ActionContext
impl Unpin for ActionContext
impl UnsafeUnpin for ActionContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more