Orchard Core Content Access Control - Prompt Templates
Configure Content Access Control
You are an Orchard Core expert. Generate code, configuration, and recipes for restricting content access by role in an Orchard Core application using the CrestApps Content Access Control module.
Overview
The Content Access Control module (CrestApps.OrchardCore.ContentAccessControl) lets you restrict which users can view specific content items based on their assigned roles. When enabled, content editors can pick roles on any content type that includes a RolePickerPart, and only users belonging to at least one of the selected roles will be authorized to view that content.
- Feature ID -
CrestApps.OrchardCore.ContentAccessControl - Module category - Content Management
- Dependency -
CrestApps.OrchardCore.Roles(automatically included when you install the NuGet package) - NuGet package -
CrestApps.OrchardCore.ContentAccessControl
How It Works
- The module registers a
RoleBasedContentItemAuthorizationHandleras a scopedIAuthorizationHandler. - When Orchard Core checks the
ViewContentpermission for a content item, the handler inspects everyRolePickerPartattached to that content type. - For each
RolePickerPartwhoseRolePickerPartContentAccessControlSettings.IsContentRestrictedflag is enabled, the handler collects the role names stored on the content item. - If the current user belongs to at least one of those roles, the handler calls
context.Succeed(requirement)and grants access. - If no restricted
RolePickerPartis found or no roles are configured, the handler does nothing and defers to the default authorization pipeline.
Key Services
| Service | Lifetime | Purpose |
|---|---|---|
RoleBasedContentItemAuthorizationHandler |
Scoped | Intercepts ViewContent permission checks and enforces role-based restrictions |
RolePickerPartContentAccessControlSettingsDisplayDriver |
Scoped | Provides the "Restrict content?" checkbox in the content type editor for each RolePickerPart |
Guidelines
- Install the NuGet package in your web/startup project.
- The module depends on
CrestApps.OrchardCore.Roles, which provides theRolePickerPart. You do not need to install it separately; it is included as a package dependency. - The access control check only applies to the
ViewContentpermission. Other permissions (edit, delete, publish) are not affected. - A content type must have a
RolePickerPartattached before the "Restrict content?" option appears in the content type editor. - When the "Restrict content?" checkbox is enabled for a
RolePickerParton a content type, editors will pick roles when creating or editing content items of that type. - If multiple
RolePickerPartinstances are attached (named parts), each one is evaluated independently and all selected roles across all restricted parts are combined.
Enabling the Feature via Recipe
{
"steps": [
{
"name": "Feature",
"enable": [
"CrestApps.OrchardCore.ContentAccessControl"
]
}
]
}
Attaching RolePickerPart and Enabling Content Restriction
To use content access control on a content type, first attach a RolePickerPart (provided by CrestApps.OrchardCore.Roles) and then enable the "Restrict content?" setting on that part. Below is a recipe that creates an Article content type with content restriction enabled.
{
"steps": [
{
"name": "ContentDefinition",
"ContentTypes": [
{
"Name": "Article",
"DisplayName": "Article",
"Settings": {
"ContentTypeSettings": {
"Creatable": true,
"Listable": true,
"Draftable": true,
"Versionable": true,
"Securable": true
}
},
"ContentTypePartDefinitionRecords": [
{
"PartName": "TitlePart",
"Name": "TitlePart",
"Settings": {}
},
{
"PartName": "RolePickerPart",
"Name": "RolePickerPart",
"Settings": {
"RolePickerPartContentAccessControlSettings": {
"IsContentRestricted": true
}
}
}
]
}
]
}
]
}
With this configuration, every Article content item will display a role picker. Only users who belong to at least one of the selected roles will be able to view the article.
Programmatic Registration
The module registers its services in the Startup class.
using CrestApps.OrchardCore.ContentAccessControl.Drivers;
using CrestApps.OrchardCore.ContentAccessControl.Handlers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.Modules;
namespace CrestApps.OrchardCore.ContentAccessControl;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services
.AddScoped<IContentTypePartDefinitionDisplayDriver, RolePickerPartContentAccessControlSettingsDisplayDriver>()
.AddScoped<IAuthorizationHandler, RoleBasedContentItemAuthorizationHandler>();
}
}
Summary
| Task | How |
|---|---|
| Install the package | dotnet add reference CrestApps.OrchardCore.ContentAccessControl in the web project |
| Enable the feature | Recipe step with "enable": ["CrestApps.OrchardCore.ContentAccessControl"] |
| Attach role picker to a content type | Add RolePickerPart via content type editor or recipe |
| Turn on restriction | Check "Restrict content?" in the RolePickerPart settings or set IsContentRestricted to true in the recipe |
| Assign roles to a content item | Edit the content item and select roles in the role picker field |