ORE Studio 0.0.4
Loading...
Searching...
No Matches
Static Public Member Functions | Static Public Attributes | List of all members
tenant_context Class Referencefinal

Manages tenant context for multi-tenant database operations. More...

#include <tenant_context.hpp>

Collaboration diagram for tenant_context:
Collaboration graph

Static Public Member Functions

static context with_tenant (const context &ctx, const std::string &tenant)
 Creates a new context with the specified tenant.
 
static context with_system_tenant (const context &ctx)
 Creates a new context with the system tenant.
 
static utility::uuid::tenant_id lookup_by_code (const context &ctx, const std::string &code)
 Looks up a tenant ID by its code.
 
static utility::uuid::tenant_id lookup_by_hostname (const context &ctx, const std::string &hostname)
 Looks up a tenant ID by its hostname.
 
static std::string lookup_name (const context &ctx, const utility::uuid::tenant_id &tenant_id)
 Looks up a tenant name by its ID.
 
static bool is_uuid (const std::string &str)
 Checks if a string is a valid UUID format.
 

Static Public Attributes

static constexpr const char * system_tenant_id
 The UUID of the system tenant (max UUID per RFC 9562).
 

Detailed Description

Manages tenant context for multi-tenant database operations.

This class provides utilities for setting and managing the current tenant context on database connections. In a multi-tenant environment, all database operations must be performed within a tenant context to ensure proper row-level security enforcement.

Member Function Documentation

◆ with_tenant()

context with_tenant ( const context ctx,
const std::string &  tenant 
)
static

Creates a new context with the specified tenant.

Accepts either a tenant code (e.g., "system", "acme") or a tenant UUID. If a code is provided, looks up the corresponding tenant_id from the database. The returned context will set the session variable app.current_tenant_id on connection acquisition, which is used by row-level security policies.

Parameters
ctxThe source context (provides pool and credentials).
tenantTenant code or UUID string.
Returns
A new context configured for the specified tenant.
Exceptions
std::runtime_errorif tenant not found.
Here is the caller graph for this function:

◆ with_system_tenant()

context with_system_tenant ( const context ctx)
static

Creates a new context with the system tenant.

Convenience method that creates a context for the system tenant (ffffffff-ffff-ffff-ffff-ffffffffffff, the max UUID).

Parameters
ctxThe source context (provides pool and credentials).
Returns
A new context configured for the system tenant.
Here is the caller graph for this function:

◆ lookup_by_code()

utility::uuid::tenant_id lookup_by_code ( const context ctx,
const std::string &  code 
)
static

Looks up a tenant ID by its code.

Parameters
ctxThe database context to query.
codeThe tenant code (e.g., "system", "acme").
Returns
The tenant ID.
Exceptions
std::runtime_errorif tenant not found.

◆ lookup_by_hostname()

utility::uuid::tenant_id lookup_by_hostname ( const context ctx,
const std::string &  hostname 
)
static

Looks up a tenant ID by its hostname.

Parameters
ctxThe database context to query.
hostnameThe tenant hostname (e.g., "localhost", "acme.example.com").
Returns
The tenant ID.
Exceptions
std::runtime_errorif tenant not found.

◆ lookup_name()

std::string lookup_name ( const context ctx,
const utility::uuid::tenant_id tenant_id 
)
static

Looks up a tenant name by its ID.

Returns "System" for the system tenant (max UUID), otherwise queries the database for the tenant name.

Parameters
ctxThe database context to query.
tenant_idThe tenant ID.
Returns
The tenant name.
Exceptions
std::runtime_errorif tenant not found.

◆ is_uuid()

bool is_uuid ( const std::string &  str)
static

Checks if a string is a valid UUID format.

Parameters
strThe string to check.
Returns
true if the string matches UUID format, false otherwise.

Member Data Documentation

◆ system_tenant_id

constexpr const char* system_tenant_id
staticconstexpr
Initial value:
=
"ffffffff-ffff-ffff-ffff-ffffffffffff"

The UUID of the system tenant (max UUID per RFC 9562).

Uses max UUID instead of nil UUID to prevent confusion with uninitialized UUIDs (boost::uuids::uuid default-constructs to nil).