Orchardcore permission providers

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore permission providers is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for implementing Orchard Core permission providers. Covers static Permissions classes in Core projects, the IPermissionProvider pattern, AddPermissionProvider registration, LocalizationSource permission descriptions and PO-file translation, IStringLocalizerFactory.Localize, implied and securit

How to use this skill

Skill file

Orchard Core Permission Providers - Prompt Templates

Create a Permission Provider

You are an Orchard Core expert. Generate permission-provider code that follows Orchard Core conventions and CrestApps project structure.

Guidelines

Recommended Project Placement

Static Permission Definitions in a Core Project

using OrchardCore.Localization;
using OrchardCore.Security.Permissions;

namespace CrestApps.Sports.Teams.Core.Permissions;

public static class TeamPermissions
{
    public static readonly Permission ManageTeams = new(
        "ManageTeams",
        LocalizationSource.Create("Manage teams", typeof(TeamPermissions)),
        isSecurityCritical: true);

    public static readonly Permission ViewTeams = new(
        "ViewTeams",
        LocalizationSource.Create("View teams", typeof(TeamPermissions)),
        [ManageTeams]);
}

Use typeof() here: a static class cannot be the T of LocalizationSource.Create<T>(). The constructors taking a plain string description (new Permission("ManageTeams", "Manage teams")) are [Obsolete] (CS0618) since Orchard Core 4.0; they create a context-free source that is translated only through Data Localization, not PO files.

PermissionProvider Class

using CrestApps.Sports.Teams.Core.Permissions;
using OrchardCore;
using OrchardCore.Security.Permissions;

namespace CrestApps.Sports.Teams;

internal sealed class PermissionProvider : IPermissionProvider
{
    private readonly IEnumerable<Permission> _allPermissions =
    [
        TeamPermissions.ManageTeams,
        TeamPermissions.ViewTeams,
    ];

    public Task<IEnumerable<Permission>> GetPermissionsAsync()
        => Task.FromResult(_allPermissions);

    public IEnumerable<PermissionStereotype> GetDefaultStereotypes() =>
    [
        new PermissionStereotype
        {
            Name = OrchardCoreConstants.Roles.Administrator,
            Permissions = _allPermissions,
        },
        new PermissionStereotype
        {
            Name = "Coach",
            Permissions =
            [
                TeamPermissions.ViewTeams,
            ],
        },
    ];
}

Registering the Provider

using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Modules;
using OrchardCore.Security.Permissions;

namespace CrestApps.Sports.Teams;

public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddPermissionProvider<PermissionProvider>();
    }
}

Default stereotypes are granted to the roles of the same name when the feature is installed (first enabled), and to a role when it is created, for the enabled features. A role created while an installed feature is disabled remembers that feature, and gets its stereotype permissions when the feature is enabled again. Since Orchard Core 4.0 this also works for internal providers: every installed but disabled feature is tracked when a role is created, and its providers are resolved when it is re-enabled (previously only features whose public types included an IPermissionProvider were tracked, so the stereotypes of an internal provider were never granted to such roles).

Displaying and Translating Descriptions

Permission.Description is a LocalizationSource, not a string. Show it with stringLocalizerFactory.Localize(permission.Description)?.Value ?? permission.Name, and read permission.Description?.Value for the original text. The roles editor and the admin menu and menu item permission pickers translate typed descriptions with PO files, using the type's full name as msgctxt:

msgctxt "CrestApps.Sports.Teams.Core.Permissions.TeamPermissions"
msgid "Manage teams"
msgstr "Gérer les équipes"

Typed descriptions are not listed in Data Localization; context-free descriptions, permissions built from templates (for example "Edit Article"), and categories still are. orchardcore-localization-source covers the API, the Data Localization interaction, and migration in detail.

Dynamic Permission Templates

Declare the template of a family of dynamic permissions (one per team, role, content type...) as a PermissionTemplate, not as a Permission. A template is never listed by a provider or granted to a role; it only creates permissions:

using OrchardCore.Localization;
using OrchardCore.Security.Permissions;

namespace CrestApps.Sports.Teams.Core.Permissions;

public static class TeamPermissionTemplates
{
    private static readonly PermissionTemplate s_manageTeam = new(
        "ManageTeam_{0}",
        LocalizationSource.Create("Manage team {0}", typeof(TeamPermissionTemplates)),
        TeamPermissions.ManageTeams);

    public static Permission CreateManageTeamPermission(string teamName, string teamDisplayName) =>
        s_manageTeam.CreateDynamicPermission(teamName, teamDisplayName);
}

Content type permissions use the same model: ContentTypePermissionsHelper.PermissionTemplates (namespace OrchardCore.Contents.Security) is a Dictionary<string, PermissionTemplate> keyed by the global permission name. Use ContentTypePermissionsHelper.CreateDynamicPermissionOf(CommonPermissions.EditContent, typeDefinition), or its (string basePermissionName, ContentTypeDefinition) and (string basePermissionName, string contentType) overloads, and GetPermissionTemplate(name).

Content Part Placement Guidance

When a content part model is reused by services or other projects, move it into the matching *.Core project:

using OrchardCore.ContentManagement;

namespace CrestApps.Sports.Teams.Core.Models;

public sealed class TeamPart : ContentPart
{
    public string AgeGroup { get; set; }

    public string Level { get; set; }
}

The feature project should reference the core project and use that shared model from drivers, services, and startup wiring.

Upgrading from 3.x

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.