1
public HasPermissionAttribute(Permissions permission) : base(((int)permission).ToString())
{

}

I want to dynamically check for permissions. Right now I have one single enum having multiple options inside. With that I can use [HasPermission(Permissions.SomePermission)] with the controller and dynamic policy creation does the rest checking if someone has such permission or not.

BUT, it would be nice if I could have more than one enum containing permissions. I could then split permissions into domains, and for example in the future could split the application into domain parts being modules encapsulated inside .dll with their own models, controllers and permissions that main application does not need to know about - but is able to check them anyway by having their values.

So I have such attribute that I could give to Enum that I want to mark as permission container.

[AttributeUsage(AttributeTargets.Enum, AllowMultiple = false)]
public class PermissionsEnumAttribute : Attribute
{
}

I can give it to such enum and dynamically find enums that are marked as permission containers, but how can I preserve ability of IntelliSense to help me? It would be best if HasPermissionAttribute accepted any enum having PermissionEnumAttribute and also giving me some hints what I can actually use.

Is it possible to achieve something like that or I would rather need to use different approach? The main goal is to be able to use [HasPermission] because it's very convinient way to check if someone can access certain controller. Enums were my primary choice as they have autoincremented numeric value that can serve as primary key in code-first approach, so I can just add more options inside and IntelliSense will help me with the rest.

Khaine
  • 295
  • 5
  • 18
  • Very legitimate question. C# unlike Java does not allow for Enum inheriting anything. So it seems this attribute way of things is the only way to ensure what we need. I'd surely believe we'd need a compiler extension that will check the enum type given in parameter. It'd be specified in the parameter attribute. Eg. `func([AllowEnum(typeof(MyEnumAttribute))] enum enumValue)` Not sure of the way though. – StackHola Oct 06 '22 at 10:25
  • I believe the correct c# way would even be a class simulating an Enum : https://stackoverflow.com/a/3007754/1375031 I looks like it will be way more flexible. – StackHola Oct 06 '22 at 10:31
  • But unfortunately this kind of implementation does not allow for the static class values to be added to Attribute constructors or other constant contexts. So I'd still believe enum has its own utility. – StackHola Oct 06 '22 at 10:57

1 Answers1

0

How about using an array instat of an enum?

string[] array = new string[5]; 
array[0] = "Value1"; 
array[1] = "Value1"; 
array[2] = "Value1"; 
array[3] = "Value1"; 
array[4] = "Value1"; 

Method(array, 2); 


static bool Method(string[] array, int i) { 
  if (i >= array.Length) { 
    //Error 
    return false; 
  } 

  string a = array[i]; 
  return true; 
}
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
PandaSITT
  • 37
  • 6
  • string[] array = new string[5]; array[0] = "Value1"; array[1] = "Value1"; array[2] = "Value1"; array[3] = "Value1"; array[4] = "Value1"; Method(array, 2); } static bool Method(string[] array, int i) { if (i >= array.Length) { //Error return false; } string a = array[i]; return true; } – PandaSITT Sep 13 '19 at 11:15
  • Array does not have information that is human-friendly like enum does. IntelliSense can help you with an enum, but can't tell what is inside of an array. It would rather have to be a class with const fields because only consts can be used inside attributes - but then I would have to give key values manually, so when I want to insert permission between others, then I would have to modify all keys manually. I use it like that: `[HasPermission(Permissions.CanReadUsers)] [HttpGet("getUsers")] public async Task> GetUsers()` – Khaine Sep 13 '19 at 11:17