More in this section

Forums / Developing with Sitefinity / Displaying libraries based on AD group

Displaying libraries based on AD group

17 posts, 0 answered
  1. Michael
    Michael avatar
    12 posts
    Registered:
    13 Oct 2008
    24 Feb 2009
    Link to this post
    I'm trying to set up the Images & Documents module to display only a certain library based on the user's AD group.  I've overrided the embedded template and am working with LibraryListView.ascx.  I have a couple of questions...

    1) How can I access the items inside of "librariesRepeater"?  I need to check the name of each library to determine whether it should be hidden or not, yet when I use librariesRepeater.Items.Count, I'm given a value of 0.  There are two libraries that should be found.

    2) How can I check the authenticated user's AD group?  I have been able to use SecurityManager.GetCurrentUserName() to grab the current user's name, but I have not been successful in checking to see if a user is in a certain AD group.  Here's what I tried:

    TelerikADRoleProvider roleProvider = new TelerikADRoleProvider(); 
     
    if (roleProvider.IsUserInRole(SecurityManager.GetCurrentUserName(), "AD Group")) 
         //Show or hide library 

    Am I doing something wrong?

    Thanks!
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    26 Feb 2009
    Link to this post
    Hello Michael,

    Here is an example that shows how to hide one of the libraries based on the current user.

    First we need to change the template - basically I have added a code behind to the following template

    ~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryList-Libs.ascx

    <asp:Repeater ID="allLibraries" runat="server" OnItemDataBound="allLibraries_ItemDataBound"
                    <HeaderTemplate> 
                        <ul id="modulesItemsList" class="modulesItemsList"
                    </HeaderTemplate> 
                    <ItemTemplate> 
                        <li id="libraryPanel" runat="server"
                        <dl> 
                            <dt> 
                           <asp:LinkButton ID="libName" ToolTip='<%#Eval("Name") %>' CssClass='<%#Eval("TypeName").ToString().ToLower()%>' runat="server"><strong><%#Eval("Name") %></strong></asp:LinkButton> 
                            </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> 


    Then in the code behind I have the following.

        protected void allLibraries_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        { 
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
            { 
                ILibrary library = e.Item.DataItem as ILibrary; 
                if(library != null && library.Name == "Administrator" && UserManager.IsCurrentUserInRole("visitors")) 
                { 
                    HtmlGenericControl libraryPanel =(HtmlGenericControl)e.Item.FindControl("libraryPanel"); 
                    libraryPanel.Visible = false
                } 
            } 
        } 

    You can do the same for your AD. Just to let you know there is no interface for AD permissions for modules and they should be hard coded.

    This functionality will be available from 4.0.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    29 Apr 2009
    Link to this post
    We are trying to a very simmilar thing.

    Prior to 3.6 we would have done what was shown in Ivan's example, but this messed up paging in the library list etc.  We want to add our filter expression before databind in the view so that paging etc. all works consistently instead of intercepting the results and trimming them.

    I've got an open support ticket but am interested if you found a way to do this... 
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    04 May 2009
    Link to this post
    Hi Adam @Habanero,

    The problem is that filtering works only for items in a library. You can filter the items, but not the libraries.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    04 May 2009
    Link to this post
    Hi Ivan

    Can you guys think of any way to filter the Libraries?

    We have a situation where there will about 700 users on the system, each needs to only deal with about half a dozen libraries and we don't want them to be able to add/delete/view items in the other libraries, nor do we want them to even see those other 1200 libraries... it is just too much for the users.

    I will right as much code as needed in order to do this.  Do I need a custom module?  Do I need to reflect way way up the inheritance tree?

    This is very important and I'm hoping there is a way to do it.

    Thanks  
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 May 2009
    Link to this post
    Hello Adam @Habanero,

    We will consider this implementation when changing the API for 4.0. however there is a workaround that will allow you filtering by libraries. I sent a reply to your support request a few days ago with more description.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    05 May 2009
    Link to this post
    Thanks Ivan

    I did get that, after I posted here ;)

    I've replied.
  8. Jay
    Jay avatar
    25 posts
    Registered:
    12 May 2008
    05 May 2009
    Link to this post
    Hi Ivan,

    Are you able to publically post the solution for setting up permissions on a per library basis?


    Thanks,

    Jay
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    06 May 2009
    Link to this post
    Hi Jay,

    There are two approaches:

    1. Do it like my post above shows.
    2. If you are working with 3.6 you can create a filter for your libraries. Generally the API supports filtering by items. So that you need to create your CustomLibraryManager that inherits LibraryManager. Then create a new method for your manager that has filter or sorting properties.

    public new IList GetAllLibraries(int from, int max, string SortExp) 
        { 
            IList AllLibraries = base.GetAllLibraries(0, 0, "Author"); 

    Basically you also should work with UserManager class to check whrther the current user is one of your roles.

    Then you the view that inherits LibraryLists you need to return your custom manager witht he provider name.

    public override Telerik.Libraries.LibraryManager LibManager 
        { 
            get 
            { 
                return new CustomLibraryManager(this.Host.Host.ProviderName); 
            } 
        } 


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    07 May 2009
    Link to this post
    Hi Ivan

    I replied to my ticket but just for anyone else here.  For me number two above is the better option as paging etc.  works best.  However, for me the GetLibraries method on my custom manager is not being fired.

    If anyone gets this working please post up!  I'll do the same.  
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    08 May 2009
    Link to this post
    Hello Adam @Habanero,

    If you return the CustomLibraryManager in your LibManager you should get GetAllLibraries fired.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  12. TIOS
    TIOS avatar
    48 posts
    Registered:
    22 Mar 2006
    30 Nov 2009
    Link to this post
    Hi Adam,

    Did you get this working as i've got the same issue.  Would you be able to post the code you used?

    I've created the CustomLibraryManager in App_Code, but then not sure where the rest goes cant find any reference to it in LibraryListView............

    Thanks,
    Chris
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    30 Nov 2009
    Link to this post
    Hello Chris Bond,

    Below is a sample code

    // THIS WILL WORK IN CASE YOU WANT TO USE FILTER
    // ANOTHER OPTION IS USING THE SUGGESTION MADE IN THE SECOND POST
    // THERE JUST USE "AD" AS A MEMBERSHIP PROVIDER
     
    public class CustomLibraryManager : LibraryManager
        {
            public CustomLibraryManager()
            {}
      
            public CustomLibraryManager (string providerName)
                : base(providerName) { }
      
      
            public IList CustomGetAllLibraries(int from, int max, string SortExp)
            {
                IList AllLibraries = base.GetAllLibraries(0, 0, "Name");
      
                AllLibraries.Clear();
                // HERE YOU CAN ADD SOME ADDITIONAL CUSTOM LOGIC
                return AllLibraries;
      
            }

    Now you need to override InitializeControls method from LibraryItemsList view and create a custom data source

        protected override void InitializeControls(System.Web.UI.Control viewContainer)
     {
         base.InitializeControls(viewContainer);
     
         ObjectDataSource dataSource = new ObjectDataSource();
         dataSource .ID = "DataSource1";
         odsLibraries.TypeName = "CustomLibraryManager";
         odsLibraries.SelectMethod = "CustomGetAllLibraries";
     
             .....
                .....
    }


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  14. TIOS
    TIOS avatar
    48 posts
    Registered:
    22 Mar 2006
    30 Nov 2009
    Link to this post
    Thanks thats great - For the InitializeControls, where should this be inheriting from as all I did was add the LibraryListView.ascx derective to the app_data xml config file.  then created LibraryListView.ascx with code bebind then copyied and pasted the one from templates downloaded.  So the top line has

    Inherits

     

    System.Web.UI.UserControl

    I'm taking it this needs to inherit something else?

    Thanks

     

  15. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    30 Nov 2009
    Link to this post
    Hi Chris Bond,

    "Now you need to override InitializeControls method from LibraryItemsList view and create a custom data source" You need to create another custom view that inherits from LibraryItemsList . The way you do it looks like not the the Adam requirement. If you want to do it as he did you need to create a custom module, because mapping only LibraryItemsList and creating a code behind will not allow you to use filtering - you can just restrinct the access to a library (please see the second post)

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  16. TIOS
    TIOS avatar
    48 posts
    Registered:
    22 Mar 2006
    04 Dec 2009
    Link to this post

    Thanks for that.  Cant seem to get InitializeControls to fire (doesnt hit any of the breakpoints in the code). Here's my code i've added to App_Code/Custom:

    CustomLibraryItemsList.vb:

    Imports Microsoft.VisualBasic  
    Imports Telerik.Cms.Web.UI  
     
    Public Class LibraryItemsList  
        Inherits ViewModeControl(Of LibraryItemsList)  
     
        Protected Overrides Sub InitializeControls(ByVal viewContainer As System.Web.UI.Control)  
            MyBase.InitializeControls(viewContainer)  
     
            Dim dataSource As New ObjectDataSource()  
            dataSource.ID = "DataSource1" 
            dataSource.TypeName = "CustomLibraryManager" 
            dataSource.SelectMethod = "CustomGetAllLibraries" 
        End Sub 
     
    End Class 


    CustomLibraryManager.vb:

    Imports Microsoft.VisualBasic  
    Imports System.Web.UI  
    Imports Telerik.Cms.Web  
    Imports Telerik.Cms.Web.UI  
    Imports Telerik.Libraries  
     
    Public Class CustomLibraryManager  
        Inherits LibraryManager  
     
        Public Sub New()  
        End Sub 
     
        Public Sub New(ByVal providerName As String)  
            MyBase.New(providerName)  
        End Sub 
     
        Public Function CustomGetAllLibraries(ByVal from As IntegerByVal max As IntegerByVal SortExp As StringAs IList  
            Dim AllLibraries As IList = MyBase.GetAllLibraries(0, 0, "Author")  
            AllLibraries.Clear()  
     
            Return AllLibraries  
        End Function 
     
    End Class 

    When i view Items & Documents it's just got the full list not the cleared list as expected.

    Thanks,
    Chris
  17. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    04 Dec 2009
    Link to this post
    Hello Chris Bond,

    ObjectDataSource is not written correctly.You are missing SelectCountMethod, MaximumRowsParameterName , StartRowIndexParameterName, SortParameterName, DeleteMethod, ObjectCreating,Selecting. The control dataSource  is not added to the controls collection as well.
    More information about ObjectDataSource server Web control can be found at

    ObjectDataSource Web Server Control Overview
    Using Parameters with the ObjectDataSource Control
    BASICS: ObjectDataSource Control

    In the Libraries View you have to overridde Name property.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
17 posts, 0 answered