EtchUK / Etch.OrchardCore.ContentPermissions

Module for Orchard Core to enable configuring access at a content item level.
MIT License
25 stars 6 forks source link

Apply permissions to menu items #13

Open fzadrazil opened 3 years ago

fzadrazil commented 3 years ago

The module currently applies only to pages. When I try to add the Content Permissions Part to menu item (CustomLink or ContentLink), it doesn't affect rendering of the menu. I've tried to find some way how to implement this and extend the module, but can't get to it. Use some filter maybe?

FrancoisCamus commented 2 years ago

Hi!

I had the same concern and had to override the default MenuItem.cshtml view to modify its behaviour.

@inject Etch.OrchardCore.ContentPermissions.Services.IContentPermissionsService ContentPermissionsService

@{
    // We are modifiyng the default behaviour to check for permissions
    if (!ContentPermissionsService.CanAccess(Model.ContentItem))
    {
        return;
    }

    TagBuilder tag = Tag(Model, "li");

    // Morphing the shape to keep Model untouched
    Model.Metadata.Alternates.Clear();
    Model.Classes.Clear();
    Model.Metadata.Type = "MenuItemLink";

    tag.InnerHtml.AppendHtml(await DisplayAsync(Model));

    if ((bool)Model.HasItems)
    {
        tag.InnerHtml.AppendHtml("<ul>");

        foreach (var item in Model.Items)
        {
            tag.InnerHtml.AppendHtml(await DisplayAsync(item));
        }

        tag.InnerHtml.AppendHtml("</ul>");
    }
}

@tag