Instead of splitting each permission into individual columns or sql server table we are rethinking of using Flag attribute to Enum for use with bit wise operations. Our application defines end points ( not the one from rest services) for accessing resources( read, modify, update) and each user is entitled with permissions for example Bob has permissions to Read, Modify resource X so when bob comes and accesses this resource we check if bob has permission x.Now that where we are going to apply this is clear here are my doubts
-
Suppose Bob has Read, Write, Delete permission on resource using Flagged enumerations i would do
Permission.Read | Permission.Write | Permission.DeleteIs the end result of bit wise AND a integer or another Enum ? -
For Storing this permission in database table should the column be a
integer? -
If answer to question 1 is
Enumthen should i define another property on thePermissionsenum that represents the integer value of Bit wise and operation?
disclaimer: the Bob referred to in the example does not represent any user in the Stack overflow network of sites or any person. It is just another character
To answer your questions in order:
Assuming you have set the
[Flags]attribute on theenum, and use the correct operator (&and|for bitwise operations) it is essentially both. If you cast the result to an integer, you will get the bitwise and of the relevant values, ie. 7 for your example. If you use.ToString(), then you will get a string back indicating that the value has all three values. I believe that there is aHasFlags()method somewhere, but most code I have seen uses the tried and true bitwise code for comparisons, ie:From a technical standpoint, C#’s type safety helps keep things sane and the result of
|ing or&ing twoenums of the same type is anotherenumof the same type but allenums are simply integers, and can be treated as such in most cases, though some things need explicit casts to/fromint.It should be an integral type, yes. I believe the default type for an enum is
Int32, however you can set that explicitly if you want to match up the database and code exactly.Given all the above, it can still make a lot of sense to have predefined values for commonly used sets of permissions, for example
ReadWrite. In particular, aNoneelement is recommended for all enums by Microsoft, especially if you are going to be casting values. This allows for a fallback and a valid value for freshly initialized variables as well as a sane value fordefault(Permissions). You can set values in your enum based on other ones like so: