PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Abp/Authorization/PermissionCheckerExtensions.cs

https://gitlab.com/chenzhuo/abpMirror
C# | 267 lines | 166 code | 22 blank | 79 comment | 13 complexity | b30c2c468269ea9dfdea35500142cbcc MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Abp.Collections.Extensions;
  5. using Abp.Dependency;
  6. using Abp.Localization;
  7. using Abp.Threading;
  8. namespace Abp.Authorization
  9. {
  10. /// <summary>
  11. /// Extension methods for <see cref="IPermissionChecker"/>
  12. /// </summary>
  13. public static class PermissionCheckerExtensions
  14. {
  15. /// <summary>
  16. /// Checks if current user is granted for a permission.
  17. /// </summary>
  18. /// <param name="permissionChecker">Permission checker</param>
  19. /// <param name="permissionName">Name of the permission</param>
  20. public static bool IsGranted(this IPermissionChecker permissionChecker, string permissionName)
  21. {
  22. return AsyncHelper.RunSync(() => permissionChecker.IsGrantedAsync(permissionName));
  23. }
  24. /// <summary>
  25. /// Checks if a user is granted for a permission.
  26. /// </summary>
  27. /// <param name="permissionChecker">Permission checker</param>
  28. /// <param name="user">User to check</param>
  29. /// <param name="permissionName">Name of the permission</param>
  30. public static bool IsGranted(this IPermissionChecker permissionChecker, UserIdentifier user, string permissionName)
  31. {
  32. return AsyncHelper.RunSync(() => permissionChecker.IsGrantedAsync(user, permissionName));
  33. }
  34. /// <summary>
  35. /// Checks if given user is granted for given permission.
  36. /// </summary>
  37. /// <param name="permissionChecker">Permission checker</param>
  38. /// <param name="user">User</param>
  39. /// <param name="requiresAll">True, to require all given permissions are granted. False, to require one or more.</param>
  40. /// <param name="permissionNames">Name of the permissions</param>
  41. public static bool IsGranted(this IPermissionChecker permissionChecker, UserIdentifier user, bool requiresAll, params string[] permissionNames)
  42. {
  43. return AsyncHelper.RunSync(() => IsGrantedAsync(permissionChecker, user, requiresAll, permissionNames));
  44. }
  45. /// <summary>
  46. /// Checks if given user is granted for given permission.
  47. /// </summary>
  48. /// <param name="permissionChecker">Permission checker</param>
  49. /// <param name="user">User</param>
  50. /// <param name="requiresAll">True, to require all given permissions are granted. False, to require one or more.</param>
  51. /// <param name="permissionNames">Name of the permissions</param>
  52. public static async Task<bool> IsGrantedAsync(this IPermissionChecker permissionChecker, UserIdentifier user, bool requiresAll, params string[] permissionNames)
  53. {
  54. if (permissionNames.IsNullOrEmpty())
  55. {
  56. return true;
  57. }
  58. if (requiresAll)
  59. {
  60. foreach (var permissionName in permissionNames)
  61. {
  62. if (!(await permissionChecker.IsGrantedAsync(user, permissionName)))
  63. {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. else
  70. {
  71. foreach (var permissionName in permissionNames)
  72. {
  73. if (await permissionChecker.IsGrantedAsync(user, permissionName))
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. }
  81. /// <summary>
  82. /// Checks if current user is granted for given permission.
  83. /// </summary>
  84. /// <param name="permissionChecker">Permission checker</param>
  85. /// <param name="requiresAll">True, to require all given permissions are granted. False, to require one or more.</param>
  86. /// <param name="permissionNames">Name of the permissions</param>
  87. public static bool IsGranted(this IPermissionChecker permissionChecker, bool requiresAll, params string[] permissionNames)
  88. {
  89. return AsyncHelper.RunSync(() => IsGrantedAsync(permissionChecker, requiresAll, permissionNames));
  90. }
  91. /// <summary>
  92. /// Checks if current user is granted for given permission.
  93. /// </summary>
  94. /// <param name="permissionChecker">Permission checker</param>
  95. /// <param name="requiresAll">True, to require all given permissions are granted. False, to require one or more.</param>
  96. /// <param name="permissionNames">Name of the permissions</param>
  97. public static async Task<bool> IsGrantedAsync(this IPermissionChecker permissionChecker, bool requiresAll, params string[] permissionNames)
  98. {
  99. if (permissionNames.IsNullOrEmpty())
  100. {
  101. return true;
  102. }
  103. if (requiresAll)
  104. {
  105. foreach (var permissionName in permissionNames)
  106. {
  107. if (!(await permissionChecker.IsGrantedAsync(permissionName)))
  108. {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. else
  115. {
  116. foreach (var permissionName in permissionNames)
  117. {
  118. if (await permissionChecker.IsGrantedAsync(permissionName))
  119. {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. }
  126. /// <summary>
  127. /// Authorizes current user for given permission or permissions,
  128. /// throws <see cref="AbpAuthorizationException"/> if not authorized.
  129. /// User it authorized if any of the <see cref="permissionNames"/> are granted.
  130. /// </summary>
  131. /// <param name="permissionChecker">Permission checker</param>
  132. /// <param name="permissionNames">Name of the permissions to authorize</param>
  133. /// <exception cref="AbpAuthorizationException">Throws authorization exception if</exception>
  134. public static void Authorize(this IPermissionChecker permissionChecker, params string[] permissionNames)
  135. {
  136. Authorize(permissionChecker, false, permissionNames);
  137. }
  138. /// <summary>
  139. /// Authorizes current user for given permission or permissions,
  140. /// throws <see cref="AbpAuthorizationException"/> if not authorized.
  141. /// User it authorized if any of the <see cref="permissionNames"/> are granted.
  142. /// </summary>
  143. /// <param name="permissionChecker">Permission checker</param>
  144. /// <param name="requireAll">
  145. /// If this is set to true, all of the <see cref="permissionNames"/> must be granted.
  146. /// If it's false, at least one of the <see cref="permissionNames"/> must be granted.
  147. /// </param>
  148. /// <param name="permissionNames">Name of the permissions to authorize</param>
  149. /// <exception cref="AbpAuthorizationException">Throws authorization exception if</exception>
  150. public static void Authorize(this IPermissionChecker permissionChecker, bool requireAll, params string[] permissionNames)
  151. {
  152. AsyncHelper.RunSync(() => AuthorizeAsync(permissionChecker, requireAll, permissionNames));
  153. }
  154. /// <summary>
  155. /// Authorizes current user for given permission or permissions,
  156. /// throws <see cref="AbpAuthorizationException"/> if not authorized.
  157. /// User it authorized if any of the <see cref="permissionNames"/> are granted.
  158. /// </summary>
  159. /// <param name="permissionChecker">Permission checker</param>
  160. /// <param name="permissionNames">Name of the permissions to authorize</param>
  161. /// <exception cref="AbpAuthorizationException">Throws authorization exception if</exception>
  162. public static Task AuthorizeAsync(this IPermissionChecker permissionChecker, params string[] permissionNames)
  163. {
  164. return AuthorizeAsync(permissionChecker, false, permissionNames);
  165. }
  166. /// <summary>
  167. /// Authorizes current user for given permission or permissions,
  168. /// throws <see cref="AbpAuthorizationException"/> if not authorized.
  169. /// </summary>
  170. /// <param name="permissionChecker">Permission checker</param>
  171. /// <param name="requireAll">
  172. /// If this is set to true, all of the <see cref="permissionNames"/> must be granted.
  173. /// If it's false, at least one of the <see cref="permissionNames"/> must be granted.
  174. /// </param>
  175. /// <param name="permissionNames">Name of the permissions to authorize</param>
  176. /// <exception cref="AbpAuthorizationException">Throws authorization exception if</exception>
  177. public static async Task AuthorizeAsync(this IPermissionChecker permissionChecker, bool requireAll, params string[] permissionNames)
  178. {
  179. if (await IsGrantedAsync(permissionChecker, requireAll, permissionNames))
  180. {
  181. return;
  182. }
  183. var localizedPermissionNames = LocalizePermissionNames(permissionChecker, permissionNames);
  184. if (requireAll)
  185. {
  186. throw new AbpAuthorizationException(
  187. string.Format(
  188. L(
  189. permissionChecker,
  190. "AllOfThesePermissionsMustBeGranted",
  191. "Required permissions are not granted. All of these permissions must be granted: {0}"
  192. ),
  193. string.Join(", ", localizedPermissionNames)
  194. )
  195. );
  196. }
  197. else
  198. {
  199. throw new AbpAuthorizationException(
  200. string.Format(
  201. L(
  202. permissionChecker,
  203. "AtLeastOneOfThesePermissionsMustBeGranted",
  204. "Required permissions are not granted. At least one of these permissions must be granted: {0}"
  205. ),
  206. string.Join(", ", localizedPermissionNames)
  207. )
  208. );
  209. }
  210. }
  211. public static string L(IPermissionChecker permissionChecker, string name, string defaultValue)
  212. {
  213. if (!(permissionChecker is IIocManagerAccessor))
  214. {
  215. return defaultValue;
  216. }
  217. var iocManager = (permissionChecker as IIocManagerAccessor).IocManager;
  218. using (var localizationManager = iocManager.ResolveAsDisposable<ILocalizationManager>())
  219. {
  220. return localizationManager.Object.GetString(AbpConsts.LocalizationSourceName, name);
  221. }
  222. }
  223. public static string[] LocalizePermissionNames(IPermissionChecker permissionChecker, string[] permissionNames)
  224. {
  225. if (!(permissionChecker is IIocManagerAccessor))
  226. {
  227. return permissionNames;
  228. }
  229. var iocManager = (permissionChecker as IIocManagerAccessor).IocManager;
  230. using (var localizationContext = iocManager.ResolveAsDisposable<ILocalizationContext>())
  231. {
  232. using (var permissionManager = iocManager.ResolveAsDisposable<IPermissionManager>())
  233. {
  234. return permissionNames.Select(permissionName =>
  235. {
  236. var permission = permissionManager.Object.GetPermissionOrNull(permissionName);
  237. return permission?.DisplayName == null
  238. ? permissionName
  239. : permission.DisplayName.Localize(localizationContext.Object);
  240. }).ToArray();
  241. }
  242. }
  243. }
  244. }
  245. }