Skip to content

Instantly share code, notes, and snippets.

@itcotta itcotta/PageTypeTabs.aspx Secret
Last active Jan 23, 2020

Embed
What would you like to do?
Admin mode plugin to list all page types and their available tabs by access rights filtering. Read the blog post over at Episerver world. [Insert link]
<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeBehind="PageTypeTabs.aspx.cs" Inherits="Pure.MVC.modules.PageTypeTabs.PageTypeTabs" %>
<%@ Register TagPrefix="EPiServerUI" Namespace="EPiServer.UI.WebControls" Assembly="EPiServer.UI" %>
<%@ Register TagPrefix="EPiServerScript" Namespace="EPiServer.ClientScript.WebControls" Assembly="EPiServer" %>
<asp:Content ContentPlaceHolderID="MainRegion" runat="server">
<div>
<asp:CheckBox id="ReadCheckBox" text="<%#GetAccessLevelName(EPiServer.Security.AccessLevel.Read) %>" visible="true" runat="server" EnableViewState="true" AutoPostBack="True"/>
<asp:CheckBox id="CreateCheckBox" text="<%#GetAccessLevelName(EPiServer.Security.AccessLevel.Create) %>" visible="true" runat="server" EnableViewState="true" AutoPostBack="True" style="margin-left: 1em"/>
<asp:CheckBox id="EditCheckBox" text="<%#GetAccessLevelName(EPiServer.Security.AccessLevel.Edit) %>" visible="true" runat="server" EnableViewState="true" AutoPostBack="True" style="margin-left: 1em"/>
<asp:CheckBox id="DeleteCheckBox" text="<%#GetAccessLevelName(EPiServer.Security.AccessLevel.Delete) %>" visible="true" runat="server" EnableViewState="true" AutoPostBack="True" style="margin-left: 1em"/>
<asp:CheckBox id="PublishCheckBox" text="<%#GetAccessLevelName(EPiServer.Security.AccessLevel.Publish) %>" visible="true" runat="server" EnableViewState="true" AutoPostBack="True" style="margin-left: 1em"/>
<asp:CheckBox id="AdministerCheckBox" text="<%#GetAccessLevelName(EPiServer.Security.AccessLevel.Administer) %>" visible="true" runat="server" EnableViewState="true" AutoPostBack="True" style="margin-left: 1em"/>
</div>
<br />
<asp:Button id="SelectAllButton" text="Select All" OnClick="SelectAll_Click" runat="server" />
<asp:Button id="UnselectAllButton" text="Unselect All" OnClick="UnSelectAll_Click" runat="server"/>
<br />
<br />
<asp:Repeater id="rptPageTypes" runat="server">
<itemtemplate>
<h2 style="display: inline"> <%# PageItem.PageType.LocalizedName %> </h2> (<%# PageItem.PageType.Name %>)
<EPiServerUI:TabStrip runat="server" ID="actionTab" EnableViewState="true" GeneratesPostBack="False" TargetID="tabView" SupportedPlugInArea="SystemSettings">
<asp:Repeater runat="server" DataSource=" <%# PageItem.Tabs %> ">
<itemtemplate>
<EPiServerUI:Tab Text="<%# TabItem.LocalizedName %>" runat="server" ID="Tab1" />
</itemtemplate>
</asp:Repeater>
</EPiServerUI:TabStrip>
<asp:Panel runat="server" ID="tabView" CssClass="epi-padding"></asp:Panel>
</itemtemplate>
</asp:Repeater>
</asp:Content>
using EPiServer;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.Security;
using EPiServer.ServiceLocation;
using EPiServer.Shell.Security;
using EPiServer.Shell.WebForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Pure.MVC.modules.PageTypeTabs
{
[GuiPlugIn(DisplayName = "[Knowit] List PageType Tabs", Area = PlugInArea.AdminMenu, Url = ("~/modules/PageTypeTabs/PageTypeTabs.aspx"))]
public partial class PageTypeTabs : WebFormsBase
{
public Dictionary<string, SettingsPageType> PageTypesDictionary { get; set; } =
new Dictionary<string, SettingsPageType>(); //Key = PageType Name, Value= SettingsPageType object.
private AccessLevel[] _availableAccessLevels;
protected int TabDisplayCounter { get; set; }
protected Repeater rptPageTypes = new Repeater();
protected Repeater rptTabDefinitions = new Repeater();
protected SettingsPageType PageItem => Page.GetDataItem() as SettingsPageType;
protected TabDefinition TabItem => Page.GetDataItem() as TabDefinition;
public PageTypeTabs()
{
_availableAccessLevels = new[] {AccessLevel.Read, AccessLevel.Create ,
AccessLevel.Edit, AccessLevel.Delete, AccessLevel.Publish, AccessLevel.Administer };
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
MasterPageFile = UriSupport.ResolveUrlFromUIBySettings("MasterPages/EPiServerUI.master");
SystemMessageContainer.Heading = "PageTypeTabs";
SystemMessageContainer.Description = "Lists page types with available tabs by access rights.";
}
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
CheckAndBindCheckBox(ReadCheckBox, true);
CheckAndBindCheckBox(CreateCheckBox, true);
CheckAndBindCheckBox(EditCheckBox, true);
CheckAndBindCheckBox(DeleteCheckBox, true);
CheckAndBindCheckBox(PublishCheckBox, true);
CheckAndBindCheckBox(AdministerCheckBox, true);
}
PopulatePageTypesDictionary(); //always re-populate the page types and tabs.
}
public void SelectAll_Click(object sender, EventArgs e)
{
SelectAll();
PopulatePageTypesDictionary();
}
public void UnSelectAll_Click(object sender, EventArgs e)
{
UnselectAll();
PopulatePageTypesDictionary();
}
private void CheckAndBindCheckBox(CheckBox cb, bool isChecked)
{
cb.Checked = isChecked;
cb.DataBind();
}
private void UnselectAll()
{
ReadCheckBox.Checked = false;
CreateCheckBox.Checked = false;
EditCheckBox.Checked = false;
DeleteCheckBox.Checked = false;
PublishCheckBox.Checked = false;
AdministerCheckBox.Checked = false;
}
private void SelectAll()
{
ReadCheckBox.Checked = true;
CreateCheckBox.Checked = true;
EditCheckBox.Checked = true;
DeleteCheckBox.Checked = true;
PublishCheckBox.Checked = true;
AdministerCheckBox.Checked = true;
}
private AccessLevel GetCheckboxesAccessLevel()
{
var level = AccessLevel.Undefined;
if (ReadCheckBox.Checked)
level |= AccessLevel.Read;
if (CreateCheckBox.Checked)
level |= AccessLevel.Create;
if (EditCheckBox.Checked)
level |= AccessLevel.Edit;
if (DeleteCheckBox.Checked)
level |= AccessLevel.Delete;
if (PublishCheckBox.Checked)
level |= AccessLevel.Publish;
if (AdministerCheckBox.Checked)
level |= AccessLevel.Administer;
return level;
}
/// <summary>
/// Populates the PageTypes dictionary with all pages and their respective tabs.
/// The tabs are extracted from the PropertyDefinitions from the page.
/// </summary>
private void PopulatePageTypesDictionary()
{
var pageTypes = PageTypesList();
PageTypesDictionary.Clear();
var selectedFilterAccess = GetCheckboxesAccessLevel();
foreach (var pageType in pageTypes)
{
var test = pageType.PropertyDefinitions.Select(x => x.Tab).GroupBy(z => z.LocalizedName);
var tabs = test.Select(y => y.FirstOrDefault());
//Apply filter by required access.
tabs = tabs.Where(a => selectedFilterAccess.HasFlag(a.RequiredAccess)).OrderBy(b => b.SortIndex);
var settingsPageType = new SettingsPageType() { PageType = pageType, Tabs = tabs };
PageTypesDictionary.Add(pageType.Name, settingsPageType);
}
rptPageTypes.DataSource = PageTypesDictionary.Values.ToList();
rptPageTypes.DataBind();
}
protected void AccessDropDown_DataBinding(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
foreach (AccessLevel level in this._availableAccessLevels)
{
ListItem item = new ListItem(this.GetAccessLevelName(level), level.ToString());
list.Items.Add(item);
}
}
public string GetAccessLevelName(AccessLevel accessLevel)
{
return string.Join(", ", (from a in this._availableAccessLevels
where (a != AccessLevel.NoAccess) && accessLevel.HasFlag(a)
select this.Translate(AccessControlList.LocalizationKey(a))));
}
/// <summary>
/// Returns an IEnumerable with all pages.
/// </summary>
private IEnumerable<PageType> PageTypesList()
{
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
IEnumerable<PageType> pagetypes = contentTypeRepository.List().OfType<PageType>();
return pagetypes.OrderBy(p => p.LocalizedName);
}
}
/// <summary>
/// Container wrapper class for PageType and TabDefinition.
/// </summary>
public class SettingsPageType
{
public PageType PageType { get; set; }
public IEnumerable<TabDefinition> Tabs { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Pure.MVC.modules.PageTypeTabs
{
public partial class PageTypeTabs
{
/// <summary>
/// ReadCheckBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CheckBox ReadCheckBox;
/// <summary>
/// CreateCheckBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CheckBox CreateCheckBox;
/// <summary>
/// EditCheckBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CheckBox EditCheckBox;
/// <summary>
/// DeleteCheckBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CheckBox DeleteCheckBox;
/// <summary>
/// PublishCheckBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CheckBox PublishCheckBox;
/// <summary>
/// AdministerCheckBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CheckBox AdministerCheckBox;
/// <summary>
/// SelectAllButton control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button SelectAllButton;
/// <summary>
/// UnselectAllButton control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button UnselectAllButton;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.