FeatureCheckerExtensions.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Threading.Tasks;
  3. using Abp.Authorization;
  4. using Abp.Collections.Extensions;
  5. using Abp.Runtime.Session;
  6. using Abp.Threading;
  7. namespace Abp.Application.Features
  8. {
  9. /// <summary>
  10. /// Some extension methods for <see cref="IFeatureChecker"/>.
  11. /// </summary>
  12. public static class FeatureCheckerExtensions
  13. {
  14. /// <summary>
  15. /// Gets the value of a feature by its name. This is the sync version of <see cref="IFeatureChecker.GetValueAsync(string)"/>
  16. ///
  17. /// This is a shortcut for <see cref="GetValue(IFeatureChecker, int, string)"/> that uses <see cref="IAbpSession.TenantId"/>.
  18. /// Note: This method should be used only if the TenantId can be obtained from the session.
  19. /// </summary>
  20. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  21. /// <param name="featureName">Unique feature name</param>
  22. /// <returns>Feature's current value</returns>
  23. public static string GetValue(this IFeatureChecker featureChecker, string featureName)
  24. {
  25. return AsyncHelper.RunSync(() => featureChecker.GetValueAsync(featureName));
  26. }
  27. /// <summary>
  28. /// Gets the value of a feature by its name. This is the sync version of <see cref="IFeatureChecker.GetValueAsync(int, string)"/>
  29. /// </summary>
  30. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  31. /// <param name="tenantId">Tenant's Id</param>
  32. /// <param name="featureName">Unique feature name</param>
  33. /// <returns>Feature's current value</returns>
  34. public static string GetValue(this IFeatureChecker featureChecker, int tenantId, string featureName)
  35. {
  36. return AsyncHelper.RunSync(() => featureChecker.GetValueAsync(tenantId, featureName));
  37. }
  38. /// <summary>
  39. /// Checks if a given feature is enabled.
  40. /// This should be used for boolean-value features.
  41. ///
  42. /// This is a shortcut for <see cref="IsEnabledAsync(IFeatureChecker, int, string)"/> that uses <see cref="IAbpSession.TenantId"/>.
  43. /// Note: This method should be used only if the TenantId can be obtained from the session.
  44. /// </summary>
  45. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  46. /// <param name="featureName">Unique feature name</param>
  47. /// <returns>True, if the current feature's value is "true".</returns>
  48. public static async Task<bool> IsEnabledAsync(this IFeatureChecker featureChecker, string featureName)
  49. {
  50. return string.Equals(await featureChecker.GetValueAsync(featureName), "true", StringComparison.OrdinalIgnoreCase);
  51. }
  52. /// <summary>
  53. /// Checks if a given feature is enabled.
  54. /// This should be used for boolean-value features.
  55. /// </summary>
  56. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  57. /// <param name="tenantId">Tenant's Id</param>
  58. /// <param name="featureName">Unique feature name</param>
  59. /// <returns>True, if the current feature's value is "true".</returns>
  60. public static async Task<bool> IsEnabledAsync(this IFeatureChecker featureChecker, int tenantId, string featureName)
  61. {
  62. return string.Equals(await featureChecker.GetValueAsync(tenantId, featureName), "true", StringComparison.OrdinalIgnoreCase);
  63. }
  64. /// <summary>
  65. /// Checks if a given feature is enabled.
  66. /// This should be used for boolean-value features.
  67. ///
  68. /// This is a shortcut for <see cref="IsEnabled(IFeatureChecker, int, string)"/> that uses <see cref="IAbpSession.TenantId"/>.
  69. /// Note: This method should be used only if the TenantId can be obtained from the session.
  70. /// </summary>
  71. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  72. /// <param name="name">Unique feature name</param>
  73. /// <returns>True, if the current feature's value is "true".</returns>
  74. public static bool IsEnabled(this IFeatureChecker featureChecker, string name)
  75. {
  76. return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(name));
  77. }
  78. /// <summary>
  79. /// Checks if a given feature is enabled.
  80. /// This should be used for boolean-value features.
  81. /// </summary>
  82. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  83. /// <param name="tenantId">Tenant's Id</param>
  84. /// <param name="featureName">Unique feature name</param>
  85. /// <returns>True, if the current feature's value is "true".</returns>
  86. public static bool IsEnabled(this IFeatureChecker featureChecker, int tenantId, string featureName)
  87. {
  88. return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(tenantId, featureName));
  89. }
  90. /// <summary>
  91. /// Used to check if one or all of the given features are enabled.
  92. /// </summary>
  93. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  94. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  95. /// <param name="featureNames">Names of the features</param>
  96. public static async Task<bool> IsEnabledAsync(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
  97. {
  98. if (featureNames.IsNullOrEmpty())
  99. {
  100. return true;
  101. }
  102. if (requiresAll)
  103. {
  104. foreach (var featureName in featureNames)
  105. {
  106. if (!(await featureChecker.IsEnabledAsync(featureName)))
  107. {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. foreach (var featureName in featureNames)
  114. {
  115. if (await featureChecker.IsEnabledAsync(featureName))
  116. {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /// <summary>
  123. /// Used to check if one or all of the given features are enabled.
  124. /// </summary>
  125. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  126. /// <param name="tenantId">Tenant id</param>
  127. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  128. /// <param name="featureNames">Names of the features</param>
  129. public static async Task<bool> IsEnabledAsync(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
  130. {
  131. if (featureNames.IsNullOrEmpty())
  132. {
  133. return true;
  134. }
  135. if (requiresAll)
  136. {
  137. foreach (var featureName in featureNames)
  138. {
  139. if (!(await featureChecker.IsEnabledAsync(tenantId, featureName)))
  140. {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. foreach (var featureName in featureNames)
  147. {
  148. if (await featureChecker.IsEnabledAsync(tenantId, featureName))
  149. {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. /// <summary>
  156. /// Used to check if one or all of the given features are enabled.
  157. /// </summary>
  158. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  159. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  160. /// <param name="featureNames">Names of the features</param>
  161. public static bool IsEnabled(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
  162. {
  163. return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(requiresAll, featureNames));
  164. }
  165. /// <summary>
  166. /// Used to check if one or all of the given features are enabled.
  167. /// </summary>
  168. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  169. /// <param name="tenantId">Tenant id</param>
  170. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  171. /// <param name="featureNames">Names of the features</param>
  172. public static bool IsEnabled(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
  173. {
  174. return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(tenantId, requiresAll, featureNames));
  175. }
  176. /// <summary>
  177. /// Checks if a given feature is enabled. Throws <see cref="AbpAuthorizationException"/> if not.
  178. /// </summary>
  179. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  180. /// <param name="featureName">Unique feature name</param>
  181. public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, string featureName)
  182. {
  183. if (!(await featureChecker.IsEnabledAsync(featureName)))
  184. {
  185. throw new AbpAuthorizationException("Feature is not enabled: " + featureName);
  186. }
  187. }
  188. /// <summary>
  189. /// Checks if a given feature is enabled. Throws <see cref="AbpAuthorizationException"/> if not.
  190. /// </summary>
  191. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  192. /// <param name="featureName">Unique feature name</param>
  193. public static void CheckEnabled(this IFeatureChecker featureChecker, string featureName)
  194. {
  195. if (!featureChecker.IsEnabled(featureName))
  196. {
  197. throw new AbpAuthorizationException("Feature is not enabled: " + featureName);
  198. }
  199. }
  200. /// <summary>
  201. /// Checks if one or all of the given features are enabled. Throws <see cref="AbpAuthorizationException"/> if not.
  202. /// </summary>
  203. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  204. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  205. /// <param name="featureNames">Names of the features</param>
  206. public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
  207. {
  208. if (featureNames.IsNullOrEmpty())
  209. {
  210. return;
  211. }
  212. if (requiresAll)
  213. {
  214. foreach (var featureName in featureNames)
  215. {
  216. if (!(await featureChecker.IsEnabledAsync(featureName)))
  217. {
  218. throw new AbpAuthorizationException(
  219. "Required features are not enabled. All of these features must be enabled: " +
  220. string.Join(", ", featureNames)
  221. );
  222. }
  223. }
  224. }
  225. else
  226. {
  227. foreach (var featureName in featureNames)
  228. {
  229. if (await featureChecker.IsEnabledAsync(featureName))
  230. {
  231. return;
  232. }
  233. }
  234. throw new AbpAuthorizationException(
  235. "Required features are not enabled. At least one of these features must be enabled: " +
  236. string.Join(", ", featureNames)
  237. );
  238. }
  239. }
  240. /// <summary>
  241. /// Checks if one or all of the given features are enabled. Throws <see cref="AbpAuthorizationException"/> if not.
  242. /// </summary>
  243. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  244. /// <param name="tenantId">Tenant id</param>
  245. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  246. /// <param name="featureNames">Names of the features</param>
  247. public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
  248. {
  249. if (featureNames.IsNullOrEmpty())
  250. {
  251. return;
  252. }
  253. if (requiresAll)
  254. {
  255. foreach (var featureName in featureNames)
  256. {
  257. if (!(await featureChecker.IsEnabledAsync(tenantId, featureName)))
  258. {
  259. throw new AbpAuthorizationException(
  260. "Required features are not enabled. All of these features must be enabled: " +
  261. string.Join(", ", featureNames)
  262. );
  263. }
  264. }
  265. }
  266. else
  267. {
  268. foreach (var featureName in featureNames)
  269. {
  270. if (await featureChecker.IsEnabledAsync(tenantId, featureName))
  271. {
  272. return;
  273. }
  274. }
  275. throw new AbpAuthorizationException(
  276. "Required features are not enabled. At least one of these features must be enabled: " +
  277. string.Join(", ", featureNames)
  278. );
  279. }
  280. }
  281. /// <summary>
  282. /// Checks if one or all of the given features are enabled. Throws <see cref="AbpAuthorizationException"/> if not.
  283. /// </summary>
  284. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  285. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  286. /// <param name="featureNames">Names of the features</param>
  287. public static void CheckEnabled(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
  288. {
  289. AsyncHelper.RunSync(() => featureChecker.CheckEnabledAsync(requiresAll, featureNames));
  290. }
  291. /// <summary>
  292. /// Checks if one or all of the given features are enabled. Throws <see cref="AbpAuthorizationException"/> if not.
  293. /// </summary>
  294. /// <param name="featureChecker"><see cref="IFeatureChecker"/> instance</param>
  295. /// <param name="tenantId">Tenant id</param>
  296. /// <param name="requiresAll">True, to require that all the given features are enabled. False, to require one or more.</param>
  297. /// <param name="featureNames">Names of the features</param>
  298. public static void CheckEnabled(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
  299. {
  300. AsyncHelper.RunSync(() => featureChecker.CheckEnabledAsync(tenantId, requiresAll, featureNames));
  301. }
  302. }
  303. }