Question

I know the subject might be a little strange, but I wasn't sure how to exactly describe my goal

I'm trying to do some Role based permissions in a content management system. The best way I can think of to do this is to pull the list of roles from the database and place them in a CheckBoxList. From there, I save a comma separated string of the checked values into the page details of the database. Finally when the page is pulled back up, I check if the current User has permission by pulling down the comma separated string and splitting it into a String() and loop through the values.

The concern I have is that when I loop through the CheckBoxList and convert the values to a string, the loop method adds a comma to the end of the string... I then have to strip the comma before saving it to the database.

My question is, is there a better way of doing this so that I don't have to go back into the string and strip the trailing comma before saving?

    ''# this is to get all selected items from
    ''# a checkboxlist and add it to a string
    ''# the reason for this is to insert the final string
    ''# into a database for future retrieval.
    Dim i As Integer
    Dim sb As StringBuilder = New StringBuilder()
    For i = 0 To CheckBoxList1.Items.Count - 1
        If CheckBoxList1.Items(i).Selected Then
            sb.Append(CheckBoxList1.Items(i).Value & ",")
        End If
    Next

    ''# now I want to save the string to the database
    ''# so I first have to strip the comma at the end
    Dim str As String = sb.ToString.Trim(",")
    ''# Then save str to the database

    ''# I have now retrieved the string from the 
    ''# database and I need to now add it to a One Dimensional String()
    Dim str_arr() As String = Split(str, ",")
    For Each s As String In str_arr
        ''# testing purposes only
        Response.Write(s & " -</br>")

        ''# If User.IsInRole(s) Then
        ''#     Do Stuff
        ''# End If
    Next
Was it helpful?

Solution

Put all the values in a list of strings, then use String.Join to concatenate them. I won't comment on keeping a comma-separated list rather than a one-to-many relationship of users to roles. :-)

Dim values as List(Of String) = New List(Of String)()
For i = 0 To CheckBoxList1.Items.Count - 1 
    If CheckBoxList1.Items(i).Selected Then 
        values.Add( CheckBoxList1.Items(i).Value )
    End If 
Next

Dim str As String = String.Join( ",", values.ToArray() )

OTHER TIPS

Could you perhaps simply have a database table that contains a reference to the page (be it by name, path, or some other unique id), and a reference to the role?

I'm certainly not saying this is the best solution (by any means), it's simply an example:

Role Table:

RoleId | RoleName
_________________
1      | Editor
2      | Administrator

Page Table:

PageId | PageName
8      | ~/Page1.aspx
9      | ~/OtherPlace/Page2.aspx

PageRole Table:

RoleId | PageId
2      | 8
2      | 9
1      | 9

You would then store your data in the joining table, and when accessing the page pull back your roles based on the current page. Of course, it doesn't have to based on the page at all, you could just as easily replace Page in my example with Permission.

If you use ASP.NET membership, you can assign multiple roles to a user.

Roles.AddUserToRoles(username, roles);

Roles.IsUserInRole(role);

Just something to consider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top