I'm trying to implement a PostSharp attribute to check whether a user accessing a certain method is authorised to do so. I had implemented a test solution but with hard coded values like so:
[AuthorisationAspect(RolesEnum.Roles.Admin, RolesEnum.Roles.User]
First parameter describes which role the user should have, the second parameter describes the role of the current user. Like I've mentioned this was just a test. What I'm trying to implement now is the same concept with a few differences. Both parameters are now lists because a method could be accessed by multiple roles and a single user could also have multiple roles. Therefore what I'm trying to achieve is comparing these two lists using PostSharp. I've tried a few different ways to solve it but I'm always getting the same error:
"An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type."
Solutions I've tried:
First I've tried something like the following, but just like the error describes I can't call methods inside an attribute.
[AuthorisationAspect(GetRoles(), GetUserRoles()]
But then I realised that this is not possible as only static/constant values can be passed as parameters in attributes.
I've also tried using something based on this solution. How to set dynamic value in my Attribute but again it did not even compile.
Finally I've also looked at the following solution http://geekswithblogs.net/abhijeetp/archive/2009/01/10/dynamic-attributes-in-c.aspx, but it looks far too complicated when what I'm trying to do is use AOP which should make things simpler.
Basically I'm trying to pass dynamic parameters in an attribute and passed to a PostSharp attribute but I can't achieve it. I don't know if it's possible, maybe there is a better way to solve this issue. Any help would be appreciated.
Note: The simplest solution would be to call methods to access the database straight from the PostSharp aspect. However, I can't access these methods from the aspect because referencing the class library where the aspects reside would result in a circular dependency. (I'm using 3-tier architecture)