NotificationDefinitionManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections.Generic;
  2. using System.Collections.Immutable;
  3. using System.Threading.Tasks;
  4. using Abp.Application.Features;
  5. using Abp.Authorization;
  6. using Abp.Collections.Extensions;
  7. using Abp.Dependency;
  8. namespace Abp.Notifications
  9. {
  10. /// <summary>
  11. /// Implements <see cref="INotificationDefinitionManager"/>.
  12. /// </summary>
  13. internal class NotificationDefinitionManager : INotificationDefinitionManager, ISingletonDependency
  14. {
  15. private readonly INotificationConfiguration _configuration;
  16. private readonly IocManager _iocManager;
  17. private readonly IDictionary<string, NotificationDefinition> _notificationDefinitions;
  18. public NotificationDefinitionManager(
  19. IocManager iocManager,
  20. INotificationConfiguration configuration)
  21. {
  22. _configuration = configuration;
  23. _iocManager = iocManager;
  24. _notificationDefinitions = new Dictionary<string, NotificationDefinition>();
  25. }
  26. public void Initialize()
  27. {
  28. var context = new NotificationDefinitionContext(this);
  29. foreach (var providerType in _configuration.Providers)
  30. {
  31. using (var provider = _iocManager.ResolveAsDisposable<NotificationProvider>(providerType))
  32. {
  33. provider.Object.SetNotifications(context);
  34. }
  35. }
  36. }
  37. public void Add(NotificationDefinition notificationDefinition)
  38. {
  39. if (_notificationDefinitions.ContainsKey(notificationDefinition.Name))
  40. {
  41. throw new AbpInitializationException("There is already a notification definition with given name: " + notificationDefinition.Name + ". Notification names must be unique!");
  42. }
  43. _notificationDefinitions[notificationDefinition.Name] = notificationDefinition;
  44. }
  45. public NotificationDefinition Get(string name)
  46. {
  47. var definition = GetOrNull(name);
  48. if (definition == null)
  49. {
  50. throw new AbpException("There is no notification definition with given name: " + name);
  51. }
  52. return definition;
  53. }
  54. public NotificationDefinition GetOrNull(string name)
  55. {
  56. return _notificationDefinitions.GetOrDefault(name);
  57. }
  58. public void Remove(string name)
  59. {
  60. _notificationDefinitions.Remove(name);
  61. }
  62. public IReadOnlyList<NotificationDefinition> GetAll()
  63. {
  64. return _notificationDefinitions.Values.ToImmutableList();
  65. }
  66. public async Task<bool> IsAvailableAsync(string name, UserIdentifier user)
  67. {
  68. var notificationDefinition = GetOrNull(name);
  69. if (notificationDefinition == null)
  70. {
  71. return true;
  72. }
  73. if (notificationDefinition.FeatureDependency != null)
  74. {
  75. using (var featureDependencyContext = _iocManager.ResolveAsDisposable<FeatureDependencyContext>())
  76. {
  77. featureDependencyContext.Object.TenantId = user.TenantId;
  78. if (!await notificationDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext.Object))
  79. {
  80. return false;
  81. }
  82. }
  83. }
  84. if (notificationDefinition.PermissionDependency != null)
  85. {
  86. using (var permissionDependencyContext = _iocManager.ResolveAsDisposable<PermissionDependencyContext>())
  87. {
  88. permissionDependencyContext.Object.User = user;
  89. if (!await notificationDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext.Object))
  90. {
  91. return false;
  92. }
  93. }
  94. }
  95. return true;
  96. }
  97. public async Task<IReadOnlyList<NotificationDefinition>> GetAllAvailableAsync(UserIdentifier user)
  98. {
  99. var availableDefinitions = new List<NotificationDefinition>();
  100. using (var permissionDependencyContext = _iocManager.ResolveAsDisposable<PermissionDependencyContext>())
  101. {
  102. permissionDependencyContext.Object.User = user;
  103. using (var featureDependencyContext = _iocManager.ResolveAsDisposable<FeatureDependencyContext>())
  104. {
  105. featureDependencyContext.Object.TenantId = user.TenantId;
  106. foreach (var notificationDefinition in GetAll())
  107. {
  108. if (notificationDefinition.PermissionDependency != null &&
  109. !await notificationDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext.Object))
  110. {
  111. continue;
  112. }
  113. if (user.TenantId.HasValue &&
  114. notificationDefinition.FeatureDependency != null &&
  115. !await notificationDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext.Object))
  116. {
  117. continue;
  118. }
  119. availableDefinitions.Add(notificationDefinition);
  120. }
  121. }
  122. }
  123. return availableDefinitions.ToImmutableList();
  124. }
  125. }
  126. }