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

Posted on 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:

~/Sitefinity/Admin/ControlTemplates/Products/CommandPanel.ascx

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" %> 
<h2> 
    <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 %>">  
    </cc1:LabelToolTip> 
    <asp:DropDownList ID="providersList" AutoPostBack="true" runat="server">  
    </asp:DropDownList> 
</div> 
 
<!-- menu with various commands of CommandPanel --> 
 
<dl id="expMenu">  
    <dt id="all">  
        <asp:LinkButton ID="contentViewButton" Text="<%$Resources:ProductItems %>" runat="server" /> 
    </dt> 
    <dd> 
        <asp:Literal runat="server" Text="<%$Resources:ControlDescription %>"></asp:Literal> 
    </dd> 
      
    <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>  
 
</dl> 
 

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:

Telerik.Samples.Products.WebControls.Admin.CommandPanel

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  
{  
   get 
   {  
       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;  
             ctrlPnl.Refresh();  
             break;  
        case "CategoriesView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Categories;  
             ctrlPnl.Refresh();  
             break;  
        case "TagsView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Tags;  
             ctrlPnl.Refresh();  
             break;  
        case "CommentsView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.CommentsView;  
             ctrlPnl.Refresh();  
             break;  
        case "PermissionView":  
             ctrlPnl.Mode = Cms.Engine.WebControls.Admin.ControlPanel.Modes.Permissions;  
             ctrlPnl.Refresh();  
             break;  
    }  
 
    // 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:

~/Sitefinity/Admin/ControlTemplates/Generic_Content/ControlPanelPermissions.ascx
~/Sitefinity/Admin/ControlTemplates/Generic_Content/App_LocalResources/ControlPanelPermissions.ascx.resx
~/Sitefinity/Admin/ControlTemplates/Generic_Content/App_LocalResources/PermissionsFAQText.txt

To these folders respectively:

~/Sitefinity/Admin/ControlTemplates/Products
~/Sitefinity/Admin/ControlTemplates/Products/App_LocalResources
~/Sitefinity/Admin/ControlTemplates/Products/App_LocalResources

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.

progress-logo

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

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation