views:

17

answers:

1

I'm in the middle of implementing permissions on a multi-level tabbed/dropdown menu navigation setup. Is there a better method to implementing permissions compared to wrapping each menu item in an if statement?

User permissions are stored in a table and pulled in to an array, there are 10 menu items and each menu item has numerous sub-menu items. I have, for example, 20 different columns in the database that are boolean flags to indicate access to a particular menu item.

Home
Admin -----> Users
             Notices
             Setup
Projects --> Active Projects
             Inactive Projects
             Updates
Reports
Preferences
Research --> Open Tickets
             Closed Tickets
             Unassigned

Ideally, I want to have a flag for each menu item that determines if the individual account has access to that section. The only method to pull this off I can come up with is wrapping each menu item in an if condition, but this right off the bat sounds like it will get rather unmanageable quickly.

I'm not concerned about performance, since the generated menu is cached on creation (and the cached version referenced each time unless the user permissions are detected to have changed from the cached version - then a new menu is generated).

A: 

I would not have a column per menu item, but a row per menu item in a table something like this:

create table menu_access 
   (userid varchar2(30),
    menuid integer, 
    access_flag integer,
    primary key (username, menuid)
   );

Then you would insert data like:

insert into menu_access (username, menuid, access_flag) values ('TONY', 123, 1);
insert into menu_access (username, menuid, access_flag) values ('TONY', 456, 1);
insert into menu_access (username, menuid, access_flag) values ('TONY', 789, 0);

(An alternative would be to omit the access_flag column altogether and just store the rows for menu items to which the user has access).

This way, the condition for displaying each menu item will be the same, just binding a different value for menuid. You could also perhaps generate the menu by selecting all the rows for which the user has access and constructing the menu accordingly.

Tony Andrews