SecurityAttribute.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /// <summary>
  10. /// If the user does not have the security token, this property will be <see cref="Visible.Visible"/> if <see langword="true"/>
  11. /// or <see cref="Visible.Hidden"/> if <see langword="false"/>. The default is <see langword="false"/>.
  12. /// </summary>
  13. public bool Visible { get; set; } = false;
  14. /// <summary>
  15. /// If the user does not have the security token, this property will be <see cref="Editable.Editable"/> if <see langword="true"/>
  16. /// or <see cref="Editable.Hidden"/> if <see langword="false"/>. The default is <see langword="false"/>.
  17. /// </summary>
  18. public bool Editable { get; set; } = false;
  19. private SecurityAttribute() { }
  20. public SecurityAttribute(Type securityDescriptor)
  21. {
  22. if (!securityDescriptor.GetInterfaces().Contains(typeof(ISecurityDescriptor)))
  23. throw new Exception(securityDescriptor.EntityName() + " is not a valid security descriptor!");
  24. SecurityDescriptor = securityDescriptor;
  25. }
  26. public virtual SecurityAttribute Clone()
  27. {
  28. var result = new SecurityAttribute(SecurityDescriptor);
  29. result.SecurityDescriptor = SecurityDescriptor;
  30. return result;
  31. }
  32. }
  33. public class CanViewAttribute : SecurityAttribute
  34. {
  35. public CanViewAttribute(Type TEntity): base(
  36. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanView<>).MakeGenericType(TEntity)))
  37. {
  38. }
  39. }
  40. public class CanEditAttribute : SecurityAttribute
  41. {
  42. public CanEditAttribute(Type TEntity) : base(
  43. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanEdit<>).MakeGenericType(TEntity)))
  44. {
  45. }
  46. }
  47. public class CanDeleteAttribute : SecurityAttribute
  48. {
  49. public CanDeleteAttribute(Type TEntity) : base(
  50. typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanDelete<>).MakeGenericType(TEntity)))
  51. {
  52. }
  53. }
  54. }