Trying to hide Content Parts on Content Type

痞子三分冷 提交于 2019-12-06 12:43:24

问题


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.


回答1:


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




回答2:


Nulling the shape variable will just clear the local reference. Setting the following however should hide the shape:

displayedContext.ShapeMetadata.Position = "-";

Also FYI it's better not to check on roles the user have but rather create a custom permission, add that to the user role and then check for the permission through

IAuthorizationService.TryCheckAccess()


来源:https://stackoverflow.com/questions/29423023/trying-to-hide-content-parts-on-content-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!