SecurityAttribute.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Linq;
  3. namespace InABox.Core
  4. {
  5. /// <summary>
  6. /// Causes a property to update its editable status based on <see cref="SecurityDescriptor"/>. If the current user does not have the security token,
  7. /// then the property's visiblity is restricted to at most <see cref="Editable"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// <see cref="Editable"/> defaults to <see cref="Editable.Hidden"/>.
  11. /// </remarks>
  12. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
  13. public class SecurityAttribute : Attribute
  14. {
  15. public Type SecurityDescriptor { get; set; }
  16. public Editable Editable { get; set; } = Editable.Hidden;
  17. private SecurityAttribute() { }
  18. public SecurityAttribute(Type securityDescriptor)
  19. {
  20. if (!securityDescriptor.GetInterfaces().Contains(typeof(ISecurityDescriptor)))
  21. throw new Exception(securityDescriptor.EntityName() + " is not a valid security descriptor!");
  22. SecurityDescriptor = securityDescriptor;
  23. }
  24. public virtual SecurityAttribute Clone()
  25. {
  26. var result = new SecurityAttribute(SecurityDescriptor)
  27. {
  28. SecurityDescriptor = SecurityDescriptor,
  29. Editable = Editable
  30. };
  31. return result;
  32. }
  33. }
  34. public class CanViewAttribute : SecurityAttribute
  35. {
  36. public CanViewAttribute(Type TEntity): base(
  37. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanView<>).MakeGenericType(TEntity)))
  38. {
  39. }
  40. }
  41. public class CanEditAttribute : SecurityAttribute
  42. {
  43. public CanEditAttribute(Type TEntity) : base(
  44. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanEdit<>).MakeGenericType(TEntity)))
  45. {
  46. }
  47. }
  48. public class CanDeleteAttribute : SecurityAttribute
  49. {
  50. public CanDeleteAttribute(Type TEntity) : base(
  51. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanDelete<>).MakeGenericType(TEntity)))
  52. {
  53. }
  54. }
  55. }