Search code examples
orchardcmsorchardcms-1.8

Trying to hide Content Parts on Content Type


I am building Content Types and adding Content Parts specific to a Client and Attorney. All of these parts have fields and/or content pickers, etc.

I want to restrict the Client Role to see only Client Content Parts, while I just allow the Attorney Role to see any Content Parts, including it's own Attorney Content Part for a particular Content Type. Again, these are all on the same Content Type, so Content Permissions will not work (except on the Content Type in general).

I want to hide the Attorney Content Parts when a Client is logged on.

I have tried using this:

public override void Displaying(ShapeDisplayingContext context)
    {
    context.ShapeMetadata.OnDisplaying(displayedContext => {
        var shape = context.Shape;

        if (context.Shape.Part.Name == "Parts_AttorneyMatterPart")
        {
            var workContext = _workContextAccessor.GetContext();
            var user = workContext.CurrentUser;
            var roles = user.As<UserRolesPart>().Roles;

            if (!roles.Contains("Spaces Attorney"))
            {
                shape = null;
            }
        }
        });
    }

Where I have a Content Part named "AttorneyMatterPart", and where the Attorney Role is "Spaces Attorney".

These Content Types and Parts were all created in the Orchard Admin. The only thing in my module is this class file.

But this won't hide the Content Part when the Client is logged in. I know that I have to work on the logic of what roles can see things (going to add || conditions for Admin, etc.). For now I am just testing this out.

Any help is appreciated.

EDIT (Bounty Added) I am really stumped as to whether or not this is even possible. This Content Part is created through the Admin UI. Under shape tracing I can see under the "Content" zone Model > ContentItem > AttorneyMatterPart. I have tried ShapeTableBuilder and I have tried OnDisplaying and OnDisplayed from a ShapeDisplayingContext.

If someone could provide a working sample it would be much appreciated.


Solution

  • When a content part is created through the admin dashboard, there isn't really a shape to render it, only individual shapes for inner content fields...

    So, try this

    public override void Displaying(ShapeDisplayingContext context) {
      context.ShapeMetadata.OnDisplaying(displayedContext => {
        var shape = displayedContext.Shape;
    
        if (shape.ContentPart != null
          && shape.ContentPart.PartDefinition.Name == "PartName") {
          var workContext = _workContextAccessor.GetContext();
          var user = workContext.CurrentUser;
    
          if (user == null || !user.Has<UserRolesPart>()
            || !user.As<UserRolesPart>().Roles.Contains("RoleName")) {
            displayedContext.ChildContent = new System.Web.HtmlString("");
          }
        }
      });
    }
    

    See my answer on OrchardPros

    http://orchardpros.net/tickets/6914

    Best