TenantProvisioningService
in package
Provisions tenant metadata, database resources, migrations, and lifecycle state.
Table of Contents
Constants
- PLATFORM_CONNECTION : mixed = 'platform'
- TENANT_CONNECTION : mixed = 'tenant_provision'
Properties
- $platformConnection : Connection|null
Methods
- __construct() : mixed
- provision() : TenantProvisioningResult
- Provision a tenant. Database creation and migrations require CLI/worker context.
- assertDedicatedRuntimeRole() : void
- Reject reuse of the platform/default runtime credentials before provisioning writes.
- assertValidBlobContainer() : void
- Validate Azure blob container naming.
- assertValidHost() : void
- Validate DNS hostname input.
- assertValidIdentifier() : void
- Validate PostgreSQL identifier input.
- assertValidSlug() : void
- Validate tenant slug format.
- assertWorkerContext() : void
- Prevent privileged database work from normal HTTP requests.
- configureNamedTenantConnection() : void
- Configure a named tenant datasource for privilege checks.
- configureTenantConnection() : void
- Configure the tenant provisioning datasource.
- ensureInitialSuperUser() : void
- Create the initial tenant Admin account so the tenant can claim access through forgot password.
- ensureKingdomBranch() : int
- Ensure the tenant root branch exists for the initial super-user account.
- ensureMemberRole() : void
- Ensure the initial member has the Super User role.
- ensurePassword() : SensitiveString
- Ensure the tenant database password exists in the configured secret store.
- ensurePermission() : int
- Ensure the permission that marks a role as super-user exists.
- ensureRolePermission() : void
- Ensure the Super User role grants super-user permission.
- ensureSuperUserMember() : int
- Ensure the initial member account exists with no known password.
- ensureSuperUserRole() : int
- Ensure the initial Super User role exists.
- envFlag() : bool
- Read a boolean environment flag.
- generatePassword() : string
- Generate a tenant database password.
- generatePublicId() : string
- Generate an 8-character public ID that is unique in the tenant table.
- markTenantStatus() : void
- Mark final tenant lifecycle state after provisioning.
- maybeCreateDatabase() : void
- Create or update PostgreSQL tenant database resources.
- now() : string
- Return current UTC database timestamp.
- platformConnection() : Connection
- Return the platform metadata connection.
- pluginsWithMigrations() : array<int, string>
- progress() : void
- Emit progress when a callback is available.
- readSchemaVersion() : string|null
- Read the latest tenant migration version.
- runTenantMigrations() : string|null
- Configure and run tenant app/plugin migrations.
- shouldCreateDatabase() : bool
- Determine whether automatic tenant database creation is enabled.
- smokeTestTenantDatabase() : void
- Smoke-test the migrated tenant database.
- upsertTenantHost() : void
- Create or update the primary host row for a tenant.
- upsertTenantMetadata() : array<string, mixed>
- validateRequest() : void
- Validate tenant provisioning input before side effects.
Constants
PLATFORM_CONNECTION
private
mixed
PLATFORM_CONNECTION
= 'platform'
TENANT_CONNECTION
private
mixed
TENANT_CONNECTION
= 'tenant_provision'
Properties
$platformConnection read-only
private
Connection|null
$platformConnection
= null
Methods
__construct()
public
__construct([Connection|null $platformConnection = null ]) : mixed
Parameters
- $platformConnection : Connection|null = null
-
Optional platform connection override
provision()
Provision a tenant. Database creation and migrations require CLI/worker context.
public
provision(TenantProvisioningRequest $request[, callable|null $commandRunner = null ][, callable|null $progress = null ]) : TenantProvisioningResult
Parameters
- $request : TenantProvisioningRequest
- $commandRunner : callable|null = null
-
Callable receiving (object|string $command, list
$args): int|null - $progress : callable|null = null
-
Callable receiving (string $level, string $message): void
Return values
TenantProvisioningResultassertDedicatedRuntimeRole()
Reject reuse of the platform/default runtime credentials before provisioning writes.
private
assertDedicatedRuntimeRole(TenantProvisioningRequest $request, bool $alreadyRegistered) : void
Parameters
- $request : TenantProvisioningRequest
- $alreadyRegistered : bool
assertValidBlobContainer()
Validate Azure blob container naming.
private
assertValidBlobContainer(string $container) : void
Parameters
- $container : string
assertValidHost()
Validate DNS hostname input.
private
assertValidHost(string $host) : void
Parameters
- $host : string
assertValidIdentifier()
Validate PostgreSQL identifier input.
private
assertValidIdentifier(string $identifier, string $label) : void
Parameters
- $identifier : string
- $label : string
assertValidSlug()
Validate tenant slug format.
private
assertValidSlug(string $slug) : void
Parameters
- $slug : string
assertWorkerContext()
Prevent privileged database work from normal HTTP requests.
private
assertWorkerContext(TenantProvisioningRequest $request) : void
Parameters
- $request : TenantProvisioningRequest
configureNamedTenantConnection()
Configure a named tenant datasource for privilege checks.
private
configureNamedTenantConnection(string $connectionName, string $dbName, string $dbRole, SensitiveString $password) : void
Parameters
- $connectionName : string
- $dbName : string
- $dbRole : string
- $password : SensitiveString
configureTenantConnection()
Configure the tenant provisioning datasource.
private
configureTenantConnection(string $dbServer, string $dbName, string $dbRole, SensitiveString $password) : void
Parameters
- $dbServer : string
- $dbName : string
- $dbRole : string
- $password : SensitiveString
ensureInitialSuperUser()
Create the initial tenant Admin account so the tenant can claim access through forgot password.
private
ensureInitialSuperUser(TenantProvisioningRequest $request, callable|null $progress) : void
Parameters
- $request : TenantProvisioningRequest
- $progress : callable|null
ensureKingdomBranch()
Ensure the tenant root branch exists for the initial super-user account.
private
ensureKingdomBranch(Connection $connection, string $now) : int
Parameters
- $connection : Connection
- $now : string
Return values
intensureMemberRole()
Ensure the initial member has the Super User role.
private
ensureMemberRole(Connection $connection, int $memberId, int $roleId, int $branchId, string $now) : void
Parameters
- $connection : Connection
- $memberId : int
- $roleId : int
- $branchId : int
- $now : string
ensurePassword()
Ensure the tenant database password exists in the configured secret store.
private
ensurePassword(string $secretName, bool $rotate) : SensitiveString
Parameters
- $secretName : string
- $rotate : bool
Return values
SensitiveStringensurePermission()
Ensure the permission that marks a role as super-user exists.
private
ensurePermission(Connection $connection, string $now) : int
Parameters
- $connection : Connection
- $now : string
Return values
intensureRolePermission()
Ensure the Super User role grants super-user permission.
private
ensureRolePermission(Connection $connection, int $roleId, int $permissionId, string $now) : void
Parameters
- $connection : Connection
- $roleId : int
- $permissionId : int
- $now : string
ensureSuperUserMember()
Ensure the initial member account exists with no known password.
private
ensureSuperUserMember(Connection $connection, TenantProvisioningRequest $request, int $branchId, string $now) : int
Parameters
- $connection : Connection
- $request : TenantProvisioningRequest
- $branchId : int
- $now : string
Return values
intensureSuperUserRole()
Ensure the initial Super User role exists.
private
ensureSuperUserRole(Connection $connection, string $now) : int
Parameters
- $connection : Connection
- $now : string
Return values
intenvFlag()
Read a boolean environment flag.
private
envFlag(string $name) : bool
Parameters
- $name : string
Return values
boolgeneratePassword()
Generate a tenant database password.
private
generatePassword() : string
Return values
stringgeneratePublicId()
Generate an 8-character public ID that is unique in the tenant table.
private
generatePublicId(Connection $connection, string $table) : string
Parameters
- $connection : Connection
- $table : string
Return values
stringmarkTenantStatus()
Mark final tenant lifecycle state after provisioning.
private
markTenantStatus(Connection $connection, string $tenantId, string $status, string|null $schemaVersion) : void
Parameters
- $connection : Connection
- $tenantId : string
- $status : string
- $schemaVersion : string|null
maybeCreateDatabase()
Create or update PostgreSQL tenant database resources.
private
maybeCreateDatabase(Connection $platform, TenantProvisioningRequest $request, SensitiveString $password, callable|null $progress) : void
Parameters
- $platform : Connection
- $request : TenantProvisioningRequest
- $password : SensitiveString
- $progress : callable|null
now()
Return current UTC database timestamp.
private
now() : string
Return values
stringplatformConnection()
Return the platform metadata connection.
private
platformConnection() : Connection
Return values
ConnectionpluginsWithMigrations()
private
pluginsWithMigrations() : array<int, string>
Return values
array<int, string>progress()
Emit progress when a callback is available.
private
progress(callable|null $progress, string $level, string $message) : void
Parameters
- $progress : callable|null
- $level : string
- $message : string
readSchemaVersion()
Read the latest tenant migration version.
private
readSchemaVersion() : string|null
Return values
string|nullrunTenantMigrations()
Configure and run tenant app/plugin migrations.
private
runTenantMigrations(TenantProvisioningRequest $request, SensitiveString $password, callable $commandRunner, callable|null $progress) : string|null
Parameters
- $request : TenantProvisioningRequest
- $password : SensitiveString
- $commandRunner : callable
-
Callable receiving (object|string $command, list
$args): int|null - $progress : callable|null
Return values
string|nullshouldCreateDatabase()
Determine whether automatic tenant database creation is enabled.
private
shouldCreateDatabase(TenantProvisioningRequest $request, Connection $platform) : bool
Parameters
- $request : TenantProvisioningRequest
- $platform : Connection
Return values
boolsmokeTestTenantDatabase()
Smoke-test the migrated tenant database.
private
smokeTestTenantDatabase(string $table, callable|null $progress) : void
Parameters
- $table : string
- $progress : callable|null
upsertTenantHost()
Create or update the primary host row for a tenant.
private
upsertTenantHost(Connection $connection, string $tenantId, string $host) : void
Parameters
- $connection : Connection
- $tenantId : string
- $host : string
upsertTenantMetadata()
private
upsertTenantMetadata(Connection $connection, TenantProvisioningRequest $request) : array<string, mixed>
Parameters
- $connection : Connection
- $request : TenantProvisioningRequest
Return values
array<string, mixed>validateRequest()
Validate tenant provisioning input before side effects.
private
validateRequest(TenantProvisioningRequest $request, Connection $platform) : void
Parameters
- $request : TenantProvisioningRequest
- $platform : Connection