views:

539

answers:

3

Hi there.

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
+1  A: 

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() )
tvanfosson
Thanks for the input. I'm using the built in membership, so my question above has nothing to do with users to roles... What I'm doing is putting the roles in a string and attaching it to a page. I suppose I could do a one to many relationship of pages to roles, but I just felt that a single field in the pages table would be sufficient. With that however, I don't know how to store an array in a database other than using a comma separated string.
rockinthesixstring
Dim values as List<String> = new List<string>() should be Dim values As New List(Of String)
rockinthesixstring
I've updated the code.
tvanfosson
fixed... thanks
rockinthesixstring
A: 

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.

Paulus E Kurniawan
yeah I have my membership and roles down pat. It's the role permissions on a page... I have that too, my question is more with regards to building the comma separated string without the trailing comma.
rockinthesixstring
+1  A: 

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.

Sychare Jedko
Yes this is also a good idea... It was my second thought really. I just felt that a comma separated string would be just as easy.
rockinthesixstring