views:

235

answers:

1

Hi there guys, i need some of your programming experience, here is the issue:

When a user logging into a WinForm application (C#.NET), how can i Assign dynamically a defined Menu (differ the menu depending on the user logged into my appz) according to a Role in the DataBase?

Thanks in advance for your support, time and for share your knowledge, it really helps.

Any guru out there?

A: 

You can use the IS_MEMBER('role') t-sql function to detect what roles they have. I would probably wrap it in a stored procedure and return roles that I care about as a set of records (i.e., all the roles the user has that I care about).

Then you simply you use code like:

if (loginEntity.IsAdmin)
{
    MenuItem adminMenu = new MenuItem();
    adminMenu.Text = Resources.AdminMenuText;
    mainMenu.MenuItems.Add(adminMenu);


    item = new MenuItem();
    item.Text = Resources.ManageUsers;
    item.Click += UserAdminClick;
    adminMenu.MenuItems.Add(item);

...
}

where you create your menus. (My loginEntity object here is calling a similar function and sets a bit variable for each role I care about.) The code generates a new toplevel menu (assuming that mainMenu is already defined), and then an "User Admin" item below it, all assuming the current users has an administrator role (say, 'securityadmin').

Godeke