AutoSecurityDescriptor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace InABox.Core
  6. {
  7. public enum AutoSecurityDescriptorActionType
  8. {
  9. View,
  10. Edit,
  11. Delete,
  12. Import,
  13. Export,
  14. Merge,
  15. Post,
  16. ConfigurePost,
  17. ManageIssues,
  18. ManageProblems
  19. }
  20. public interface IAutoSecurityDescriptor : ISecurityDescriptor
  21. {
  22. AutoSecurityDescriptorActionType ActionType { get; }
  23. Type EntityType { get; }
  24. }
  25. public abstract class AutoSecurityDescriptor<TAction> : IAutoSecurityDescriptor
  26. where TAction : IAutoSecurityAction, new()
  27. {
  28. protected abstract Type TEntity { get; }
  29. private TAction _action = new TAction();
  30. public AutoSecurityDescriptorActionType ActionType => _action.Type;
  31. Type IAutoSecurityDescriptor.EntityType => TEntity;
  32. public virtual bool Visible => true;
  33. public string Category => new Inflector.Inflector(new CultureInfo("en"))
  34. .Pluralize(
  35. TEntity.EntityName().Split('.').Last().SplitCamelCase()
  36. );
  37. public string Type => GetLicenseCaption();
  38. public virtual string Code => GetCode();
  39. public virtual string Description
  40. {
  41. get
  42. {
  43. var caption = GetType().GetCaption(false);
  44. return string.IsNullOrWhiteSpace(caption)
  45. ? GetCode().SplitCamelCase()
  46. : caption;
  47. }
  48. }
  49. public virtual bool Value => _action.GetValue(TEntity);
  50. public virtual bool HasScope(SecurityDescriptorScope scope)
  51. {
  52. return true;
  53. }
  54. private string GetLicenseCaption()
  55. {
  56. var license = TEntity
  57. .GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ILicense<>))
  58. ?.GenericTypeArguments.FirstOrDefault();
  59. return license != null ? license.GetCaption() : typeof(CoreLicense).GetCaption();
  60. }
  61. private string GetCode()
  62. {
  63. return string.Format(
  64. "Can{0}{1}{2}",
  65. _action.Prefix,
  66. string.IsNullOrWhiteSpace(_action.Postfix)
  67. ? new Inflector.Inflector(new CultureInfo("en")).Pluralize(TEntity.EntityName().Split('.').Last())
  68. : TEntity.EntityName().Split('.').Last(),
  69. string.IsNullOrWhiteSpace(_action.Postfix)
  70. ? ""
  71. : new Inflector.Inflector(new CultureInfo("en")).Pluralize(_action.Postfix)
  72. );
  73. }
  74. }
  75. public interface IAutoSecurityAction
  76. {
  77. AutoSecurityDescriptorActionType Type { get; }
  78. string Prefix { get; }
  79. string Postfix { get; }
  80. bool GetValue(Type TEntity);
  81. }
  82. public class CanView : IAutoSecurityAction
  83. {
  84. public string Prefix => "View";
  85. public string Postfix => "";
  86. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.View;
  87. public bool GetValue(Type TEntity) => true;
  88. }
  89. public class CanView<T> : AutoSecurityDescriptor<CanView>
  90. {
  91. protected override Type TEntity => typeof(T);
  92. }
  93. public class CanEdit : IAutoSecurityAction
  94. {
  95. public string Prefix => "Edit";
  96. public string Postfix => "";
  97. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Edit;
  98. public bool GetValue(Type TEntity) => TEntity.GetCustomAttribute<AutoEntity>() == null;
  99. }
  100. public class CanEdit<T> : AutoSecurityDescriptor<CanEdit>
  101. {
  102. protected override Type TEntity => typeof(T);
  103. }
  104. public class CanImport : IAutoSecurityAction
  105. {
  106. public string Prefix => "Import";
  107. public string Postfix => "";
  108. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Import;
  109. public bool GetValue(Type TEntity) => TEntity.GetCustomAttribute<AutoEntity>() == null && TEntity.HasInterface<IImportable>();
  110. }
  111. public class CanImport<T> : AutoSecurityDescriptor<CanImport>
  112. {
  113. protected override Type TEntity => typeof(T);
  114. }
  115. public class CanExport : IAutoSecurityAction
  116. {
  117. public string Prefix => "Export";
  118. public string Postfix => "";
  119. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Export;
  120. public bool GetValue(Type TEntity) => false;
  121. }
  122. public class CanExport<T> : AutoSecurityDescriptor<CanExport>
  123. {
  124. protected override Type TEntity => typeof(T);
  125. }
  126. public class CanMerge : IAutoSecurityAction
  127. {
  128. public string Prefix => "Merge";
  129. public string Postfix => "";
  130. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Merge;
  131. public bool GetValue(Type TEntity) => TEntity.GetCustomAttribute<AutoEntity>() == null;
  132. }
  133. public class CanMerge<T> : AutoSecurityDescriptor<CanMerge>
  134. {
  135. protected override Type TEntity => typeof(T);
  136. }
  137. public class CanPost : IAutoSecurityAction
  138. {
  139. public string Prefix => "Post";
  140. public string Postfix => "";
  141. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Post;
  142. public bool GetValue(Type TEntity) => true;
  143. }
  144. public class CanPost<T> : AutoSecurityDescriptor<CanPost>
  145. {
  146. protected override Type TEntity => typeof(T);
  147. }
  148. public class CanConfigurePost : IAutoSecurityAction
  149. {
  150. public string Prefix => "Configure";
  151. public string Postfix => "Post Settings";
  152. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.ConfigurePost;
  153. public bool GetValue(Type TEntity) => true;
  154. }
  155. public class CanConfigurePost<T> : AutoSecurityDescriptor<CanConfigurePost>
  156. {
  157. protected override Type TEntity => typeof(T);
  158. }
  159. public class CanDelete : IAutoSecurityAction
  160. {
  161. public string Prefix => "Delete";
  162. public string Postfix => "";
  163. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Delete;
  164. public bool GetValue(Type TEntity) =>
  165. (TEntity.GetCustomAttribute<AutoEntity>() == null) &&
  166. (TEntity.GetInterfaces().Any(x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(IManyToMany<,>)));
  167. }
  168. public class CanDelete<T> : AutoSecurityDescriptor<CanDelete>
  169. {
  170. protected override Type TEntity => typeof(T);
  171. }
  172. public class CanManageIssues : IAutoSecurityAction
  173. {
  174. public string Prefix => "Manage";
  175. public string Postfix => "Issue";
  176. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.ManageIssues;
  177. public bool GetValue(Type TEntity) => (TEntity.GetCustomAttribute<AutoEntity>() == null);
  178. }
  179. public class CanManageIssues<T> : AutoSecurityDescriptor<CanManageIssues>
  180. {
  181. protected override Type TEntity => typeof(T);
  182. }
  183. public class CanManageProblems : IAutoSecurityAction
  184. {
  185. public string Prefix => "Manage";
  186. public string Postfix => "Problem";
  187. public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.ManageProblems;
  188. public bool GetValue(Type TEntity) => (TEntity.GetCustomAttribute<AutoEntity>() == null);
  189. }
  190. public class CanManageProblems<T> : AutoSecurityDescriptor<CanManageProblems>
  191. {
  192. protected override Type TEntity => typeof(T);
  193. }
  194. }