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
{
///
/// Implements .
///
internal class NotificationDefinitionManager : INotificationDefinitionManager, ISingletonDependency
{
private readonly INotificationConfiguration _configuration;
private readonly IocManager _iocManager;
private readonly IDictionary _notificationDefinitions;
public NotificationDefinitionManager(
IocManager iocManager,
INotificationConfiguration configuration)
{
_configuration = configuration;
_iocManager = iocManager;
_notificationDefinitions = new Dictionary();
}
public void Initialize()
{
var context = new NotificationDefinitionContext(this);
foreach (var providerType in _configuration.Providers)
{
using (var provider = _iocManager.ResolveAsDisposable(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 GetAll()
{
return _notificationDefinitions.Values.ToImmutableList();
}
public async Task IsAvailableAsync(string name, UserIdentifier user)
{
var notificationDefinition = GetOrNull(name);
if (notificationDefinition == null)
{
return true;
}
if (notificationDefinition.FeatureDependency != null)
{
using (var featureDependencyContext = _iocManager.ResolveAsDisposable())
{
featureDependencyContext.Object.TenantId = user.TenantId;
if (!await notificationDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext.Object))
{
return false;
}
}
}
if (notificationDefinition.PermissionDependency != null)
{
using (var permissionDependencyContext = _iocManager.ResolveAsDisposable())
{
permissionDependencyContext.Object.User = user;
if (!await notificationDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext.Object))
{
return false;
}
}
}
return true;
}
public async Task> GetAllAvailableAsync(UserIdentifier user)
{
var availableDefinitions = new List();
using (var permissionDependencyContext = _iocManager.ResolveAsDisposable())
{
permissionDependencyContext.Object.User = user;
using (var featureDependencyContext = _iocManager.ResolveAsDisposable())
{
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();
}
}
}