Friday, March 15, 2013

Hide tabs in EPiServers edit mode if not in certain role



Recently I had a customer requirement where a certain role should only be able to edit one specific property on the page.
I couldn't find any built in support in EPiServer for this, so I had to figure out another way to solve this problem.

Inspired by this blog post by David Knipe I decided to go for an Edit plugin with the following code:


 [GuiPlugIn(Area = PlugInArea.EditPanel)]
 class HideTabsFromRole : ICustomPlugInLoader
 {
  public PlugInDescriptor[] List()
  {
   EditPanel.LoadedPage += EditPanel_LoadedPage;

   return new PlugInDescriptor[0] { };
  }

  void EditPanel_LoadedPage(EditPanel sender, LoadedPageEventArgs e)
  {
   if (!HttpContext.Current.User.IsInRole("MyRole")) return;

   foreach (var propertyData in e.Page.Property.Where(propertyData => propertyData.Name != "MyProperty"))
   {
    e.Page.Property[propertyData.Name].DisplayEditUI = false;
   }
  }
 }


Basically what I'm doing is first checking if the user is in the role and if so then hide the properties, except for the property I want to display for this role. If there's no visible properties left on the tab the actual tab will not be rendered. The last part is default functionality in episerver.

Maybe this solution by Mark Everard would be a neater way of solving this problem, but this time I chose to take a shortcut.