<p>I often encounter the following scenario where I need to offer many different types of permissions. I primarily use ASP.NET / VB.NET with SQL Server 2000.</p>
<p><strong>Scenario</strong></p>
<p>I want to offer a dynamic permission system that can work on different parameters. Let's say that I want to give either a department or just a specific person access to an application. And pretend that we have a number of applications that keeps growing.</p>
<p>In the past, I have chosen one of the following two ways that I know to do this.</p>
<ol>
<li><p>Use a single permission table with special columns that are used for
determining a how to apply the parameters. The special columns in
this example are <code>TypeID</code> and <code>TypeAuxID</code>. The SQL would look something
like this.</p>
<pre><code>SELECT COUNT(PermissionID)
FROM application_permissions
WHERE
(TypeID = 1 AND TypeAuxID = @UserID) OR
(TypeID = 2 AND TypeAuxID = @DepartmentID)
AND ApplicationID = 1
</code></pre>
</li>
<li><p>Use a mapping table for each type of permission, then joining them
all together.</p>
<pre><code>SELECT COUNT(perm.PermissionID)
FROM application_permissions perm
LEFT JOIN application_UserPermissions emp
ON perm.ApplicationID = emp.ApplicationID
LEFT JOIN application_DepartmentPermissions dept
ON perm.ApplicationID = dept.ApplicationID
WHERE q.SectionID=@SectionID
  AND (emp.UserID=@UserID OR dept.DeptID=@DeptID OR
 (emp.UserID IS NULL AND dept.DeptID IS NULL)) AND ApplicationID = 1
ORDER BY q.QID ASC
</code></pre>
</li>
</ol>
<p><strong>My Thoughts</strong></p>
<p>I hope that the examples make sense. I cobbled them together.</p>
<p>The first example requires less work, but neither of them feel like the best answer. Is there a better way to handle this?</p>

[{'answer_id': 1459, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 4, 'selected': False, 'text': '<p>The way I typically go about coding permission systems is having 6 tables.</p>\n\n<ul>\n<li>Users - this is pretty straight forward it is your typical users table</li>\n<li>Groups - this would be synonymous to your departments</li>\n<li>Roles - this is a table with all permissions generally also including a human readable name and a description</li>\n<li>Users_have_Groups - this is a many-to-many table of what groups a user belongs to</li>\n<li>Users_have_Roles - another many-to-many table of what roles are assigned to an individual user</li>\n<li>Groups_have_Roles - the final many-to-many table of what roles each group has</li>\n</ul>\n\n<p>At the beginning of a users session you would run some logic that pulls out every role they have assigned, either directory or through a group. Then you code against those roles as your security permissions.</p>\n\n<p>Like I said this is what I typically do but your millage may vary.</p>\n'}, {'answer_id': 1466, 'author': 'tags2k', 'author_id': 192, 'author_profile': 'https://Stackoverflow.com/users/192', 'pm_score': 0, 'selected': False, 'text': '<p>An approach I\'ve used in various applications is to have a generic PermissionToken class which has a changeable Value property.  Then you query the requested application, it tells you which PermissionTokens are needed in order to use it.</p>\n\n<p>For example, the Shipping application might tell you it needs:</p>\n\n<pre><code>new PermissionToken()\n{\n    Target = PermissionTokenTarget.Application,\n    Action = PermissionTokenAction.View,\n    Value = "ShippingApp"\n};\n</code></pre>\n\n<p>This can obviously be extended to Create, Edit, Delete etc and, because of the custom Value property, any application, module or widget can define its own required permissions.  YMMV, but this has always been an efficient method for me which I have found to scale well.</p>\n'}, {'answer_id': 1477, 'author': 'jdecuyper', 'author_id': 296, 'author_profile': 'https://Stackoverflow.com/users/296', 'pm_score': 5, 'selected': True, 'text': "<p>I agree with John Downey.</p>\n<p>Personally, I sometimes use a flagged enumeration of permissions. This way you can use AND, OR, NOT and XOR bitwise operations on the enumeration's items.</p>\n<pre><code>&quot;[Flags]\npublic enum Permission\n{\n    VIEWUSERS = 1, // 2^0 // 0000 0001\n    EDITUSERS = 2, // 2^1 // 0000 0010\n    VIEWPRODUCTS = 4, // 2^2 // 0000 0100\n    EDITPRODUCTS = 8, // 2^3 // 0000 1000\n    VIEWCLIENTS = 16, // 2^4 // 0001 0000\n    EDITCLIENTS = 32, // 2^5 // 0010 0000\n    DELETECLIENTS = 64, // 2^6 // 0100 0000\n}&quot;\n</code></pre>\n<p>Then, you can combine several permissions using the AND bitwise operator. <br /><br />\nFor example, if a user can view &amp; edit users, the binary result of the operation is 0000 0011 which converted to decimal is 3. <br />\nYou can then store the permission of one user into a single column of your Database (in our case it would be 3).<br /><br />\nInside your application, you just need another bitwise operation (OR) to verify if a user has a particular permission or not.</p>\n"}, {'answer_id': 1488, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 2, 'selected': False, 'text': '<p>In addition to John Downey and jdecuyper\'s solutions, I\'ve also added an "Explicit Deny" bit at the end/beginning of the bitfield, so that you can perform additive permissions by group, role membership, and then subtract permissions based upon explicit deny entries, much like NTFS works, permission-wise.</p>\n'}, {'answer_id': 4301, 'author': 'Shawn', 'author_id': 26, 'author_profile': 'https://Stackoverflow.com/users/26', 'pm_score': 2, 'selected': False, 'text': "<p>Honestly the ASP.NET Membership / Roles features would work perfectly for the scenario you described. Writing your own tables / procs / classes is a great exercise and you can get very nice control over minute details, but after doing this myself I've concluded it's better to just use the built in .NET stuff. A lot of existing code is designed to work around it which is nice at well. Writing from scratch took me about 2 weeks and it was no where near as robust as .NETs. You have to code so much crap (password recovery, auto lockout, encryption, roles, a permission interface, tons of procs, etc) and the time could be better spent elsewhere.</p>\n\n<p>Sorry if I didn't answer your question, I'm like the guy who says to learn c# when someone asks a vb question.</p>\n"}]
['https://Stackoverflow.com/questions/1451', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/106/']