In c# it is quite common to find the following Enum structure
[Flags] public enum Permission { Read = 1 << 1, Create = 1 << 2, Update = 1 << 3, Destroy = 1 << 4 } which allows you to join enums like this: Permission.Read|Permission.Create
I am now faced with a different sort of requirement, and the solutions I have come up with are problematic IMO.
I need to allow some sort of enum implementation to multiple types of premissions - some contradicting, and some aren't
and I want the following sort of functionality
[Flags] public enum Permission { Read1 = 1, Read2 = 2, Read3 = 3, Write1 = 10, Write2 = 20, Write3 = 30, Update1 = 100, Update2 = 200, Update3 = 300, Destory = 1000, Other = 10000, SomethingElse = 100000, } when this won't work Permission.Read1|Permission.Read2 mainly because it means a user now has a reading permission level 3
besides using different bit flag for each Permission (which will require my db to hold a much larger integer than a INT for a very bad reason), or having a different enum (and column) per permission (which will limit my flexibility with the permissions) , and having no form of compile time verification (I guess I can create some sort of a workarroundish runtime verification) do you have any other idea?
PermissionFlags,ObjectType(same for a table in a relational database, 2 columns). ObjectType would be the thing that the permission applies to. Keep your permissions as generic as possible. I doubt that there is a solution that would enable you to catch illegal combinations of permissions at compile time, you would have to create run-time validation and/or add validation logic in the database.