Trying to control access to the website, I created some necessary objects
The goal is to use a special permission attribute for some action method of the controller of my MVC application.
[Permissions(PermissionType.SomePermissionName, CrudType.CanDelete)] public ActionResult SomeAction() { }
For this operation, I have two enumerations
[Flags] public enum CrudType { CanCreate = 0x1, CanRead = 0x2, CanUpdate = 0x4, CanDelete = 0x8, } [Flags] public enum PermissionType { SomePermissionName = 0x1,
Now I want the method below to check permissions
public static bool CanAccess(RolePermissions rp, CrudType crudType) { var pInfo = rp.GetType().GetProperties(); var res = pInfo.FirstOrDefault(x => x.Name == crudType.ToString()); if(res != null) { return Convert.ToBoolean(res.GetValue(rp, null)); } return false; }
It works well, but is it safe to use reflection here? Is this a good style?
Another question concerns this part of the code.
var permission = PermissionService.GetByName(permissionType.ToString());
Here I am trying to get a permission object from a database using some named constant from the PermissionType
enumeration.
In both cases, proper operation depends on the relationship between the enumerations and some table fields or records. On the other hand, I have a good mechanism for managing logic (as it seems to me). Is this a good way?
Shymep
source share