| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System.Collections.Generic;
- using System.Collections.Immutable;
- using System.Threading.Tasks;
- using Abp.Application.Features;
- using Abp.Authorization;
- using Abp.Collections.Extensions;
- using Abp.Dependency;
- namespace Abp.Notifications
- {
- /// <summary>
- /// Implements <see cref="INotificationDefinitionManager"/>.
- /// </summary>
- internal class NotificationDefinitionManager : INotificationDefinitionManager, ISingletonDependency
- {
- private readonly INotificationConfiguration _configuration;
- private readonly IocManager _iocManager;
- private readonly IDictionary<string, NotificationDefinition> _notificationDefinitions;
- public NotificationDefinitionManager(
- IocManager iocManager,
- INotificationConfiguration configuration)
- {
- _configuration = configuration;
- _iocManager = iocManager;
- _notificationDefinitions = new Dictionary<string, NotificationDefinition>();
- }
- public void Initialize()
- {
- var context = new NotificationDefinitionContext(this);
- foreach (var providerType in _configuration.Providers)
- {
- using (var provider = _iocManager.ResolveAsDisposable<NotificationProvider>(providerType))
- {
- provider.Object.SetNotifications(context);
- }
- }
- }
- public void Add(NotificationDefinition notificationDefinition)
- {
- if (_notificationDefinitions.ContainsKey(notificationDefinition.Name))
- {
- throw new AbpInitializationException("There is already a notification definition with given name: " + notificationDefinition.Name + ". Notification names must be unique!");
- }
- _notificationDefinitions[notificationDefinition.Name] = notificationDefinition;
- }
- public NotificationDefinition Get(string name)
- {
- var definition = GetOrNull(name);
- if (definition == null)
- {
- throw new AbpException("There is no notification definition with given name: " + name);
- }
- return definition;
- }
- public NotificationDefinition GetOrNull(string name)
- {
- return _notificationDefinitions.GetOrDefault(name);
- }
- public void Remove(string name)
- {
- _notificationDefinitions.Remove(name);
- }
- public IReadOnlyList<NotificationDefinition> GetAll()
- {
- return _notificationDefinitions.Values.ToImmutableList();
- }
- public async Task<bool> IsAvailableAsync(string name, UserIdentifier user)
- {
- var notificationDefinition = GetOrNull(name);
- if (notificationDefinition == null)
- {
- return true;
- }
- if (notificationDefinition.FeatureDependency != null)
- {
- using (var featureDependencyContext = _iocManager.ResolveAsDisposable<FeatureDependencyContext>())
- {
- featureDependencyContext.Object.TenantId = user.TenantId;
- if (!await notificationDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext.Object))
- {
- return false;
- }
- }
- }
- if (notificationDefinition.PermissionDependency != null)
- {
- using (var permissionDependencyContext = _iocManager.ResolveAsDisposable<PermissionDependencyContext>())
- {
- permissionDependencyContext.Object.User = user;
- if (!await notificationDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext.Object))
- {
- return false;
- }
- }
- }
- return true;
- }
- public async Task<IReadOnlyList<NotificationDefinition>> GetAllAvailableAsync(UserIdentifier user)
- {
- var availableDefinitions = new List<NotificationDefinition>();
- using (var permissionDependencyContext = _iocManager.ResolveAsDisposable<PermissionDependencyContext>())
- {
- permissionDependencyContext.Object.User = user;
- using (var featureDependencyContext = _iocManager.ResolveAsDisposable<FeatureDependencyContext>())
- {
- featureDependencyContext.Object.TenantId = user.TenantId;
- foreach (var notificationDefinition in GetAll())
- {
- if (notificationDefinition.PermissionDependency != null &&
- !await notificationDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext.Object))
- {
- continue;
- }
- if (user.TenantId.HasValue &&
- notificationDefinition.FeatureDependency != null &&
- !await notificationDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext.Object))
- {
- continue;
- }
- availableDefinitions.Add(notificationDefinition);
- }
- }
- }
- return availableDefinitions.ToImmutableList();
- }
- }
- }
|