/engineering:multi-tenant-saas-architecturefrom engineering
Guides designing tenant isolation, panel boundaries, zero-trust authorization, audit trails, and tenant permission overrides for SaaS applications.
Ships in engineering. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Acknowledgement: Shared by Peter Bamuhigire, techguypeter.com, +256 784 464178.
| Input | Required | Use |
|---|---|---|
| Tenant, product, and lifecycle scope | yes | Bound the SaaS decision |
| Current architecture, plans, policies, and constraints | yes | Preserve enforceable behaviour |
| Production data or verified evidence | conditional | Validate thresholds and migrations |
Skill-specific inputs:
system-architecture-design.database-design-engineering (so isolation model matches real queries).vibe-security-skill.Default to read-only analysis. Change configuration, billing, identity, tenant data, infrastructure, or customer communications only with explicit authority, least-privilege credentials, tenant scope, rollback, and auditable approval. Never expose secrets or cross tenant boundaries.
If production access, policy, telemetry, or authoritative records are unavailable, produce a labelled design or dry-run plan. Do not claim deployment, reconciliation, deletion, delivery, or measured outcomes; list missing evidence and verification.
| Condition | Action | Stop condition |
|---|---|---|
| Tenant isolation, money, identity, or deletion is affected | Require approval and rollback evidence | Scope or authority is ambiguous |
| Evidence supports a reversible change | Stage, test, and record it | Acceptance checks fail |
| Only partial context is available | Return assumptions and validation | A production claim cannot be verified |
COUNT(*)+1, MAX+1, the row id or one counter shared across tenants or with platform billing. Fix: one contiguous, never-reset series per tenant and document type through tenant-document-numbering.tenant_id plumbing that downstream database, API, security, and delivery skills rely on.modular-saas-architecture.database-design-engineering and mysql-best-practices.Skill-specific outputs:
| Artifact | Consumed by | Template |
|---|---|---|
Tenant-isolation map (isolation model + tenant_id rules per table) | database-design-engineering, api-design-first | inline (this skill) |
| Panel definitions (super admin, tenant admin, end user) | api-design-first, kubernetes-saas-delivery | inline |
| Permission priority model (user/tenant override/role/default deny) | vibe-security-skill, api-design-first | inline + references/permission-model.md |
| Session and JWT plan (prefixing, lifetimes, rotation) | api-design-first, vibe-security-skill | inline |
| Audit event set and retention policy | observability-monitoring, reliability-engineering | inline |
| Migration plan (single-tenant to multi-tenant) | database-design-engineering, deployment-release-engineering | documentation/migration.md |
| Category | Artifact | Format | Example |
|---|---|---|---|
| Data safety | Tenant isolation note | Markdown doc covering row-level vs schema-level isolation, RLS policies, and tenant-scoped indexes | docs/saas/tenant-isolation-note.md |
| Security | Tenant authorization audit | Markdown doc covering super-admin vs tenant-admin vs user privilege boundaries and authz tests | docs/saas/tenant-authz-audit.md |
references/ directory for deep detail after reading the core workflow below.documentation/ directory for supporting implementation detail or migration notes.Production-grade multi-tenant SaaS architecture with three-panel separation, zero-trust enforcement, strict tenant isolation, and comprehensive audit trails. This skill defines the tenant and auth boundaries that every downstream module, API, and data query inherits.
Skill-specific references:
references/database-schema.md — tenant, user, audit, and tenant-scoped table schemas with indexes and partitioning.references/permission-model.md — RBAC schema, permission resolution algorithm, caching, middleware, hierarchical and conditional permissions.documentation/migration.md — adding tenant_id safely, zero-downtime migration, single-to-multi-tenant phases, rollback.Load these first, in order:
world-class-engineering — repository-wide quality bar.system-architecture-design — produces the context map and critical flows this skill consumes.database-design-engineering — produces the access-pattern list that shapes the isolation model.vibe-security-skill — produces the threat model and auth rules this skill encodes.documentation/migration.md).tenant_id filters, or un-audited admin actions./, /adminpanel/, /memberpanel/).| Artifact | Produced by | Required? | Why |
|---|---|---|---|
| Context map | system-architecture-design | required | defines services, panels, ownership |
| Critical-flow table | system-architecture-design | required | shapes auth and session boundaries |
| Access-pattern list | database-design-engineering | required | chooses shared-DB-with-tenant_id vs DB-per-tenant |
| Threat model | vibe-security-skill | required | informs audit rules and abuse cases |
| Panel and user-type list | product requirements | required | determines role and scope per panel |
tenant_id (or franchise_id) NOT NULL with a foreign key to tenants.tenant_id in the WHERE clause.tenant_id is always extracted from session or JWT — never from client input (body, query, header).404 Not Found, never 403 Forbidden (403 confirms the resource exists).Tenants share data schema AND tenants < ~10k AND regulated data not tenant-partitioned
-> Shared DB, row-level tenant_id (default choice for this skill)
Strong regulatory boundary (HIPAA/PCI) per tenant
-> Schema-per-tenant OR DB-per-tenant; app injects connection by tenant
Single very large tenant needs isolation from the rest
-> Hybrid: shared DB for small tenants, dedicated DB for large tenant
Wrong choice failure modes:
- Shared DB without tenant_id discipline -> data leakage, impossible to audit
- DB-per-tenant at small scale -> migration and operational cost explodes
- Schema-per-tenant without tooling -> migrations drift across schemas
Feature controls platform config, billing, tenant lifecycle, or impersonation
-> /adminpanel/ (super_admin only, audited)
Feature is tenant-admin workspace (manage own tenant, staff, catalogue)
-> /public/ root (tenant owners/staff, scoped by tenant_id)
Feature is self-service for end user (customer/member/student/patient)
-> /memberpanel/ (own records only, scoped by tenant_id AND user_id)
Wrong choice failure modes:
- Admin feature in /public/ -> tenant users see platform levers
- Tenant feature in /memberpanel/ -> end users escalate to admin-only data
- End-user feature in /public/ -> collides with tenant admin routes
1. user_permissions.denied match -> DENY for tenant-scoped actions
2. actor is super_admin -> ALLOW only for an explicitly scoped administrative permission (always audit)
3. user_permissions.granted match -> ALLOW
4. tenant_role_overrides match -> ALLOW/DENY per is_enabled
5. role -> permission via template -> ALLOW
6. no match -> DENY
Wrong ordering failure mode: if role is checked before user deny, an
explicit revocation never takes effect. Always deny-before-grant at each tier.
One SaaS codebase hosts multiple apps on the same origin -> prefix required
Only one app on origin AND no shared subdomain sessions -> prefix optional
SSO / impersonation across apps planned -> prefix required
Without a prefix, two SaaS apps on the same host collide in $_SESSION and the
bug only appears in production when both apps are logged in.
+-------------------------------------------------------------+
| Shared Infrastructure Layer |
| Data (tenant isolated) | Business Logic | Session system |
+-------------------------------------------------------------+
| | |
+--------v--------+ +---------v------+ +---------v-------+
| /public/ | | /adminpanel/ | | /memberpanel/ |
| (ROOT) | | | | |
| Tenant Admin | | Super Admin | | End User |
| Workspace | | System | | Portal |
| owner, staff | | super_admin | | member/student |
+-----------------+ +----------------+ +-----------------+
Important: /public/ root is the tenant-admin workspace, not an end-user panel. Confusing the two is the most common mistake.
File layout:
public/
index.php # Landing
sign-in.php # Login
dashboard.php # Tenant admin dashboard
adminpanel/ # Super admin panel
includes/
memberpanel/ # End user portal
includes/
includes/ # Shared includes
assets/
| Panel | Path | Users | Scope | Notes |
|---|---|---|---|---|
| Tenant Admin | /public/ | owner, staff | single tenant | all queries include tenant_id = ?; cannot touch platform config |
| Super Admin | /public/adminpanel/ | super_admin | cross-tenant | tenant_id may be NULL; every action audited |
| End User | /public/memberpanel/ | member, student, customer, patient | own records within tenant | queries scoped by tenant_id AND user_id |
tenant_id rules per user type| User type | tenant_id column |
|---|---|
| super_admin | nullable |
| tenant owner | required, NOT NULL |
| tenant staff | required, NOT NULL |
| member / student / customer / patient | required, NOT NULL |
CREATE TABLE students (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
tenant_id BIGINT UNSIGNED NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100),
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
INDEX idx_tenant (tenant_id),
INDEX idx_tenant_email (tenant_id, email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Composite indexes lead with tenant_id. utf8mb4_unicode_ci everywhere. Match file case exactly (Linux file systems are case-sensitive).
// Extract tenant from session/JWT. Never from client input.
$tenantId = getSession('tenant_id');
// Regular users: filter by tenant_id
$stmt = $db->prepare('SELECT * FROM students WHERE tenant_id = ? AND id = ?');
$stmt->execute([$tenantId, $studentId]);
// Super admin: cross-tenant access is allowed but always audited
if (getSession('user_type') === 'super_admin') {
auditLog('CROSS_TENANT_ACCESS', [
'admin_user_id' => getSession('user_id'),
'target_tenant_id' => $requestedTenantId,
'action' => 'VIEW_STUDENTS',
'justification' => $request['justification'] ?? null,
]);
}
define('SESSION_PREFIX', 'saas_app_'); // e.g. 'school_', 'restaurant_'
setSession('user_id', $userId); // writes $_SESSION['saas_app_user_id']
$userId = getSession('user_id'); // reads $_SESSION['saas_app_user_id']
| Setting | Value |
|---|---|
| HttpOnly | true |
| Secure | auto-detect HTTPS (port 443 or $_SERVER['HTTPS']) |
| SameSite | Strict |
| Lifetime | 30 min idle |
| Regeneration | on login and on privilege change |
$isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| (int) $_SERVER['SERVER_PORT'] === 443;
ini_set('session.cookie_secure', $isHttps ? '1' : '0');
| Setting | Value |
|---|---|
| Access token lifetime | 15 min |
| Refresh token lifetime | 30 days |
| Rotation | on every refresh |
| Revocation | persisted table keyed by jti |
Priority (highest first): user deny > user grant > tenant override > role permission > default deny. See references/permission-model.md for full schema, resolution algorithm, caching strategy, and seed data.
function hasPermission(userId, tenantId, permission) {
if (userPermissions.denied(userId, tenantId, permission)) return false;
if (user.type === 'super_admin' && isScopedAdministrativePermission(permission)) {
auditLog('PERMISSION_BYPASS', { userId, permission });
return true;
}
if (userPermissions.granted(userId, tenantId, permission)) return true;
for (const role of getUserRoles(userId, tenantId)) {
const override = tenantRoleOverride(tenantId, role.id, permission);
if (override !== null) return override.isEnabled;
if (roleHasPermission(role, permission)) return true;
}
return false; // default deny
}
tenant_id in every query on a tenant-scoped table (except super_admin with audit).# Tenant-scoped (default)
GET /api/v1/orders # list within tenant
POST /api/v1/orders # create within tenant
GET /api/v1/orders/{id} # show; 404 if wrong tenant
DELETE /api/v1/orders/{id} # delete; 404 if wrong tenant
# Super admin (cross-tenant, audited)
GET /api/v1/admin/tenants
POST /api/v1/admin/impersonate
Response envelope:
{ "success": true, "data": {}, "meta": { "page": 1, "total": 100 } }
Error envelope:
{ "success": false, "error": { "code": "PERMISSION_DENIED", "message": "..." } }
Always audit: all super-admin actions, impersonation, permission changes, tenant creation/suspension, data exports, failed auth, cross-tenant access attempts.
{
"id": "uuid",
"timestamp": "2026-04-07T10:30:00Z",
"actor_user_id": 123,
"actor_type": "super_admin",
"action": "IMPERSONATE_USER",
"target_tenant_id": 456,
"justification": "Support request #12345",
"ip_address": "203.0.113.1",
"changes": { "before": {}, "after": {} }
}
Retention: security logs 1 year, audit trails 7 years, operational logs 90 days.
tenant_id on a tenant-scoped table.PENDING -> ACTIVE -> SUSPENDED -> ARCHIVED
Archival is reversible until a configured retention cut-off; archived tenants are then hard-deleted via cascade.
$franchiseId = $_POST['franchise_id']; — any user can set this to another tenant. Fix: always getSession('tenant_id') or read from the verified JWT claim.SELECT * FROM students WHERE id = ? — returns any student across any tenant. Fix: SELECT * FROM students WHERE tenant_id = ? AND id = ? and treat a missing row as 404.deleteStudent($studentId); run from the super-admin panel with no log. Fix: wrap every super-admin mutation with auditLog('ADMIN_DELETE_STUDENT', [...]) capturing actor, target tenant, and justification.tenant_id nullable on a business table. tenant_id BIGINT NULL — allows rows with no owner, which every tenant query then excludes silently. Fix: tenant_id BIGINT UNSIGNED NOT NULL with a foreign key; backfill before flipping NOT NULL.$_SESSION['user_id']. Fix: SESSION_PREFIX per app, always go through setSession() / getSession().(status, tenant_id) instead of (tenant_id, status). Query planner cannot use the composite index to isolate one tenant's slice. Fix: lead every composite index with tenant_id.documentation/migration.md and verify no NULL tenant_id before flipping the column to NOT NULL.database-design-engineering — schema, indexes, migration discipline once the isolation model is chosen.api-design-first — REST conventions, auth model, and error envelope that this skill's panels depend on.vibe-security-skill — threat modelling, abuse cases, and auth/authz matrix that feed this skill.kubernetes-saas-delivery — per-tenant deploy, namespace isolation, and progressive rollout on top of this model.modular-saas-architecture — when the platform also composes pluggable business modules inside the tenancy defined here.mysql-best-practices — MySQL engine-specific execution of the schema patterns here.Tenant isolation patterns apply one level deeper when AI is in play. The transactional patterns in this skill (RLS / schema-per-tenant / DB-per-tenant) extend to AI asset classes — vector stores, prompts, fine-tunes, eval datasets, conversation logs, retrieval caches, audit log payloads. The dedicated skill is ai-tenant-isolation-patterns.
Cross-references:
ai-on-saas-architecture — unifying AI+SaaS architecture.ai-tenant-isolation-patterns — vector-store partitioning, defence-in-depth, BYOK, data-bleed test suite.ai-rag-multi-tenant — RAG-specific isolation.ai-model-gateway — enforces tenant scope at the request boundary.ai-prompt-injection-and-tenant-safety — prompt-layer adversarial complement.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub peterbamuhigire/chwezi-dev-engine --plugin engineering169 skills in this plugin
/accounting-engineUse when designing or implementing double-entry accounting policy and an embedded ledger engine for invoices, payments, stock, balanced postings, mapped accounts, idempotency, reversals, period locks, audit trails, and financial statements./accounting-finance-controllerUse when coordinating accounting and finance implementation reviews, doctrine routing, control evidence, remediation priorities, and release decisions across a software system./advanced-testing-strategyUse when designing or reviewing test strategy for production systems, APIs, mobile apps, SaaS platforms, ERP workflows, and AI-enabled systems. Covers unit, integration, contract, end-to-end, regression, release-gate, and risk-based testing decisions.Other plugins' skills for the same job