AutoSecurityDescriptor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 CanDelete<TEntity> : IAutoSecurityAction<TEntity>
  43. {
  44. public string Prefix => "Delete";
  45. public string Postfix => "";
  46. public bool Value =>
  47. (typeof(TEntity).GetCustomAttribute<AutoEntity>() == null) &&
  48. (typeof(TEntity).GetInterfaces().Any(x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(IManyToMany<,>)));
  49. }
  50. public class CanManageIssues<TEntity> : IAutoSecurityAction<TEntity>
  51. {
  52. public string Prefix => "Manage";
  53. public string Postfix => "Issue";
  54. public bool Value => (typeof(TEntity).GetCustomAttribute<AutoEntity>() == null);
  55. }
  56. public class AutoSecurityDescriptor<TEntity, TAction> : ISecurityDescriptor
  57. where TEntity : Entity where TAction : IAutoSecurityAction<TEntity>, new()
  58. {
  59. private TAction _action = new TAction();
  60. public virtual bool Visible => true;
  61. public string Category => new Inflector.Inflector(new CultureInfo("en"))
  62. .Pluralize(
  63. typeof(TEntity).EntityName().Split('.').Last().SplitCamelCase()
  64. );
  65. public string Type => GetLicenseCaption();
  66. public virtual string Code => GetCode();
  67. public virtual string Description
  68. {
  69. get
  70. {
  71. var caption = GetType().GetCaption(false);
  72. return string.IsNullOrWhiteSpace(caption)
  73. ? GetCode().SplitCamelCase()
  74. : caption;
  75. }
  76. }
  77. public virtual bool Value => _action.Value;
  78. public virtual bool HasScope(SecurityDescriptorScope scope)
  79. {
  80. return true;
  81. }
  82. private string GetLicenseCaption()
  83. {
  84. var license = typeof(TEntity)
  85. .GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ILicense<>))
  86. ?.GenericTypeArguments.FirstOrDefault();
  87. return license != null ? license.GetCaption() : typeof(CoreLicense).GetCaption();
  88. }
  89. private string GetCode()
  90. {
  91. return string.Format(
  92. "Can{0}{1}{2}",
  93. _action.Prefix,
  94. string.IsNullOrWhiteSpace(_action.Postfix)
  95. ? new Inflector.Inflector(new CultureInfo("en")).Pluralize(typeof(TEntity).EntityName().Split('.').Last())
  96. : typeof(TEntity).EntityName().Split('.').Last(),
  97. string.IsNullOrWhiteSpace(_action.Postfix)
  98. ? ""
  99. : new Inflector.Inflector(new CultureInfo("en")).Pluralize(_action.Postfix)
  100. );
  101. }
  102. }
  103. }