SecurityAttribute.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Linq;
  3. namespace InABox.Core
  4. {
  5. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
  6. public class SecurityAttribute : Attribute
  7. {
  8. public Type SecurityDescriptor { get; set; }
  9. private SecurityAttribute() { }
  10. public SecurityAttribute(Type securityDescriptor)
  11. {
  12. if (!securityDescriptor.GetInterfaces().Contains(typeof(ISecurityDescriptor)))
  13. throw new Exception(securityDescriptor.EntityName() + " is not a valid security descriptor!");
  14. SecurityDescriptor = securityDescriptor;
  15. }
  16. public virtual SecurityAttribute Clone()
  17. {
  18. var result = new SecurityAttribute(SecurityDescriptor);
  19. result.SecurityDescriptor = SecurityDescriptor;
  20. return result;
  21. }
  22. }
  23. public class CanViewAttribute : SecurityAttribute
  24. {
  25. public CanViewAttribute(Type TEntity): base(
  26. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanView<>).MakeGenericType(TEntity)))
  27. {
  28. }
  29. }
  30. public class CanEditAttribute : SecurityAttribute
  31. {
  32. public CanEditAttribute(Type TEntity) : base(
  33. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanEdit<>).MakeGenericType(TEntity)))
  34. {
  35. }
  36. }
  37. public class CanDeleteAttribute : SecurityAttribute
  38. {
  39. public CanDeleteAttribute(Type TEntity) : base(
  40. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanDelete<>).MakeGenericType(TEntity)))
  41. {
  42. }
  43. }
  44. }