+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Hiding Libraries Based on User Role

by Georgi Chokov
Introduction

Sitefinity cannot hide image, document or video libraries out of the box. You can, however, hide these libraries by overriding the library repeater's item databound event, finding the download link and disabling it.

Overriding Templates

Before reading the article below, please read this article on overriding views.

Instructions

1.    Create a file called Telerik.Sitefinity.Configuration.ControlsConfig.xml in the ~/App_Data/Configuration folder of your application

2.    To that file, add this code:


<?xml version="1.0" encoding="utf-8"?> 
<controlsConfig> 
    <viewMap> 
        <!--Displays a list of libraries.--> 
        <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryList`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryListView.ascx" /> 
    </viewMap> 
</controlsConfig> 


3.    In the templates download mentioned in overriding templates, copy the ~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryListView.ascx file and its corresponding .resx file in the App_LocalResouces folder into the ~/Sitefinity/Admin/ControlTemplates/Libraries/ directory and the App_LocalResources directory of your application.

4.    Find the Repeater control and the link for entering the library and deleting the library (shown below)
5.    Disable those links, if the user is in the visitors role
6.    On your command prompt, type in IISReset
7.    Press Ctrl + F5 to rebuild your project
8.    Logon to your Sitefinity project as someone in the Visitors role
9.    On the top navigation, click Modules > Images and Documents

You will now see that the downloads library has been hidden:




~/Sitefinity/ControlTemplates
/Libraries/LibraryListView.ascx

<%@ Control Language="C#" %> 
<%@ Register TagPrefix="sfMsg" Namespace="Telerik.Cms.Web.UI" Assembly="Telerik.Cms.Web.UI" %> 
<%@ Register TagPrefix="telerik" Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" %> 
 
<script runat="server"
 
    protected void fullWindow_Load(object sender, EventArgs e) 
    { 
        if(System.Web.Security.Roles.IsUserInRole("Visitors")) 
        { 
            viewMode.Enabled = false;
            RadMultiPage r = (RadMultiPage)fullWindow.FindControl("multiPage"); 
            RadPageView rpv = (RadPageView)r.FindControl("boxesView"); 
            Repeater repeater = (Repeater)rpv.FindControl("librariesRepeater"); 
            repeater.ItemDataBound += new RepeaterItemEventHandler(repeater_ItemDataBound); 
 
             
 
        } 
    } 
 
    void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
        { 
            Telerik.Libraries.ILibrary library = e.Item.DataItem as Telerik.Libraries.ILibrary; 
            if (library != null && library.Name == "Downloads" && Telerik.Security.UserManager.IsCurrentUserInRole("Visitors")) 
            { 
               HyperLink link =  (HyperLink)e.Item.FindControl("libName"); 
               if (link != null) 
               { 
                   link.Enabled = false
               } 
               Label label = (Label)e.Item.FindControl("PermissionLabel"); 
                    if(label != null) 
                    { 
                        label.Visible = true
                    } 
 
                    LinkButton linkButton = (LinkButton)e.Item.FindControl("libDelete"); 
                    if (linkButton != null) 
                    { 
                        linkButton.Visible = false
                    } 
                     
            } 
        } 
    } 
</script> 
 
<div class="ToolsAll"
    <%--<p><asp:LinkButton runat="server" ID="modulesPermissions" Text="<%$Resources:Permissions %>" CssClass="modulePermissions"></asp:LinkButton></p>--%> 
    <asp:PlaceHolder runat="server" ID="browseWindow"
        <asp:HyperLink ID="uploadButton" runat="server" CssClass="CmsButLeft new"
            <strong id="Strong1" runat="server" class="CmsButRight light"
                <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:Upload %>"></asp:Literal></strong></asp:HyperLink> 
        <div class="clear"
            <!-- --> 
        </div> 
    </asp:PlaceHolder> 
</div> 
<div id="divWorkArea" runat="server" class="workArea insert noInfoColumn"
    <div class="mainForm"
        <sfMsg:MessageControl runat="server" ID="message"
            <ItemTemplate> 
                <asp:Label runat="server" ID="messageText"></asp:Label> 
            </ItemTemplate> 
        </sfMsg:MessageControl> 
        <asp:PlaceHolder runat="server" ID="fullWindow" onload="fullWindow_Load"
            <div class="listHeader moduleItemsHeader"
                <h2> 
                    <asp:Literal ID="Literal2" runat="server" Text="<%$Resources:AllLibraries %>" /></h2
                <span id="delimiter" runat="server" class="cmstextlbl">| </span> 
                <asp:HyperLink ID="createLibraryButton" runat="server" Text="<%$Resources:CreateLibrary %>"></asp:HyperLink> 
            </div> 
            <%-- <div id="providersDiv" runat="server" class="provider SiteMapTools"
        <asp:DropDownList runat="server" ID="providersList" AutoPostBack="true"></asp:DropDownList> 
    </div>--%> 
            <div class="viewOptions"
                <p> 
                    View as:</p> 
                <telerik:RadTabStrip ID="viewMode" runat="server" MultiPageID="multiPage" SelectedIndex="0" 
                    EnableEmbeddedSkins="false" Skin="SitefinityViewMode"
                    <Tabs> 
                        <telerik:RadTab PageViewID="boxesView" runat="Server" Text="<%$Resources:Boxes %>"
                        </telerik:RadTab> 
                        <telerik:RadTab PageViewID="listView" runat="server" Text="<%$Resources:List %>"
                        </telerik:RadTab> 
                    </Tabs> 
                </telerik:RadTabStrip> 
            </div> 
            <sfMsg:Pager runat="server" ID="pager1"
                <LayoutTemplate> 
                    <asp:Repeater ID="PageRepeaterLinkButton" runat="server"
                        <HeaderTemplate> 
                            <ol class="pager"
                                <li> 
                                    <asp:LinkButton ID="PreviousPage" runat="server" Text="<%$Resources:PreviousPage %>"></asp:LinkButton></li
                        </HeaderTemplate> 
                        <ItemTemplate> 
                            <li> 
                                <asp:LinkButton ID="SingleItem" runat="server" /> 
                            </li> 
                        </ItemTemplate> 
                        <FooterTemplate> 
                            <li> 
                                <asp:LinkButton ID="NextPage" runat="server" Text="<%$Resources:NextPage %>"></asp:LinkButton></li
                            </ol> 
                        </FooterTemplate> 
                    </asp:Repeater> 
                    <asp:Repeater ID="PageRepeaterHyperLink" runat="server"
                        <HeaderTemplate> 
                            <ol class="sf_pager"
                                <li> 
                                    <asp:HyperLink ID="PreviousPage" runat="server" Text="<%$Resources:PreviousPage %>"></asp:HyperLink></li
                        </HeaderTemplate> 
                        <ItemTemplate> 
                            <li> 
                                <asp:HyperLink ID="SingleItem" runat="server" /> 
                            </li> 
                        </ItemTemplate> 
                        <FooterTemplate> 
                            <li> 
                                <asp:HyperLink ID="NextPage" runat="server" Text="<%$Resources:NextPage %>"></asp:HyperLink></li
                            </ol> 
                        </FooterTemplate> 
                    </asp:Repeater> 
                </LayoutTemplate> 
            </sfMsg:Pager> 
            <telerik:RadMultiPage ID="multiPage" runat="server"
                <telerik:RadPageView ID="boxesView" runat="server"
                    <asp:Repeater ID="librariesRepeater" runat="server"
                        <HeaderTemplate> 
                            <ul id="modulesItemsList" class="modulesItemsList"
                        </HeaderTemplate> 
                        <ItemTemplate> 
                            <li> 
                                <dl> 
                                    <dt> 
                                        <asp:HyperLink ID="libName" ToolTip='<%#Eval("Name") %>' runat="server"><strong><%#Eval("Name") %></strong></asp:HyperLink> 
                                    <asp:Label ID="PermissionLabel" Text="Permission Denied" runat="server" Visible="false"></asp:Label> 
                                    </dt> 
                                    <dd> 
                                        <asp:Label ID="libType" runat="server"><%#Eval("TypeName")%> Library</asp:Label> 
                                    </dd> 
                                    <dd class="itemCount"
                                        <asp:Label runat="server" ID="libCount"></asp:Label> 
                                    </dd> 
                                    <dd> 
                                        <asp:LinkButton ID="libDelete" runat="server" Text="<%$Resources:Delete %>" CssClass="delete" 
                                            ToolTip="<%$Resources:DeleteToolTip %>"></asp:LinkButton> 
                                    </dd> 
                                </dl> 
                            </li> 
                        </ItemTemplate> 
                        <FooterTemplate> 
                            </ul> 
                        </FooterTemplate> 
                    </asp:Repeater> 
                </telerik:RadPageView> 
                <telerik:RadPageView ID="listView" runat="server"
                    <telerik:RadGrid ID="librariesGrid" runat="server" AutoGenerateColumns="false" AllowSorting="true" 
                        AllowPaging="true" PageSize="50" EnableViewState="false" Skin="SitefinityItems" 
                        EnableEmbeddedSkins="false"
                        <MasterTableView AllowMultiColumnSorting="false" CssClass="listItems listItemsBindOnClient" 
                            Width="98%"
                            <Columns> 
                                <telerik:GridTemplateColumn UniqueName="Delete" ItemStyle-CssClass="gridActions delete"
                                    <ItemTemplate> 
                                        <asp:LinkButton ID="delete" runat="server" CommandName="Delete" Text="<%$Resources:Delete %>"></asp:LinkButton> 
                                    </ItemTemplate> 
                                </telerik:GridTemplateColumn> 
                                <telerik:GridTemplateColumn HeaderText="<%$Resources:Name %>" UniqueName="Name" SortExpression="Name" 
                                    ItemStyle-CssClass="gridContentTitle"
                                    <ItemTemplate> 
                                        <asp:HyperLink ID="openLibrary" runat="server"></asp:HyperLink> 
                                        <span class="gridTitleInfo"
                                            <%#Eval("TypeName")%> 
                                            Library</span> 
                                    </ItemTemplate> 
                                </telerik:GridTemplateColumn> 
                                <%--<telerik:GridButtonColumn CommandName="OpenLibrary" DataTextField="Name" HeaderText="<%$Resources:Name %>" SortExpression="Name" UniqueName="Delete"></telerik:GridButtonColumn>--%> 
                                <telerik:GridBoundColumn DataField="Owner" HeaderText="<%$Resources:Owner %>" UniqueName="Owner" 
                                    SortExpression="Owner"
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="DateCreated" HeaderText="<%$Resources:Created %>" 
                                    UniqueName="DateCreated" SortExpression="DateCreated" DataFormatString="<%$Resources:ItemList_DateFormat %>"
                                </telerik:GridBoundColumn> 
                                <telerik:GridTemplateColumn HeaderText="<%$Resources:Items %>"
                                    <ItemTemplate> 
                                        <asp:HyperLink ID="itemCountLink" runat="server"></asp:HyperLink> 
                                    </ItemTemplate> 
                                </telerik:GridTemplateColumn> 
                                <%--<telerik:GridButtonColumn CommandName="ChangeSettings" Text="<%$Resources:ChangeSettings %>" HeaderText="<%$Resources:Settings %>"></asp:ButtonField>--%> 
                                <telerik:GridTemplateColumn HeaderText="<%$Resources:Settings %>" UniqueName="ChangeSettings"
                                    <ItemTemplate> 
                                        <asp:HyperLink ID="changeSettings" runat="server" Text="<%$Resources:ChangeSettings %>"></asp:HyperLink> 
                                    </ItemTemplate> 
                                </telerik:GridTemplateColumn> 
                            </Columns> 
                        </MasterTableView> 
                        <PagerStyle Mode="NumericPages" /> 
                    </telerik:RadGrid> 
                    <%--<asp:GridView runat="server" ID="librariesGrid" AllowSorting="true" AutoGenerateColumns="false" GridLines="none" CssClass="listItems"
                <Columns>    
                    <asp:ButtonField ButtonType="Link" CommandName="Delete" Text="<%$Resources:Delete %>"><ItemStyle CssClass="gridActions delete" /></asp:ButtonField>          
                    <asp:ButtonField ButtonType="Link" CommandName="OpenLibrary" DataTextField="Name" SortExpression="Name" HeaderText="<%$Resources:Name %>"><ItemStyle CssClass="gridContentTitle" /></asp:ButtonField> 
                    <asp:BoundField DataField="Owner" HeaderText="<%$Resources:Owner %>" SortExpression="Owner" /> 
                    <asp:BoundField DataField="DateCreated" HeaderText="<%$Resources:Created %>" DataFormatString="<%$Resources:CreatedDateFormat %>" HtmlEncode="false" SortExpression="DateCreated" /> 
                    <asp:ButtonField DataTextField="ItemsCount" HeaderText="<%$Resources:Items %>" CommandName="OpenLibrary" /> 
                    <asp:ButtonField ButtonType="Link" CommandName="ChangeSettings" Text="<%$Resources:ChangeSettings %>" HeaderText="<%$Resources:Settings %>"></asp:ButtonField> 
                </Columns> 
            </asp:GridView>--%> 
                </telerik:RadPageView> 
            </telerik:RadMultiPage> 
            <sfMsg:Pager runat="server" ID="pager2"
                <LayoutTemplate> 
                    <asp:Repeater ID="PageRepeaterLinkButton" runat="server"
                        <HeaderTemplate> 
                            <ol class="pager"
                                <li> 
                                    <asp:LinkButton ID="PreviousPage" runat="server" Text="<%$Resources:PreviousPage %>"></asp:LinkButton></li
                        </HeaderTemplate> 
                        <ItemTemplate> 
                            <li> 
                                <asp:LinkButton ID="SingleItem" runat="server" /> 
                            </li> 
                        </ItemTemplate> 
                        <FooterTemplate> 
                            <li> 
                                <asp:LinkButton ID="NextPage" runat="server" Text="<%$Resources:NextPage %>"></asp:LinkButton></li
                            </ol> 
                        </FooterTemplate> 
                    </asp:Repeater> 
                    <asp:Repeater ID="PageRepeaterHyperLink" runat="server"
                        <HeaderTemplate> 
                            <ol class="sf_pager"
                                <li> 
                                    <asp:HyperLink ID="PreviousPage" runat="server" Text="<%$Resources:PreviousPage %>"></asp:HyperLink></li
                        </HeaderTemplate> 
                        <ItemTemplate> 
                            <li> 
                                <asp:HyperLink ID="SingleItem" runat="server" /> 
                            </li> 
                        </ItemTemplate> 
                        <FooterTemplate> 
                            <li> 
                                <asp:HyperLink ID="NextPage" runat="server" Text="<%$Resources:NextPage %>"></asp:HyperLink></li
                            </ol> 
                        </FooterTemplate> 
                    </asp:Repeater> 
                </LayoutTemplate> 
            </sfMsg:Pager> 
 
            <script type="text/javascript"
                if (document.getElementById("modulesItemsList")) { 
                    var mItemsLis = document.getElementById("modulesItemsList").getElementsByTagName("li"); 
                    for (var i = 0; i < mItemsLis.length; i++) { 
                        mItemsLis[i].onmouseover = function() { 
                            var flag = new RegExp('\\b' + 'over' + '\\b').test(this.className); 
                            if (!flag) this.className += this.className ? ' ' + 'over' : 'over'; 
                        } 
                        mItemsLis[i].onmouseout = function() { 
                            var rep = this.className.match(' ' + 'over') ? ' ' + 'over' : 'over'; 
                            thisthis.className = this.className.replace(rep, ''); 
                        } 
                    } 
                } 
            </script> 
 
        </asp:PlaceHolder> 
        <asp:PlaceHolder runat="server" ID="emptyWindow"
            <div id="empty"
                <h2 class="gridTitle"
                    <asp:Literal ID="Literal3" runat="server" Text="<%$Resources:NoContent %>"></asp:Literal></h2
                <p> 
                    <asp:HyperLink ID="createNew" runat="server" CssClass="mainLink" ToolTip="<%$Resources:CreateYourFirstContentTooltip %>"
                        <strong> 
                            <asp:Literal ID="Literal4" runat="server" Text="<%$Resources:CreateYourFirstContent %>"></asp:Literal></strong></asp:HyperLink><br /> 
                    <%--<asp:Literal ID="Literal5" runat="server" Text="<%$Resources:Or %>"></asp:Literal><href="http://www.sitefinity.com/sf3/faq.html" target="_blank" title="Opens in new browser window"><asp:Literal ID="Literal6" runat="server" Text="<%$Resources:CheckGenericContentFAQ %>"></asp:Literal></a></p>      --%> 
            </div> 
        </asp:PlaceHolder> 
    </div> 
    <div class="info" id="divFaq" runat="server"
        <div class="infoBottom"
            <asp:Literal ID="faq" runat="server" Text="<%$Resources:LibraryListFAQ %>"></asp:Literal> 
            <class="hideAllFAQs"
                <href="javascript:void(0)" onclick="javascript:Personalization.hideFaqSection('<%= divFaq.ClientID %>')"
                    Hide FAQs everywhere</a> 
            </p> 
        </div> 
    </div> 
</div> 
 

5 comments

Leave a comment
  1. almny May 28, 2009
    thanks
    can i get documentation about all functions or sitefinty api like this ?
  2. Joseph Anderson Jun 04, 2009
  3. shae Feb 22, 2010
    Hi Joseph, thanks for sharing this.

    An issue I am having now is the users in the Visitors group cannot select a library to upload to. It defaults to the ~/images folder. Select a library is grayed out.

    Any thoughts on how to fix this?
  4. shae Feb 22, 2010
    Actually, it was a permissions issue. But now even though permission is denied to access the defined library, the user can still upload to any library.

  5. shae Feb 22, 2010
    Actually, it was a permissions issue. But now even though permission is denied to access the defined library, the user can still upload to any library.

    Leave a comment