AutoSecurityDescriptor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 && typeof(TEntity).HasInterface<IImportable>();
  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 CanManageProblems<TEntity> : IAutoSecurityAction<TEntity>
  69. {
  70. public string Prefix => "Manage";
  71. public string Postfix => "Problem";
  72. public bool Value => (typeof(TEntity).GetCustomAttribute<AutoEntity>() == null);
  73. }
  74. public class AutoSecurityDescriptor<TEntity, TAction> : ISecurityDescriptor
  75. where TEntity : Entity where TAction : IAutoSecurityAction<TEntity>, new()
  76. {
  77. private TAction _action = new TAction();
  78. public virtual bool Visible => true;
  79. public string Category => new Inflector.Inflector(new CultureInfo("en"))
  80. .Pluralize(
  81. typeof(TEntity).EntityName().Split('.').Last().SplitCamelCase()
  82. );
  83. public string Type => GetLicenseCaption();
  84. public virtual string Code => GetCode();
  85. public virtual string Description
  86. {
  87. get
  88. {
  89. var caption = GetType().GetCaption(false);
  90. return string.IsNullOrWhiteSpace(caption)
  91. ? GetCode().SplitCamelCase()
  92. : caption;
  93. }
  94. }
  95. public virtual bool Value => _action.Value;
  96. public virtual bool HasScope(SecurityDescriptorScope scope)
  97. {
  98. return true;
  99. }
  100. private string GetLicenseCaption()
  101. {
  102. var license = typeof(TEntity)
  103. .GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ILicense<>))
  104. ?.GenericTypeArguments.FirstOrDefault();
  105. return license != null ? license.GetCaption() : typeof(CoreLicense).GetCaption();
  106. }
  107. private string GetCode()
  108. {
  109. return string.Format(
  110. "Can{0}{1}{2}",
  111. _action.Prefix,
  112. string.IsNullOrWhiteSpace(_action.Postfix)
  113. ? new Inflector.Inflector(new CultureInfo("en")).Pluralize(typeof(TEntity).EntityName().Split('.').Last())
  114. : typeof(TEntity).EntityName().Split('.').Last(),
  115. string.IsNullOrWhiteSpace(_action.Postfix)
  116. ? ""
  117. : new Inflector.Inflector(new CultureInfo("en")).Pluralize(_action.Postfix)
  118. );
  119. }
  120. }
  121. }