C# – How to Manage Privileges in C# WPF applications

cdatabasewpf

I'm writing my first "large" program in C# and WPF. It's a Database system (MySQL) that has three main items, Contracts, Companies and People. I have the main UI down for the Companies section of the Database and I'm now looking at managing user privileges before I go any further.

I've hit a roadblock really on what would be a way to manage user privileges. The privileges would include access like being able to add, edit or delete a Company, the ability to view a Person's private details (home address etc) and access to specific Contract details (value of the job, whose working on it).

There are going to be many different privileges throughout the Database system as many will be very specific to the section you are in (it's not going to be a simple add/edit/delete privilege system). Another layer of complication to this is that the user is going to see different parts of the Database based on which department they work in. For example, a salesman will see some details specific to a company whereas a builder will see something totally different, but on the same company.

My initial thought is to have a privileges table in MySQL, something that looks like this;

enter image description here

Every user has multiple entries into this table, based on which part of the program they are in. For example if they are in sales, then they will have about 7/8 entries purely based on what sales can do and so on and so forth. This would be managed in a system like this;

enter image description here

Where each CheckBox would correspond to a 0 or a 1 in that entry for that person in the Database. When the user then logs into my Database program I setup their privileges at the very beginning – it could reach something like 75 global variables for what the user can or cannot do in the program which I look at further down the line when necessary.

My question really is what would be a way to manage privileges in this system? I haven't had much experience in this area before so am open to suggestions!

Best Answer

I worked on a system that had a list of privileges (IE: Create users, modify customer accounts, etc) that were then assigned to groups. Users were then assigned to those groups and then had all the privileges of the group.

In our application when we wanted to check a permission we had a method DoesUserHavePermission(PermissionEnum.Option) that we could call (and later turned into an MVC controller action filter). The method looked at the user to the group to the permission. This allows us to quickly add a permission to a group of users. IE: All the sales people now need the Add Customer permission, just add it to the sales group and every one gets it.

We had 5 main tables for our permissions: Users, Groups, Privileges, UserGroup, and GroupPrivilege. The logic was that if there was a privilege that a group had there was an entry in the GroupPrivilege table (eliminated the need for bit fields) and if a user was part of a group there was an entry in the UserGroup table. (GroupPrivilege and UserGroup are many to many union tables).

This has kept the configuration down to a minimum after initial setup and if we need a group of users with a different set of permissions we can create a new group with just the privileges needed and assign the users to that group.