AutoSecurityDescriptor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Globalization;
  2. using System.Linq;
  3. using System.Reflection;
  4. namespace InABox.Core
  5. {
  6. public interface IAutoSecurityAction<TEntity>
  7. {
  8. string Prefix { get; }
  9. string Postfix { get; }
  10. bool Value { get; }
  11. }
  12. public class CanView<TEntity> : IAutoSecurityAction<TEntity>
  13. {
  14. public string Prefix => "View";
  15. public string Postfix => "";
  16. public bool Value => true;
  17. }
  18. public class CanEdit<TEntity> : IAutoSecurityAction<TEntity>
  19. {
  20. public string Prefix => "Edit";
  21. public string Postfix => "";
  22. public bool Value => typeof(TEntity).GetCustomAttribute<AutoEntity>() == null;
  23. }
  24. public class CanImport<TEntity> : IAutoSecurityAction<TEntity>
  25. {
  26. public string Prefix => "Import";
  27. public string Postfix => "";
  28. public bool Value => typeof(TEntity).GetCustomAttribute<AutoEntity>() == null;
  29. }
  30. public class CanExport<TEntity> : IAutoSecurityAction<TEntity>
  31. {
  32. public string Prefix => "Export";
  33. public string Postfix => "";
  34. public bool Value => false;
  35. }
  36. public class CanMerge<TEntity> : IAutoSecurityAction<TEntity>
  37. {
  38. public string Prefix => "Merge";
  39. public string Postfix => "";
  40. public bool Value => typeof(TEntity).GetCustomAttribute<AutoEntity>() == null;
  41. }
  42. public class CanPost<TEntity> : IAutoSecurityAction<TEntity>
  43. {
  44. public string Prefix => "Post";
  45. public string Postfix => "";
  46. public bool Value => true;
  47. }
  48. public class CanConfigurePost<TEntity> : IAutoSecurityAction<TEntity>
  49. {
  50. public string Prefix => "Configure";
  51. public string Postfix => "Post Settings";
  52. public bool Value => true;
  53. }
  54. public class CanDelete<TEntity> : IAutoSecurityAction<TEntity>
  55. {
  56. public string Prefix => "Delete";
  57. public string Postfix => "";
  58. public bool Value =>
  59. (typeof(TEntity).GetCustomAttribute<AutoEntity>() == null) &&
  60. (typeof(TEntity).GetInterfaces().Any(x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(IManyToMany<,>)));
  61. }
  62. public class CanManageIssues<TEntity> : IAutoSecurityAction<TEntity>
  63. {
  64. public string Prefix => "Manage";
  65. public string Postfix => "Issue";
  66. public bool Value => (typeof(TEntity).GetCustomAttribute<AutoEntity>() == null);
  67. }
  68. public class AutoSecurityDescriptor<TEntity, TAction> : ISecurityDescriptor
  69. where TEntity : Entity where TAction : IAutoSecurityAction<TEntity>, new()
  70. {
  71. private TAction _action = new TAction();
  72. public virtual bool Visible => true;
  73. public string Category => new Inflector.Inflector(new CultureInfo("en"))
  74. .Pluralize(
  75. typeof(TEntity).EntityName().Split('.').Last().SplitCamelCase()
  76. );
  77. public string Type => GetLicenseCaption();
  78. public virtual string Code => GetCode();
  79. public virtual string Description
  80. {
  81. get
  82. {
  83. var caption = GetType().GetCaption(false);
  84. return string.IsNullOrWhiteSpace(caption)
  85. ? GetCode().SplitCamelCase()
  86. : caption;
  87. }
  88. }
  89. public virtual bool Value => _action.Value;
  90. public virtual bool HasScope(SecurityDescriptorScope scope)
  91. {
  92. return true;
  93. }
  94. private string GetLicenseCaption()
  95. {
  96. var license = typeof(TEntity)
  97. .GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ILicense<>))
  98. ?.GenericTypeArguments.FirstOrDefault();
  99. return license != null ? license.GetCaption() : typeof(CoreLicense).GetCaption();
  100. }
  101. private string GetCode()
  102. {
  103. return string.Format(
  104. "Can{0}{1}{2}",
  105. _action.Prefix,
  106. string.IsNullOrWhiteSpace(_action.Postfix)
  107. ? new Inflector.Inflector(new CultureInfo("en")).Pluralize(typeof(TEntity).EntityName().Split('.').Last())
  108. : typeof(TEntity).EntityName().Split('.').Last(),
  109. string.IsNullOrWhiteSpace(_action.Postfix)
  110. ? ""
  111. : new Inflector.Inflector(new CultureInfo("en")).Pluralize(_action.Postfix)
  112. );
  113. }
  114. }
  115. }