Currently I have 4 permissions in my website
Edit
Delete
Add
View
Everyone gets view permissions when someone subscribes to someone else.
Currently in I store each as a column in my database as a bit value.
I am thinking maybe I should use enum with flags but I have some questions.
- The user will be able to choose the users permissions(eveyone gets view you cannot take that away). Right now it is easy as I use a checkbox and then save the bool value to the db.
My questions is what is the best way to convert checkboxes to the enum type? So if they choose add permission I can map that to the right enum type.
Wondering if there is a better way then
PermissionTypes t;
if(add == true)
{
t = PermissionTypes.Add
}
if(edit == true)
{
t += PermissionTypes.Edit // not sure if you even can do this.
}
I know with enum flags you can use the pipe and do something like this
PermissionTypes t = PermissionTypes.Add | PermissionTypes.Edit
I am wondering is it good practice to have another type in my enum like
AddEdit = Add | Edit
I am thinking that can result in many combinations(imagine if I had like 10 permissions or something like that).
Enums are simply integers values. By settings your enum values to different power of 2, you can easily have any combination you need:
if you want to know if some user has the Add permission just do as follow: