In the mean time series (part 3): "Products" module - simple implementation with enabled permissions

In the mean time series (part 3): "Products" module - simple implementation with enabled permissions

November 05, 2008 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

There have been quite a few requests for this particular example, and may I add, most of the angry posts regarding the developer’s manual were related to the lack of this sample. Admittedly, when I put the roadmap for this series in the manual I wasn’t going to tease you, but rather give you an idea what we are working on. Didn’t turn out that well, mostly because we couldn’t finish the samples on time – so my sincere apology to all of you. Anyhow, now that the political part of the post is over, lets dig into the code.

"Products" module – simple implementation with enabled permissions

NOTE: Since this article is a sequential part of the series, you should make sure that you have read the first article named Simple implementation and set up your project as it was explained there. I will reference that project throughout this article.

The task in front of us is to add permissions to our products module. As opposed to other features that we were enabling on our products module, we will not need to do any modifications on the public side. In order to enable permissions we will have to do three simple things. Before I explain what exactly are we going to do, take a look at the Figure 1 to see the end result.

Figure 1: Permissions command (left) and PermissionsView (right) in the products module

So, as you can see from the Figure 1, once we are done with implementing permissions we will be able to define which roles can view, create, modify and delete products. We will also have the option to define which roles can set the permissions.

Enabling Permissions command in the CommandPanel

We’ll start by uncommenting the permissions command markup in the CommandPanel template. Open following file in your project:


and uncomment the permissions command markup, represented by following lines:

<dt id="globalPerm"><asp:LinkButton ID="permissionViewButton" Text="<%$Resources:Permissions %>" runat="server" /></dt>  
<dd><asp:Literal ID="Literal3" runat="server" Text="<%$Resources:PermissionsDescription %>"></asp:Literal></dd>  

If you have followed all the articles of this series, your command panel template should now look like this:

<%@ Register TagPrefix="cc1" Namespace="Telerik.Cms.Web.UI" Assembly="Telerik.Cms.Web.UI" %> 
    <asp:Literal runat="server" Text="<%$Resources:ExploreProducts %>"></asp:Literal></h2>  
<!-- panel which provides User Interface for working with multiple providers in Products module --> 
<div id="providersPanel" runat="server" class="provider SiteMapTools">  
    <cc1:LabelToolTip HelpBoxCssClass="HelpBox" ID="labelHelpBox1" runat="server" LabelMode="true" 
        LabelTargetID="providersList" LabelText="<%$Resources:ChangeGroup %>" ToolTipTitle="<%$Resources:Group %>" 
        ToolTipText="<%$Resources:AboutGroup %>" AlternateText="<%$Resources:AboutGroup %>">  
    <asp:DropDownList ID="providersList" AutoPostBack="true" runat="server">  
<!-- menu with various commands of CommandPanel --> 
<dl id="expMenu">  
    <dt id="all">  
        <asp:LinkButton ID="contentViewButton" Text="<%$Resources:ProductItems %>" runat="server" /> 
        <asp:Literal runat="server" Text="<%$Resources:ControlDescription %>"></asp:Literal> 
    <dt id="cat"><asp:LinkButton ID="categoriesView" Text='<%$Resources:Categories %>' runat="server" CausesValidation="false"></asp:LinkButton></dt>  
    <dd><asp:Literal ID="Literal1" runat="server" Text="<%$Resources:CategoriesDescription %>"></asp:Literal></dd>  
    <dt id="tags"><asp:LinkButton ID="tagsView" Text="<%$Resources:Tags %>" runat="server" CausesValidation="false"></asp:LinkButton></dt>  
    <dd><asp:Literal ID="Literal2" runat="server" Text="<%$Resources:TagsDescription %>"></asp:Literal></dd>  
    <dt runat="server" id="commentsDt"><asp:LinkButton ID="commentsLink" Text="<%$Resources:Comments %>" runat="server" /></dt>  
    <dd runat="server" id="commentsDd"><asp:Literal runat="server" Text="<%$Resources:CommentsDescription %>"></asp:Literal></dd>  
    <dt id="globalPerm"><asp:LinkButton ID="permissionViewButton" Text="<%$Resources:Permissions %>" runat="server" /></dt>  
    <dd><asp:Literal ID="Literal3" runat="server" Text="<%$Resources:PermissionsDescription %>"></asp:Literal></dd>  

Now that we have enabled the permissionViewButton LinkButton, we need to attach some logic to its OnClick event which will change the ControlPanel mode to Permissions mode. Please open the following file located in your Telerik.Samples.Products project:


To ease the process of development, we’ll reference the LinkButton through the property of container class, so that we can access it easily in the rest of our code (without performing recursive FindControls). Scroll down to the Container class located in the CommandPanel.cs file and add following lines to it:

public IButtonControl PermissionViewButton  
       if (permissionViewButton == null)  
           permissionViewButton = (IButtonControl) FindRequiredControl<Control>("permissionViewButton");  
       return permissionViewButton;  
IButtonControl permissionViewButton;  

Now that we can access PermissionsView button easily, let’s navigate to the CreateChildControls method of the CommandPanel class and set the behavior of the PermissionsViewButton. Add following lines of code at the end of CreateChildControls method, but before the Controls.Add(container):

// set up the behavior of PermissionViewButton  
container.PermissionViewButton.CommandName = "PermissionView";  
container.PermissionViewButton.Command += button_Command;  
if (container.PermissionViewButton is LinkButton && (ctrlPnl.Mode == Cms.Engine.WebControls.Admin.ControlPanel.Modes.Permissions))  
   ((LinkButton)container.PermissionViewButton).CssClass = "sel";  

In the first two lines, we are setting the command name and command that will execute when user clicks on the LinkButton. The last two lines are simply changing the css class of the PermissionsViewButton to “sel” (as in “selected”) when ControlPanel is in the Permissions mode. One last thing to do here – handle the CommandName “PermissionView” in the button_Command. Navigate to the button_Command event handler and add the case for PermissionView. When you are done, the code for button_Command should look like this:

protected virtual void button_Command(object sender, CommandEventArgs e)  
    // based on the command name, set the Mode of ControlPanel  
    ControlPanel ctrlPnl = ((ControlPanel) base.ControlPanel);  
    switch (e.CommandName)  
        case "ContentView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.List;  
        case "CategoriesView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Categories;  
        case "TagsView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Tags;  
        case "CommentsView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.CommentsView;  
        case "PermissionView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Permissions;  
    // loop through all the buttons, and make the button that sent the command  
    // have "sel" css class  
    foreach (Control ctrl in container.Controls[0].Controls)  
        if (ctrl is LinkButton)  
            LinkButton button = (LinkButton) ctrl;  
            button.CssClass = button.ID == ((LinkButton) sender).ID ? "sel" : "";  

And that ends our coding work – now that wasn’t that hard. Since the ControlPanel will now be using Permissions templates, the last thing we need to is provide these templates in the products folder by copying the from the Generic_Content folder (we can modify them later, of course). Please copy following files:


To these folders respectively:


And we are done. Go build your project, create few roles and users and test out the new permissions. All the permissions related work is done for you by the GenericContent module on which products module is based.

You can download the code for this project from here.


The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments are disabled in preview mode.
Latest Stories in
Your Inbox
More From Progress
The New Mobile Development Landscape
Download Whitepaper
IDC Spotlight Sitefinity Thumbnail
Choosing the Right Digital Experience Platform to Improve Business Outcomes
Download Whitepaper
The Fastest Way to Build Mobile Apps With Cloud Data
Watch Webinar