-
Jul 17th, 2014, 09:13 AM
#1
Thread Starter
Fanatic Member
[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.
Last edited by ADQUSIT; Sep 24th, 2014 at 09:07 PM.
Reason: Update my question.
-
Sep 24th, 2014, 09:00 PM
#2
Thread Starter
Fanatic Member
Re: How to enable the menus that are assigned to a user
Anyone there to guide me for this pleaassee...
-
Sep 24th, 2014, 09:30 PM
#3
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
-
Sep 24th, 2014, 09:31 PM
#4
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?
-
Sep 25th, 2014, 07:06 AM
#5
Thread Starter
Fanatic Member
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?
-
Sep 25th, 2014, 07:10 AM
#6
Thread Starter
Fanatic Member
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.
-
Sep 25th, 2014, 07:13 AM
#7
Member
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
-
Sep 25th, 2014, 07:38 AM
#8
Thread Starter
Fanatic Member
Re: How to enable the menus that are assigned to a user
Originally Posted by heishiro
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.
-
Sep 25th, 2014, 07:51 AM
#9
Member
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
-
Sep 25th, 2014, 07:58 AM
#10
Thread Starter
Fanatic Member
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
-
Sep 25th, 2014, 08:14 AM
#11
Member
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
-
Sep 25th, 2014, 08:14 AM
#12
Thread Starter
Fanatic Member
Re: How to enable the menus that are assigned to a user
This the Menu Image of my application in run mode.
-
Sep 25th, 2014, 08:17 AM
#13
Thread Starter
Fanatic Member
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'.
-
Sep 25th, 2014, 08:24 AM
#14
Member
Re: How to enable the menus that are assigned to a user
Hello.
Let me try modifying the code you gave me...
BRB
Heishiro
-
Sep 25th, 2014, 08:31 AM
#15
Member
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
-
Sep 25th, 2014, 08:39 AM
#16
Thread Starter
Fanatic Member
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.
-
Sep 25th, 2014, 08:50 AM
#17
Member
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.
-
Sep 25th, 2014, 08:56 AM
#18
Member
Re: How to enable the menus that are assigned to a user
... and also you should use for each....
HTH
Heishiro
-
Sep 25th, 2014, 09:15 AM
#19
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
-
Sep 25th, 2014, 11:07 AM
#20
Thread Starter
Fanatic Member
Re: How to enable the menus that are assigned to a user
Originally Posted by heishiro
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.
-
Sep 25th, 2014, 11:47 AM
#21
Re: How to enable the menus that are assigned to a user
Originally Posted by ADQUSIT
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
-
Sep 25th, 2014, 11:59 AM
#22
Thread Starter
Fanatic Member
Re: How to enable the menus that are assigned to a user
Originally Posted by techgnome
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.
-
Sep 25th, 2014, 12:11 PM
#23
Member
Re: How to enable the menus that are assigned to a user
Originally Posted by ADQUSIT
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
-
Sep 25th, 2014, 12:13 PM
#24
Thread Starter
Fanatic Member
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.
-
Sep 25th, 2014, 12:14 PM
#25
Member
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.
-
Sep 25th, 2014, 12:19 PM
#26
Thread Starter
Fanatic Member
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
Last edited by ADQUSIT; Sep 25th, 2014 at 12:19 PM.
Reason: Coded
-
Sep 25th, 2014, 12:21 PM
#27
Member
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.
-
Sep 25th, 2014, 12:30 PM
#28
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.
-
Sep 25th, 2014, 12:33 PM
#29
Thread Starter
Fanatic Member
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.
-
Sep 25th, 2014, 12:38 PM
#30
Re: How to enable the menus that are assigned to a user
i know, but maybe it will on the long run.
-
Sep 25th, 2014, 01:04 PM
#31
Member
Re: How to enable the menus that are assigned to a user
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.
-
Oct 1st, 2014, 10:42 PM
#32
Thread Starter
Fanatic Member
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.
Last edited by ADQUSIT; Oct 1st, 2014 at 10:45 PM.
Reason: Strange error i received.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|