wsprism_gateway/context/
tenant.rs1use std::sync::Arc;
2
3use wsprism_core::error::{Result, WsPrismError};
4
5use crate::{app_state::AppState, policy};
6
7#[derive(Debug, Clone)]
10pub struct SessionMeta {
11 pub tenant_id: String,
13 pub user_id: String,
15 pub session_id: String,
17}
18
19#[derive(Clone)]
21pub struct TenantContext {
22 pub meta: SessionMeta,
23 pub policy: Arc<policy::TenantPolicyRuntime>,
24}
25
26impl TenantContext {
27 pub fn tenant_id(&self) -> &str {
28 &self.meta.tenant_id
29 }
30 pub fn user_id(&self) -> &str {
31 &self.meta.user_id
32 }
33 pub fn session_id(&self) -> &str {
34 &self.meta.session_id
35 }
36}
37
38pub fn resolve_tenant(state: &AppState, tenant_id: &str) -> Result<Arc<policy::TenantPolicyRuntime>> {
40 state
41 .tenant_policy(tenant_id)
42 .ok_or_else(|| WsPrismError::BadRequest(format!("unknown tenant: {tenant_id}")))
43}