Orchardcore tenant hierarchy

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore tenant hierarchy is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the CrestApps Orchard Core Tenant Hierarchy module, where the Default tenant makes tenants parents that create, manage and open their own child tenants. Covers the Platform, Parent and Child features, the AddTenantHierarchy host guards in Program.cs, the TenantHierarchy configuration secti

How to use this skill

Skill file

Orchard Core Tenant Hierarchy - Prompt Templates

Parent Tenants with Their Own Child Tenants

You are an Orchard Core expert. Generate accurate setup, configuration, extension and troubleshooting guidance for the CrestApps Tenant Hierarchy module. A tenant the platform marks as a parent creates, manages and opens its own child tenants (for example a bookkeeping firm with one site per client business). The Default tenant stays the platform root and still manages every tenant through Orchard Core's Tenants admin.

Guidelines

Features

| Feature | Feature ID | Where it runs |

|---|---|---|

| Tenant Hierarchy Platform | CrestApps.OrchardCore.TenantHierarchy.Platform | Default tenant only (DefaultTenantOnly). Depends on OrchardCore.Tenants and CrestApps.OrchardCore.Resources |

| Parent Tenant | CrestApps.OrchardCore.TenantHierarchy.Parent | A parent, forced on when the platform makes it one. Depends on OrchardCore.Users, OrchardCore.Roles, CrestApps.OrchardCore.Resources, CrestApps.OrchardCore.Users (user picker) and OrchardCore.AuditTrail |

| Child Tenant | CrestApps.OrchardCore.TenantHierarchy.Child | Every child, forced on through the tenant Features configuration after setup. Depends on OrchardCore.Users and OrchardCore.Roles |

The Parent and Child features never depend on the Platform feature or OrchardCore.Tenants, because a dependency on a Default-only feature would make them unavailable elsewhere. See orchardcore-tenants for Orchard Core multi-tenancy and orchardcore-crestapps-users for the user picker.

Install the Host Guards

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddOrchardCms(orchardCore => orchardCore
        .AddTenantHierarchy());

var app = builder.Build();

app.UseOrchardCore();

app.Run();

AddTenantHierarchy() (namespace Microsoft.Extensions.DependencyInjection, in CrestApps.OrchardCore.TenantHierarchy.Core) binds TenantHierarchyOptions, replaces IShellHost with GuardedShellHost, registers the built-in database provisioners, the parent removal guards, and per-tenant feature validation, egress guard and __Host- cookie setup, and adds the Fetch Metadata and unavailable-address middlewares. Each guard does nothing for a tenant that is neither a parent nor a child.

Configuration

{
  "TenantHierarchy": {
    "PlatformDomain": "platform.com",
    "Scheme": "https",
    "UseHostPrefixedCookies": true,
    "ReservedSlugs": [ "billing" ],
    "Egress": {
      "Enabled": true,
      "BlockPrivateNetworks": true,
      "AllowedHosts": [ "ollama.internal" ]
    },
    "DatabasePools": {
      "firms": {
        "DatabaseProvider": "Postgres",
        "ConnectionString": "Host=db;Username=provisioner;Password=...",
        "RetainRemovedDatabases": true
      }
    }
  }
}

| Key | Default | Meaning |

|---|---|---|

| PlatformDomain | Host of the request to Default | Parents are {slug}.{PlatformDomain}; may carry a port such as localhost:5000 |

| Scheme | https (request scheme in development) | Scheme of addresses the module builds |

| UseHostPrefixedCookies | On, except in development | __Host- prefix for parent and child cookies; needs HTTPS |

| ReservedSlugs | Empty | Extra slugs no tenant may use (built in: admin, www, api, mail, login and others) |

| Egress:Enabled / BlockPrivateNetworks / AllowedHosts | true / true / empty | Outbound HttpClient guard |

| DatabasePools:{name}:DatabaseProvider | | SqlConnection, Postgres, MySql or Sqlite |

| DatabasePools:{name}:ConnectionString | | A login that can create databases, schemas and logins |

| DatabasePools:{name}:RetainRemovedDatabases | true | Rename a removed child database to {name}_removed_{timestamp} instead of dropping it |

Environment variables use __, for example TenantHierarchy__PlatformDomain=platform.com.

The parent policy (ParentTenantPolicy) is written into the parent's shell settings from the platform's Policy screen, or set as OrchardCore:{tenant}:TenantHierarchy:Policy:{key}. Keys and defaults are in references/tenant-hierarchy-reference.md.

How It Works

Feature Guard

| Feature | Rule |

|---|---|

| Parent Tenant | Available only in a parent |

| Child Tenant | Never offered; forced on through configuration |

| Tenant Hierarchy Platform | Default only |

| OrchardCore.OpenId.Validation | Blocked in parents and children |

| OrchardCore.Deployment.Remote | Blocked in children |

