How to prevent the use of the C # method using an attribute validator?

I would like to create an attribute-based validator that goes a few steps beyond what I saw in the examples. This will basically prevent the execution of methods or other functions.

Keep in mind that I need to use AzMan , because in this case I do not have access to Active Directory.

Here is some kind of pseudo code of what I'm looking for:

// Attribute validator class AttributeUsage is arbitrary at this point and may include other items
[AttributeUsage( AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true, Inherited = true )]
public class PermissionsValidatorAttribute : Attribute
{
 public PermissionsValidatorAttribute(PermissionEnumeration permission){...}
 public bool UserCanCreateAndEdit(){...}
 public bool UserCanDelete(){...}
 public bool UserCanUpload(){...}
}

Here is a sample of the sample / pen to be decorated. The method will not be executed at all if PermissionValidator.UserCanDelete () does not return true from the place where it was executed:

public class DoStuffNeedingPermissions
{
 [PermissionValidator(PermissionEnumeration.MustHaveDeletePermission)]
 public void DeleteSomething(){...}
}

, , . , . , DeleteSomething() , .

, , POC. . , , , -, . , , DoStuffNeedingPermissions. !

+3
2

, PrincipalPermissionAttribute?

[PrincipalPermissionAttribute(SecurityAction.Demand, Name="Bob",
Role="Supervisor")]
+4

, DoStuffNeedingPermissions. , , . , , . , AOP, . , - .

+1

Source: https://habr.com/ru/post/1738039/


All Articles