Multi-Tenancy
OPBX is multi-tenant. Every data record belongs to exactly one organization, and the API enforces that boundary automatically. This document explains the scope mechanism, the bypass pattern, and the platform manager role.
Organization Isolation
All tenant models use Laravel's #[ScopedBy([OrganizationScope::class])] attribute. When any query runs, OrganizationScope automatically adds a where organization_id = :current_org clause.
- If a user is authenticated, the scope reads
auth()->user()->organization_id. An API key is its own principal and also carriesorganization_id, so key requests are scoped the same way. - If no user is authenticated, the scope forces
where 1 = 0, returning no rows. In console commands and jobs this also logs a warning (OrganizationScope: console query without bypass matches zero rows). - Webhooks and CLI commands have no authenticated user, so they must explicitly bypass the scope.
This prevents one organization from reading another organization's extensions, DIDs, call logs, or any other tenant data.
OrganizationScope
App\Scopes\OrganizationScope is a global Eloquent scope applied to every tenant model.
Key Behaviors
| Scenario | Behavior |
|---|---|
| Authenticated user | Query is scoped to the user's organization_id. |
| Unauthenticated request | Query returns zero rows (where 1 = 0). |
Inside OrganizationScope::bypass() | Scope is disabled for the duration of the callback. |
The bypass implementation uses a counter, so nested bypass calls are safe:
OrganizationScope::bypass(function () {
// All queries inside here ignore organization_id scoping.
return DidNumber::where('phone_number', $number)->first();
});
When to Bypass
Bypass the scope only when there is no authenticated user context or when a platform manager needs cross-tenant access:
- Webhook handlers (Cloudonix sends requests, no user session)
- Auto-dialer CDR processing and AMD action callbacks
- Call notification dispatching
- Platform administration
- Artisan commands and background jobs that operate across organizations
Forgetting to bypass OrganizationScope inside a webhook handler will cause the handler to return zero rows for every lookup, silently breaking routing, CDRs, and campaign updates.
EnsureTenantScope Middleware
App\Http\Middleware\EnsureTenantScope (alias tenant.scope) validates the tenant context on API routes. It is applied to the protected API route group in routes/api.php.
What It Does
- Confirms the request is authenticated.
- Allows platform managers to pass through without an organization check.
- Confirms the user has an
organization_id. - Confirms the organization exists and is active (not
suspendedordeleted). - Falls back to
isActive()for legacy compatibility.
Error Responses
| Condition | HTTP Status | Message |
|---|---|---|
| Unauthenticated | 401 | Unauthenticated. |
| No organization | 403 | User does not belong to an organization. |
| Organization not found | 403 | Organization not found. |
| Organization suspended | 403 | Your organization has been suspended. |
| Organization deleted | 403 | Organization is not active. |
Platform Manager Role
A platform manager is a user with is_platform_manager = true. Platform managers operate above the tenant boundary and can manage multiple organizations through the platform API.
Capabilities
- Access platform admin routes in
routes/platform.php(/api/v1/platform/*, middlewareauth:sanctum,platform.managerandbypass.organization.scope). - Bypass
EnsureTenantScopeorganization checks. - Manage organizations, users, and audit logs across tenants.
What Platform Managers Are Not
Platform managers are not regular tenant users. On the normal API they see their own organization's data. Cross-tenant access happens in one of two ways:
- Platform routes. The
bypass.organization.scopemiddleware runs the whole request insideOrganizationScope::bypass(), so every query on/api/v1/platform/*sees all organizations. - Operate as an organization. On the tenant API, a platform manager can send the
X-Operate-As-Organization: <organization id>header. Theoperate.asmiddleware (aftertenant.scope, beforerate_limit_org:api) swaps the request's user for an in-memory copy with thatorganization_id, the Owner role andis_platform_manager = false, so scoping and policies follow the target organization. The copy is never saved. The header from anyone else (including an API key) returns 403OPERATE_AS_FORBIDDEN; an unknown organization returns 404OPERATE_AS_ORG_NOT_FOUND; an organization that is not active returns 403OPERATE_AS_ORG_INACTIVE. Without a platform-manager token the header does nothing.
Role Hierarchy Inside an Organization
Within each organization, users have one of five roles. OrganizationScope isolates data between organizations; roles isolate permissions within an organization.
| Role | Scope |
|---|---|
| Owner | Full access to the organization, including organization settings and all users. |
| PBX Admin | Manage extensions, DIDs, routing, and most settings. Can manage only PBX User and Reporter accounts. |
| PBX User | Limited access; can update own extension and view assigned resources. |
| Reporter | Read-only access to call logs and reports. |
| Supervisor | Read-only access plus live monitoring and coaching of the users, extensions and ring groups assigned to them. |
Data Model
Organization
App\Models\Organization represents a tenant. Key attributes include:
name,slug,timezonestatus—active,suspended, ordeleted- One-to-one
cloudonixSettingsrelationship - One-to-many
users
CloudonixSettings
App\Models\CloudonixSettings stores the Cloudonix domain and API credentials for one organization. It is the lookup table that maps a Cloudonix domain to an organization_id during webhook authentication.
User
App\Models\User belongs to exactly one organization via organization_id. It also has:
role— Owner, PBX Admin, PBX User, Reporter, Supervisoris_platform_manager— cross-tenant admin flagstatus—active,inactiveorpending
Common Patterns
Scoped Query (Default)
// Automatically filtered to the authenticated user's organization.
$extensions = Extension::all();
Bypass for Webhooks
// Inside CloudonixWebhookController::sessionUpdate
$settings = OrganizationScope::bypass(function () use ($organizationId) {
return CallNotificationsSettings::forOrganization($organizationId)
->active()
->first();
});
Platform Manager Cross-Tenant Query
// Platform manager listing organizations does not need bypass,
// because the Organization model itself is not tenant-scoped.
$organizations = Organization::query()->paginate();