Results 1 to 32 of 32

Thread: [RESOLVED] How to enable the menus that are assigned to a user

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Resolved [RESOLVED] How to enable the menus that are assigned to a user

    Hello. I've a table AssignedMenus in which, I assign necessary menus to each user , he needs to work with. Its kind of rights and Privileges table. What i want is that Admin can view all menus after log in, Operator can view only 2 or 3 etc and Supervisor can view so many menus but less than Admin.
    Table fields are:

    AssignID, userID, MenuID and MenuName.
    MenuID is itself a table.


    Now What i want that when the application loads up all menus will be off and when a user logs in successfully so only those menus will enable to him which are assigned to him in AssignedMenus Table. I hope my point is clear. Please tell me that how do i do this. Thank you.

  2. #2

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How to enable the menus that are assigned to a user

    I'd probably have a table with the menu options in it... and then another table that had the list of roles....and then a RoleMenu table that liks the menu options to the roles, then I'd add a RoleID to the users table - or if the user can have more than one role, I'd have a UserRole table... when a user logs in, I get their role, find out which menu options they have and turn them on.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to enable the menus that are assigned to a user

    It seems to me that you're going about this in the wrong way. Menus should not be assigned on a per-user basis but on a per-role basis. When a user logs in, you check their role(s) and then retrieve all the menu records for that role(s). If roles are subsets and supersets of each other, i.e. a supervisor can do everything that an operator can do plus more and an administrator can do everything a supervisor can do plus more, then you just need a minimum role value in the menu table, otherwise you'd need an extra table that joins menus and roles.

    Once you've retrieved the menu records, it's a simple matter of adding an item to a collection in a MenuStrip for each one. Where exactly are you stuck?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Well I have this time 2 tables,
    The Users Table which has the UserID, Name and Pwd etc,
    UserTypes which has Types and
    Menus which has the all the menus and their IDs and
    AssignedUserMenus which has AssignMenuID, UserID, MenuID.

    Now do i need to work with it or i need to make roles tables? (Do you have Roles topic in your Signature John?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Hello tg
    How to create this table of roles and RoleMenu Table? Please explain little. These roles and Privileges are new for to work for the first time.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Hello.

    mmm.. I would use the table AssignedUserMenus. I would open it in a datatable, or a reader, whatever you like the most, then i would make a loop that reads every record of it and for each one i would set the menu enabled or not.

    But that's just me.

    HTH

    Heishiro

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Quote Originally Posted by heishiro View Post
    Hello.

    mmm.. I would use the table AssignedUserMenus. I would open it in a datatable, or a reader, whatever you like the most, then i would make a loop that reads every record of it and for each one i would set the menu enabled or not.

    But that's just me.

    HTH

    Heishiro
    Similar kind of thing I Need. I tried so many times to accomplish this but not achieving the result. I"m not grasping the idea that how to loop through all records.

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Hello.

    Try to understand how i went through all the records in a datatable...

    Of course, you'll have to fill the datatable with your own records, and then activate the menu or not depending on a boolean

    Code:
         Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dt As New DataTable 'create a datatable
            dt.Columns.Add("UserName") 'Assign columns to the datatable
            dt.Columns.Add("MenuID")
            dt.Columns.Add("IsAllowed")
    
            dt.Rows.Add({"John", "Menu1", True}) 'Add example rows
            dt.Rows.Add({"John", "Menu2", True})
            dt.Rows.Add({"John", "Menu3", False})
            dt.Rows.Add({"John", "Menu4", False})
            dt.Rows.Add({"John", "Menu5", True})
    
            ' Looping all records
            For Each UserMenu As DataRow In dt.Rows
                If UserMenu("IsAllowed") = True Then
                    MsgBox(UserMenu("MenuID") & " is allowed")
                Else
                    MsgBox(UserMenu("MenuID") & " not allowed")
                End If
            Next
        End Sub

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    This is my Login Button Code, where first the login will be match and after that textbox data will be saved into global variables. and after Query will execute to fetch AssignmedMenus Table Data and loading it into dataTable dtMenu and then looping on it but I don't know how do i go further.

    Code:
            Dim selLog As String = "select * from Users Where UserTypeID = '" & CInt(cmbUserType.SelectedValue) & "' and UserName = '" & txtUserName.Text.Trim & "' and UserPassword = '" & txtPassword.Text.Trim & "'"
    
            Dim cmdSql1 As New SqlCommand(selLog, cnSql)
            cmdSql1.CommandType = CommandType.Text
            daSql.SelectCommand = cmdSql1
            daSql.Fill(dsSql1, "Users")
    
            dtLogin = dsSql1.Tables("Users")
    
    
            If dtLogin.Rows.Count > 0 Then
    
                MessageBox.Show(" Welcome to Computerized Pharmacy of Waheed Pharmaceutical", " Waheed Pharmacy")
    
                uID = CInt(cmbUserType.SelectedValue)
                uName = txtUserName.Text
                uPwd = txtPassword.Text
    
    
                ' ---   Calling AssignMenus Table ----    uID holds the UserID.
                Dim selMenu As String = "select MenuID, MenuName from AssignedMenus where UserID = " & uID & ""
                Dim cmdSql2 As New SqlCommand(selMenu, cnSql)
                cmdSql2.CommandType = CommandType.Text
                daSql.SelectCommand = cmdSql2
                daSql.Fill(dsSql2, "AssignedMenus")
    
                dtMenu = dsSql1.Tables("AssignedMenus")
    
                For i As Integer = 0 To dtMenu.Rows.Count - 1
                    If My.Forms.MdiForm.MenuStrip.Items("msSale") = dtMenu.Rows(i).Item("MenuName") Then
                        ' Then Enable the menuName
                    End If
                Next

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Hello.... mmm...

    I would have to try it, but maybe removing the conditional and just looping this command would do.

    Code:
                For i As Integer = 0 To dtMenu.Rows.Count - 1
                       My.Forms.MdiForm.MenuStrip.Items(dtMenu.Rows(i).Item("MenuName")).enabled=true
                Next
    ?

    HTH

    Heishiro

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Name:  Menus.jpg
Views: 220
Size:  5.8 KB
    This the Menu Image of my application in run mode.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    This code has a much bigger error.

    Overload resolution failed because no accessible 'Item' can be called with these arguments:
    'Public Overridable ReadOnly Default Property Item(key As String) As System.Windows.Forms.ToolStripItem': Option Strict On disallows implicit conversions from 'Object' to 'String'.
    'Public Overridable ReadOnly Default Property Item(index As Integer) As System.Windows.Forms.ToolStripItem': Option Strict On disallows implicit conversions from 'Object' to 'Integer'.

  14. #14
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Hello.

    Let me try modifying the code you gave me...

    BRB

    Heishiro

  15. #15
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Hello.

    This code works here not using a boolean, but the menu name instead. All menuitems are disabled on load...

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dt As New DataTable 'create a datatable
            dt.Columns.Add("UserName") 'Assign columns to the datatable
            dt.Columns.Add("MenuID")
    
            dt.Rows.Add({"John", "Menu1"}) 'Add example rows
            dt.Rows.Add({"John", "Menu2"})
            dt.Rows.Add({"John", "Menu4"})
            dt.Rows.Add({"John", "Menu5"})
    
            ' Looping all records
            For Each UserMenu As DataRow In dt.Rows
                MenuStrip1.Items(UserMenu.Item("MenuID") & "toolstripmenuitem").enabled = True
            Next
    
    
        End Sub

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Heishiro, My code is not like you are working upon one. I don't have any row to add to dataTable. Take a look at my code in post # 10.

  17. #17
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Hello.

    I saw your code in post #10. All the first part of your code is just for filling the datatable you need to loop....

    Code:
            Dim selLog As String = "select * from Users Where UserTypeID = '" & CInt(cmbUserType.SelectedValue) & "' and UserName = '" & txtUserName.Text.Trim & "' and UserPassword = '" & txtPassword.Text.Trim & "'"
    
            Dim cmdSql1 As New SqlCommand(selLog, cnSql)
            cmdSql1.CommandType = CommandType.Text
            daSql.SelectCommand = cmdSql1
            daSql.Fill(dsSql1, "Users")
    
            dtLogin = dsSql1.Tables("Users")
    
    
            If dtLogin.Rows.Count > 0 Then
    
                MessageBox.Show(" Welcome to Computerized Pharmacy of Waheed Pharmaceutical", " Waheed Pharmacy")
    
                uID = CInt(cmbUserType.SelectedValue)
                uName = txtUserName.Text
                uPwd = txtPassword.Text
    
    
                ' ---   Calling AssignMenus Table ----    uID holds the UserID.
                Dim selMenu As String = "select MenuID, MenuName from AssignedMenus where UserID = " & uID & ""
                Dim cmdSql2 As New SqlCommand(selMenu, cnSql)
                cmdSql2.CommandType = CommandType.Text
                daSql.SelectCommand = cmdSql2
                daSql.Fill(dsSql2, "AssignedMenus")
    
                dtMenu = dsSql1.Tables("AssignedMenus")
    Now, the part where you loop through yor datatable (DTMENU), is this one...

    Code:
                For i As Integer = 0 To dtMenu.Rows.Count - 1
                    If My.Forms.MdiForm.MenuStrip.Items("msSale") = dtMenu.Rows(i).Item("MenuName") Then
                        ' Then Enable the menuName
                    End If
                Next
    That's what i'm changing. In my code, this part is just to provide a datatable with some data....

    Code:
            Dim dt As New DataTable 'create a datatable
            dt.Columns.Add("UserName") 'Assign columns to the datatable
            dt.Columns.Add("MenuID")
    
            dt.Rows.Add({"John", "Menu1"}) 'Add example rows
            dt.Rows.Add({"John", "Menu2"})
            dt.Rows.Add({"John", "Menu4"})
            dt.Rows.Add({"John", "Menu5"})
    My datatable name is DT... yours is DTMENU.

    The way i'm doing the loop is using "for each" instead of determining the rows and doing a numeric loop.

    The instruction that matters here is this one:

    Code:
    MenuStrip1.Items(UserMenu.Item("MenuID") & "toolstripmenuitem").enabled = True
    Assuming that on load all menustrip1 items are disabled, that instruction will only enable the menu's that the user has rights to. You'll only have to match the "menuID" & "toolstripmenuitem" thingy to the menu names.

    HTH

    Heishiro.

  18. #18
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    ... and also you should use for each....

    HTH

    Heishiro

  19. #19
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How to enable the menus that are assigned to a user

    Role based method:

    User table
    ---------------
    ID
    UserName
    ... what ever else you need ...

    Role table
    ---------------
    ID
    RoleName
    Description
    ... what ever else you need ...

    Menu table
    ---------------
    ID
    Name
    DisplayName
    ... what ever else you need ...



    UserRole table - this links a user to their assigned role(s)
    ----------------
    ID
    UserID - FKey to the User table, ID field
    RoleID - FKey to the Role table, ID field


    MenuRole table - links the roles to their menu options
    ----------------
    ID
    RoleID - FKey to the Role table, ID field
    MenuID - FKey to the Menu table, ID field




    So when I login, I get my menu options like this:
    Code:
    Select 
      M.NAME -- I'm guessing this will simply match the names of the menu items (Menu1 for example)
    from Users U
    inner join UserRole UR on U.ID = UR.USERID
    inner join Role R on UR.RoleID = R.ID
    inner join RoleMenu RM on R.ID = RM.RoleID 
    where U.UserName = 'TechGnome'
    From that you can loop through it in your datatable, and set the menus to on.

    you can actually skip the jump through the Role table since the RoleMenu table will have the Role ID
    Code:
    Select 
      M.NAME
    from Users U
    inner join UserRole UR on U.ID = UR.UserID
    inner join RoleMenu RM on UR.RoleID = RM.RoleID 
    where U.UserName = 'TechGnome'

    We used this kind of a system to control access to screens in an app (we had a Screen table, not a menu one, and it also included CRUD level permissions, I've simplified it for this example) it works great and it's expandable. We then also had a screen where you can maintain the roles, the screens and user role memberships.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Quote Originally Posted by heishiro View Post
    Hello.


    Code:
    MenuStrip1.Items(UserMenu.Item("MenuID") & "toolstripmenuitem").enabled = True
    Assuming that on load all menustrip1 items are disabled, that instruction will only enable the menu's that the user has rights to. You'll only have to match the "menuID" & "toolstripmenuitem" thingy to the menu names.

    HTH

    Heishiro.
    What is MenuStrip1? Is it MenuStrip on Form?

    secondly, The way you are adding rows to dataTable, I"ve done this in assigning dataset to DataTable way. This is giving the same error i mentioned earlier.

  21. #21
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How to enable the menus that are assigned to a user

    Quote Originally Posted by ADQUSIT View Post
    What is MenuStrip1? Is it MenuStrip on Form?

    secondly, The way you are adding rows to dataTable, I"ve done this in assigning dataset to DataTable way. This is giving the same error i mentioned earlier.
    It's called sample code... since MenuStrip1 is the default name for the first MenuStrip dropped onto a form... yeah, I'd guess that it's a MenuStrip on the form.

    And what error? I tried going back and I don't see any mention of an error earlier...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    Quote Originally Posted by techgnome View Post
    It's called sample code... since MenuStrip1 is the default name for the first MenuStrip dropped onto a form... yeah, I'd guess that it's a MenuStrip on the form.

    And what error? I tried going back and I don't see any mention of an error earlier...

    -tg
    The Error is mentioned in post #13.

  23. #23
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    Quote Originally Posted by ADQUSIT View Post
    What is MenuStrip1? Is it MenuStrip on Form?
    Hello

    It's a Menustrip in the form. I added Menu1, Menu2, etc to it.

    Here works flawlessly.

    HTH

    Heishiro

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    I created the object of the MenuStrip, available on my form but same error.

  25. #25
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    The Error is mentioned in post #13.
    Hello.

    The error you mention was when i told you

    I would have to try it, but maybe removing the conditional and just looping this command would do.
    I couldn't test it before posting the answer. I left the testing to you.

    The code in my last post should do the trick anyway.

    HTH

    Heishiro.

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    The code you are providing, I can't use. I have to receive the tables and columns from DataTable or DataSet not like the way you have created them.

    I tried to do something new here:

    Code:
    For i As Integer = 0 To dtMenu.Rows.Count - 1
    
                    Dim mst As String
                    mst = (dtMenu.Rows(i).Item("MenuName")).ToString
    
                    If mst = (dtMenu.Rows(i).Item("MenuName")).ToString Then
    
                    ' --- How to enable the menu, which is in mst? ----
                    End If
                Next

  27. #27
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    secondly, The way you are adding rows to dataTable, I"ve done this in assigning dataset to DataTable way. This is giving the same error i mentioned earlier.
    Hello

    Yes. I know that you are assigning a Dataset to a Datatable. I created a SAMPLE DATATABLE in my code so i won't have to create a SAMPLE DATABASE, TABLE, and program all the code for filling the datatable using SQL. My idea was helping you on the Menu Permission thing. That's all.

    The last code i gave you on post 17 does what you need. You just need to adapt the code to your control's names.

    HTH

    Heishiro.

  28. #28
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: How to enable the menus that are assigned to a user

    apart from the current problems you seem to go through at the moment with getting some code to work, you should secondly investigate the role based suggestion by techgnome and thirdly read this for correct authentication: http://www.vbforums.com/showthread.p...nfirm-password because this:
    Code:
    Dim selLog As String = "select * from Users Where UserTypeID = '" & CInt(cmbUserType.SelectedValue) & "' and UserName = '" & txtUserName.Text.Trim & "' and UserPassword = '" & txtPassword.Text.Trim & "'"
    is terrible. in multiple ways when also considering sql injection.
    Last edited by digitalShaman; Sep 25th, 2014 at 12:33 PM.

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    That's not helping me. I'm regret to say that I'm not Understanding that how to do that... I mean My pick is not that sharp that i could grasp it. Aahh. mm'm I can't say anything.

  30. #30
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: How to enable the menus that are assigned to a user

    That's not helping me.
    i know, but maybe it will on the long run.

  31. #31
    Member
    Join Date
    Jun 2011
    Posts
    36

    Re: How to enable the menus that are assigned to a user

    That's not helping me
    I really tried helping you, but there's a point where you also must understand what you are doing in order for our suggestions to work.

    I can't replicate your code and server conditions exactly so i can't modify your code and give it to you solved.

    If you put a little effort in it, you will see that post #17 is the one that should help you.

    Heishiro.

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to enable the menus that are assigned to a user

    HI. My problem has been solved. I tried a code which helped me and put me lot..

    Code:
    For Each r As DataRow In dtmenu.Rows
    My.Forms.MdiForm.MenuStrip.Items(r.Item("MenuName").ToString).Enabled = True 
    Next
    Thank you all of you helping me in the solution of this problem. Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width