PermissionFinder.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Generic;
  2. using System.Collections.Immutable;
  3. namespace Abp.Authorization
  4. {
  5. /// <summary>
  6. /// This class is used to get permissions out of the system.
  7. /// Normally, you should inject and use <see cref="IPermissionManager"/> and use it.
  8. /// This class can be used in database migrations or in unit tests where Abp is not initialized.
  9. /// </summary>
  10. public static class PermissionFinder
  11. {
  12. /// <summary>
  13. /// Collects and gets all permissions in given providers.
  14. /// This method can be used to get permissions in database migrations or in unit tests where Abp is not initialized.
  15. /// Otherwise, use <see cref="IPermissionManager.GetAllPermissions(bool)"/> method.
  16. ///
  17. /// </summary>
  18. /// <param name="authorizationProviders">Authorization providers</param>
  19. /// <returns>List of permissions</returns>
  20. /// <remarks>
  21. /// This method creates instances of <see cref="authorizationProviders"/> by order and
  22. /// calls <see cref="AuthorizationProvider.SetPermissions"/> to build permission list.
  23. /// So, providers should not use dependency injection.
  24. /// </remarks>
  25. public static IReadOnlyList<Permission> GetAllPermissions(params AuthorizationProvider[] authorizationProviders)
  26. {
  27. return new InternalPermissionFinder(authorizationProviders).GetAllPermissions();
  28. }
  29. internal class InternalPermissionFinder : PermissionDefinitionContextBase
  30. {
  31. public InternalPermissionFinder(params AuthorizationProvider[] authorizationProviders)
  32. {
  33. foreach (var provider in authorizationProviders)
  34. {
  35. provider.SetPermissions(this);
  36. }
  37. Permissions.AddAllPermissions();
  38. }
  39. public IReadOnlyList<Permission> GetAllPermissions()
  40. {
  41. return Permissions.Values.ToImmutableList();
  42. }
  43. }
  44. }
  45. }