| OrchardCore.Queries.Sql | Blocked in children sharing a database (schema or table prefix) |

| Policy BlockedFeatures | Blocked in that parent's children |

The guard filters what can be enabled; it does not turn off a feature already on (the parent screen lists such features). Setup recipe steps that enable a blocked feature are ignored.

Permissions

| Permission | Allows |

|---|---|

| ManageTenantHierarchy | Platform screens (Default tenant) |

| ViewChildTenants | Child tenant list; implied by the next five |

| CreateChildTenants | Create, retry and discard children |

| ManageChildTenants | Edit, suspend, resume and reload |

| ManageChildFeatures | Enable and disable features in a child |

| RemoveChildTenants | Remove and restore |

| ManageChildAccess | Access grants |

| EnterChildTenants | Base gate for opening a child; a grant is still needed |

When a tenant becomes a parent, a parent-wide grant gives the parent's Administrator role the child role Administrator in every child. Activity needs the audit trail's ViewAuditTrail (/Admin/AuditTrail?q=category:TenantHierarchy).

Access Grants Recipe and Deployment Step

In a parent tenant (Parent Tenant feature), access grants travel with the TenantHierarchyAccessGrants recipe step (TenantHierarchyConstants.Recipes.AccessGrants) and the Child tenant access deployment step, both with CrestApps Recipes schemas. Each grant goes through the access grant manager as on the admin screen: a user is found by user name or email, the role must exist, and a grant for the same principal and child is updated instead of duplicated. A grant without a child covers every child; name a child by ChildEntryId, or by ChildSlug when the entry id is not known on this site. Grants carry no secret.

{
  "steps": [
    {
      "name": "TenantHierarchyAccessGrants",
      "Grants": [
        { "PrincipalType": "Role", "Principal": "Administrator", "ChildRoles": [ "Administrator" ] },
        { "PrincipalType": "User", "Principal": "ana", "ChildSlug": "north", "ChildRoles": [ "Editor" ] }
      ]
    }
  ]
}

Expired delegated sign-in codes are purged by DelegatedAccessCodeRetentionPolicy, a policy on the shared retention building block (DocumentRetentionPolicyBase, AddRetentionPolicy<T>() in CrestApps.OrchardCore.YesSql.Core), five minutes after they expire.

The child tenant create, edit and remove pages and the platform's parent editor put their buttons in Orchard Core's sticky bottom action bar (<div class="action-bar-bottom">); a theme override of those views must render its buttons there too.

Custom Database Provisioner

Built-in strategies (ChildDatabaseStrategy): SqlitePerChild (default), DatabasePerChild and SchemaPerChild (SQL Server, PostgreSQL), TablePrefixPerChild (any provider). To support another server, implement IChildTenantDatabaseProvisioner and register it as a host singleton after AddTenantHierarchy(). The first provisioner whose CanProvision returns true wins, and built-ins are registered first, so claim only what they do not handle:

builder.Services.AddSingleton<IChildTenantDatabaseProvisioner, MySqlChildDatabaseProvisioner>();

A full sample is in references/tenant-hierarchy-reference.md.

Deployment Checklist

Not included: federated OpenID Connect for children in another application, custom domains for children, inviting the business owner at creation, idle release of inactive children.

Monitoring

Each operation a parent runs on a child through TenantHierarchyBroker records crestapps.tenanthierarchy.child.operation.duration (operation = create, setup, update, suspend, resume, reload, remove, set_features; outcome = ok, failed, error, cancelled) and a span crestapps.tenanthierarchy.child.<operation> tagged with the registry entry id (crestapps.tenanthierarchy.entry.id), on the CrestApps.OrchardCore.TenantHierarchy meter and source. Tenant names never go on a metric or span tag. ChildTenantMaintenanceBackgroundTask reports on the shared crestapps.background_task.* instruments. See orchardcore-observability.

Troubleshooting

| Symptom | Cause and fix |

|---|---|

| Screens say The host guards are not installed | Call AddTenantHierarchy() in Program.cs and restart |

| Outbound request fails with it is the address of a tenant of this application | Tenants of a hierarchy do not call each other over HTTP. Use ITenantHierarchyBroker |

| Outbound request to an internal address fails | Add the host to Egress:AllowedHosts |

| Sign-in loops back to the sign-in page | __Host- cookies need HTTPS. Turn UseHostPrefixedCookies off only on a test machine |

| TenantHierarchyAccessDeniedException logged | Code reached another tenant outside the broker; the log names both tenants |

| Child setup fails | Read the application log. The pool login must create databases, schemas and logins. Fix it, then Retry |

| Users signed out on another node | Share the data protection key ring and shell settings |

| A suspended child's address shows the platform sign-in | The app runs without AddTenantHierarchy(), so the unavailable-address guard is missing |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.