pub struct TenantContext { /* private fields */ }Expand description
Tenant context for row-level security and data isolation.
Represents a single tenant in a multi-tenant system. All queries executed with this context will be filtered to only include data belonging to this tenant.
Implementations§
Source§impl TenantContext
impl TenantContext
Sourcepub fn created_at_iso8601(&self) -> Option<&str>
pub fn created_at_iso8601(&self) -> Option<&str>
Get the creation timestamp in ISO 8601 format.
Sourcepub fn set_metadata(&mut self, key: impl Into<String>, value: impl Into<String>)
pub fn set_metadata(&mut self, key: impl Into<String>, value: impl Into<String>)
Set metadata key-value pair for the tenant.
Sourcepub fn get_metadata(&self, key: &str) -> Option<&str>
pub fn get_metadata(&self, key: &str) -> Option<&str>
Get metadata value by key.
Sourcepub fn from_jwt_claims(claims: &JsonValue) -> Result<Self, String>
pub fn from_jwt_claims(claims: &JsonValue) -> Result<Self, String>
Create a TenantContext from JWT claims.
Extracts the tenant_id from JWT claims and creates a new TenantContext.
§Arguments
claims- JWT claims as JSON object
§Errors
Returns an error if:
tenant_idclaim is missingtenant_idis not a string
§Example
use serde_json::json;
use fraiseql_core::tenancy::TenantContext;
let claims = json!({
"sub": "user123",
"tenant_id": "acme-corp",
"email": "alice@acme.com"
});
let tenant = TenantContext::from_jwt_claims(&claims).unwrap();
assert_eq!(tenant.id(), "acme-corp");§Errors
Returns a String error if the tenant_id claim is missing or not a string.
Sourcepub fn where_clause(&self) -> String
pub fn where_clause(&self) -> String
Generate a WHERE clause for tenant filtering.
Returns a WHERE clause that restricts data to this tenant. Can be combined with existing WHERE clauses using AND.
§Panics
Panics if the tenant ID contains characters outside the safe set
([A-Za-z0-9._-]). Tenant IDs are validated at context creation
so this should never trigger in practice.
§Security
Prefer where_clause_postgresql or where_clause_parameterized
for production query execution. This method embeds the tenant ID
directly into SQL and is only safe because the ID is strictly validated.
§Example
let tenant = TenantContext::new("acme-corp");
let clause = tenant.where_clause(); // "tenant_id = 'acme-corp'"
assert_eq!(clause, "tenant_id = 'acme-corp'");Sourcepub fn where_clause_postgresql(&self, param_index: usize) -> String
pub fn where_clause_postgresql(&self, param_index: usize) -> String
Generate a parameterized WHERE clause for PostgreSQL.
For use with parameterized queries to prevent SQL injection.
§Arguments
param_index- Parameter placeholder index (1-based for PostgreSQL)
§Example
let tenant = TenantContext::new("acme-corp");
let clause = tenant.where_clause_postgresql(1); // "tenant_id = $1"
assert_eq!(clause, "tenant_id = $1");Sourcepub fn where_clause_parameterized(&self) -> String
pub fn where_clause_parameterized(&self) -> String
Generate a parameterized WHERE clause for MySQL/SQLite.
For use with parameterized queries to prevent SQL injection.
§Example
let tenant = TenantContext::new("acme-corp");
let clause = tenant.where_clause_parameterized(); // "tenant_id = ?"
assert_eq!(clause, "tenant_id = ?");Trait Implementations§
Source§impl Clone for TenantContext
impl Clone for TenantContext
Source§fn clone(&self) -> TenantContext
fn clone(&self) -> TenantContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for TenantContext
impl RefUnwindSafe for TenantContext
impl Send for TenantContext
impl Sync for TenantContext
impl Unpin for TenantContext
impl UnsafeUnpin for TenantContext
impl UnwindSafe for TenantContext
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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