The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Using Telerik's RadMenu to handle Roles

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Wise

Posts: 126
Nickname: ewise
Registered: Apr, 2005

Eric Wise is a senior .NET consultant.
Using Telerik's RadMenu to handle Roles Posted: Oct 18, 2005 5:40 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Wise.
Original Post: Using Telerik's RadMenu to handle Roles
Feed Title: Eric Wise
Feed URL: /error.htm?aspxerrorpath=/blogs/eric.wise/rss.aspx
Feed Description: Business & .NET
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Wise
Latest Posts From Eric Wise

Advertisement

My company has been a customer of Telerik for about 6 months now, and we've been nothing but happy with the service and support we've gotten from them and the continuous improvement of quality in their controls.  I highly recommend checking them out.

Anyways, recently I was asked whether I could implement some role based security for displaying the items of a telerik menu so here is how I wired it up programatically.

Step 1: Create XML File

The first step was to create an xml file with the format and fields I needed for the menu items.  Instead of using the group xml codes like in the telerik samples, I settled for a parent/child id relationship.  So the end result is xml items with itemId, parentId (if null it's a top level item), navigateurl, text, target, and roles.  Note that the item and parent id's aren't numbers, they must be strings.  Like so:

<MainMenu>

<Item>

<menuItemID>i1</menuItemID>

<menuText>Distribute Inventory</menuText>

</Item>

<Item>

<menuItemID>i2</menuItemID>

<parentItem>i1</parentItem>

<menuText>Overall</menuText>

<navigateURL></navigateURL>

<target></target>

<roles></roles>

</Item>

<Item>

<menuItemID>i3</menuItemID>

<parentItem>i1</parentItem>

<menuText>By Individual</menuText>

<navigateURL></navigateURL>

<target></target>

<roles></roles>

</Item>

// etc

</MainMenu>

 

Step 2: Load and Cache the dataset/handle roles.

Since the menu structure will be accessed fairly frequently, it makes sense to cache it.  Also, we need a function to check the roles since if the role tag is blank we need to handle that as "public".  Note that you could delimit multiple roles into this tag but that is beyond the scope of this sample.  For simplicity, we will use the dataset's ReadXML method.  Notice that we are setting up a relation to link up the parent/child ids.

    Private Function CheckRole(ByVal roles As String)

        If roles = "" Then Return True

        Return context.User.IsInRole(roles)

    End Function

 

    Private Function GetMenuItems() As DataSet

        Dim ds As DataSet

 

        If Not IsNothing(Cache("MenuItems")) Then

            ds = CType(Cache.Get("MenuItems"), DataSet)

        Else

            ds = New DataSet

            ds.ReadXml(MapPath("~\XML\MainMenu.xml"))

            ds.Relations.Add("ItemRelation", ds.Tables(0).Columns("menuItemID"), ds.Tables(0).Columns("parentItem"))

            Cache.Add("MenuItems", ds, Nothing, DateTime.Now.AddHours(4), Nothing, Caching.CacheItemPriority.AboveNormal, Nothing)

        End If

 

        Return ds

    End Function

 

Step 3: Create a method for building a menu item from a datarow

Since we'll be walking the dataset and grabbing menu item information from datarows, we need a method to handle parsing the data.  Simple enough:

    Private Sub CreateItem(ByRef item As MenuItem, ByRef dbRow As DataRow)

        item.Text = Convert.ToString(dbRow("menuText"))

        item.ID = Convert.ToString(dbRow("MenuItemID"))

 

        If Not IsDBNull(dbRow("navigateURL")) Then

            item.Href = Convert.ToString(dbRow("navigateURL"))

        End If

 

        If Not IsDBNull(dbRow("target")) Then

            item.Target = Convert.ToString(dbRow("target"))

        End If

    End Sub

 

Step 4: Loop all the top level items

Remember I said the top level items are items where the parentid is null.  We need to loop the dataset and find all the parent items, when a parent item is found we need to load its child items.

    Protected Sub GenerateMainMenu()

        Dim addItem As Boolean

        Dim ds As DataSet = GetMenuItems()

 

        'Setup Menu

        Dim topGroup As New MenuGroup(RadMenu1)

        topGroup.Flow = PresentationDirection.Horizontal

        topGroup.ExpandDirection = ExpandDirection.Down

        RadMenu1.RootGroup = topGroup

 

        For Each dbRow As DataRow In ds.Tables(0).Rows

            addItem = False

            'Top level rows have a null parentItem

            'We will catch all the top items then query their children to build the menu

            If dbRow.IsNull("parentItem") Then

                If Not IsDBNull(dbRow("roles")) Then

                    addItem = CheckRole(dbRow("roles"))

                Else

                    addItem = True

                End If

            End If

 

            If addItem Then

                Dim item As New MenuItem(topGroup)

                CreateItem(item, dbRow)

                RecursivelyPopulate(dbRow, item)

                topGroup.AddItem(item)

            End If

        Next dbRow

    End Sub

 

Step 5: Recursively Populate the Child Items

The final step is to write the RecursivelyPopulate method which will take a parent item and query the dataset to see if there are child rows, if there are it will wire them up and then pass them through the same function (so it will go down multiple levels indefinitely).  Here's the code:

    Private Sub RecursivelyPopulate(ByRef parentRow As DataRow, ByRef parentItem As MenuItem)

        Dim childRow As DataRow

        Dim addItem As Boolean

        Dim childGroup As MenuGroup

 

        For Each childRow In parentRow.GetChildRows("ItemRelation")

            addItem = False

 

            'instantiate group if necessary, we're doing it in the loop since we only want the group if there are child items

            If IsNothing(childGroup) Then

                childGroup = New MenuGroup(parentItem)

                childGroup = parentItem.AddChildGroup()

                childGroup.Flow = PresentationDirection.Vertical

            End If

 

            If Not IsDBNull(childRow("roles")) Then

                addItem = CheckRole(childRow("roles"))

            Else

                addItem = True

            End If

 

            If addItem Then

                Dim item As New MenuItem(childGroup)

                CreateItem(item, childRow)

                RecursivelyPopulate(childRow, item)

                childGroup.AddItem(item)

            End If

        Next childRow

    End Sub

 

So you can see, Telerik's menu control is very flexible and even if you want to do some things outside the scope of their standard examples you can do so without much hassle!

Read: Using Telerik's RadMenu to handle Roles

Topic: Life Is Better With... Previous Topic   Next Topic Topic: Understanding SDM: Systems and the Four Layer Model

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use