diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs index 768eacd41..84798d8d2 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/AbpWebhooksModule.cs @@ -17,7 +17,7 @@ namespace LINGYUN.Abp.Webhooks; [DependsOn(typeof(AbpHttpClientModule))] public class AbpWebhooksModule : AbpModule { - internal const string WebhooksClient = "Abp.Webhooks.HttpClient"; + internal const string WebhooksClient = "__Abp_Webhooks_HttpClient"; public override void PreConfigureServices(ServiceConfigurationContext context) { diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs index d400800bf..19982a68e 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebHooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using Microsoft.Extensions.Options; using System; using System.Net; using System.Net.Http; @@ -13,18 +12,15 @@ namespace LINGYUN.Abp.Webhooks { public ILogger Logger { protected get; set; } - private readonly AbpWebhooksOptions _options; private readonly IWebhookManager _webhookManager; private readonly IHttpClientFactory _httpClientFactory; private const string FailedRequestDefaultContent = "Webhook Send Request Failed"; public DefaultWebhookSender( - IOptions options, IWebhookManager webhookManager, IHttpClientFactory httpClientFactory) { - _options = options.Value; _webhookManager = webhookManager; _httpClientFactory = httpClientFactory; diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs index 3570b8a63..3af3d870a 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissionDefinitionProvider.cs @@ -1,6 +1,7 @@ using LINGYUN.Abp.WebhooksManagement.Localization; using Volo.Abp.Authorization.Permissions; using Volo.Abp.Localization; +using Volo.Abp.MultiTenancy; namespace LINGYUN.Abp.WebhooksManagement.Authorization; @@ -10,6 +11,27 @@ public class WebhooksManagementPermissionDefinitionProvider : PermissionDefiniti { var group = context.AddGroup(WebhooksManagementPermissions.GroupName, L("Permission:WebhooksManagement")); + var subscription = group.AddPermission( + WebhooksManagementPermissions.WebhookSubscription.Default, + L("Permission:Subscriptions"), + MultiTenancySides.Host) + .WithProviders(ClientPermissionValueProvider.ProviderName); + subscription.AddChild( + WebhooksManagementPermissions.WebhookSubscription.Create, + L("Permission:Create"), + MultiTenancySides.Host) + .WithProviders(ClientPermissionValueProvider.ProviderName); + subscription.AddChild( + WebhooksManagementPermissions.WebhookSubscription.Update, + L("Permission:Update"), + MultiTenancySides.Host) + .WithProviders(ClientPermissionValueProvider.ProviderName); + subscription.AddChild( + WebhooksManagementPermissions.WebhookSubscription.Delete, + L("Permission:Delete"), + MultiTenancySides.Host) + .WithProviders(ClientPermissionValueProvider.ProviderName); + group.AddPermission( WebhooksManagementPermissions.Publish, L("Permission:Publish")) diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissions.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissions.cs index 307d94b49..bdabbfa8c 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissions.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/Authorization/WebhooksManagementPermissions.cs @@ -2,7 +2,7 @@ public static class WebhooksManagementPermissions { - public const string GroupName = "WebhooksManagement"; + public const string GroupName = "AbpWebhooks"; /// /// 授权允许发布Webhooks事件, 建议客户端授权 @@ -10,4 +10,12 @@ public static class WebhooksManagementPermissions public const string Publish = GroupName + ".Publish"; public const string ManageSettings = GroupName + ".ManageSettings"; + + public static class WebhookSubscription + { + public const string Default = GroupName + ".Subscriptions"; + public const string Create = Default + ".Create"; + public const string Update = Default + ".Update"; + public const string Delete = Default + ".Delete"; + } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs new file mode 100644 index 000000000..f25ede15b --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs @@ -0,0 +1,14 @@ +using System; +using Volo.Abp.Application.Services; + +namespace LINGYUN.Abp.WebhooksManagement; + +public interface IWebhookSubscriptionAppService : + ICrudAppService< + WebhookSubscriptionDto, + Guid, + WebhookSubscriptionGetListInput, + WebhookSubscriptionCreateInput, + WebhookSubscriptionUpdateInput> +{ +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionCreateOrUpdateInput.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionCreateOrUpdateInput.cs new file mode 100644 index 000000000..94897417d --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionCreateOrUpdateInput.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using Volo.Abp.Validation; + +namespace LINGYUN.Abp.WebhooksManagement; + +public class WebhookSubscriptionCreateInput : WebhookSubscriptionCreateOrUpdateInput +{ + +} + +public class WebhookSubscriptionUpdateInput : WebhookSubscriptionCreateOrUpdateInput +{ + +} + +public abstract class WebhookSubscriptionCreateOrUpdateInput +{ + [Required] + [DynamicStringLength(typeof(WebhookSubscriptionConsts), nameof(WebhookSubscriptionConsts.MaxWebhookUriLength))] + public string WebhookUri { get; set; } + + [Required] + [DynamicStringLength(typeof(WebhookSubscriptionConsts), nameof(WebhookSubscriptionConsts.MaxSecretLength))] + public string Secret { get; set; } + + public bool IsActive { get; set; } + + public List Webhooks { get; set; } = new List(); + + public Dictionary Headers { get; set; } = new Dictionary(); +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionDto.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionDto.cs new file mode 100644 index 000000000..204ffd2f9 --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionDto.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.Application.Dtos; + +namespace LINGYUN.Abp.WebhooksManagement; + +public class WebhookSubscriptionDto : CreationAuditedEntityDto +{ + public Guid? TenantId { get; set; } + public string WebhookUri { get; set; } + public string Secret { get; set; } + public bool IsActive { get; set; } + public List Webhooks { get; set; } = new List(); + public IDictionary Headers { get; set; } = new Dictionary(); +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionGetListInput.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionGetListInput.cs new file mode 100644 index 000000000..9e9060184 --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionGetListInput.cs @@ -0,0 +1,26 @@ +using System; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Validation; + +namespace LINGYUN.Abp.WebhooksManagement; + +public class WebhookSubscriptionGetListInput : PagedAndSortedResultRequestDto +{ + public string Filter { get; set; } + + public Guid? TenantId { get; set; } + + [DynamicStringLength(typeof(WebhookSubscriptionConsts), nameof(WebhookSubscriptionConsts.MaxWebhookUriLength))] + public string WebhookUri { get; set; } + + [DynamicStringLength(typeof(WebhookSubscriptionConsts), nameof(WebhookSubscriptionConsts.MaxSecretLength))] + public string Secret { get; set; } + + public bool? IsActive { get; set; } + + public string Webhooks { get; set; } + + public DateTime? BeginCreationTime { get; set; } + + public DateTime? EndCreationTime { get; set; } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/Extensions/WebhookSubscriptionExtensions.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/Extensions/WebhookSubscriptionExtensions.cs new file mode 100644 index 000000000..49c190f0a --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/Extensions/WebhookSubscriptionExtensions.cs @@ -0,0 +1,44 @@ +using Newtonsoft.Json; +using System.Linq; + +namespace LINGYUN.Abp.WebhooksManagement.Extensions +{ + public static class WebhookSubscriptionExtensions + { + public static WebhookSubscriptionDto ToWebhookSubscriptionDto(this WebhookSubscription webhookSubscription) + { + return new WebhookSubscriptionDto + { + Id = webhookSubscription.Id, + TenantId = webhookSubscription.TenantId, + IsActive = webhookSubscription.IsActive, + Secret = webhookSubscription.Secret, + WebhookUri = webhookSubscription.WebhookUri, + Webhooks = webhookSubscription.GetSubscribedWebhooks(), + Headers = webhookSubscription.GetWebhookHeaders(), + CreationTime = webhookSubscription.CreationTime, + CreatorId = webhookSubscription.CreatorId + }; + } + + public static string ToSubscribedWebhooksString(this WebhookSubscriptionUpdateInput webhookSubscription) + { + if (webhookSubscription.Webhooks.Any()) + { + return JsonConvert.SerializeObject(webhookSubscription.Webhooks); + } + + return null; + } + + public static string ToWebhookHeadersString(this WebhookSubscriptionUpdateInput webhookSubscription) + { + if (webhookSubscription.Headers.Any()) + { + return JsonConvert.SerializeObject(webhookSubscription.Headers); + } + + return null; + } + } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs new file mode 100644 index 000000000..7f984fc4d --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs @@ -0,0 +1,111 @@ +using LINGYUN.Abp.WebhooksManagement.Authorization; +using LINGYUN.Abp.WebhooksManagement.Extensions; +using Microsoft.AspNetCore.Authorization; +using Newtonsoft.Json; +using System; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; + +namespace LINGYUN.Abp.WebhooksManagement; + +[Authorize(WebhooksManagementPermissions.WebhookSubscription.Default)] +public class WebhookSubscriptionAppService : WebhooksManagementAppServiceBase, IWebhookSubscriptionAppService +{ + protected IWebhookSubscriptionRepository SubscriptionRepository { get; } + + public WebhookSubscriptionAppService( + IWebhookSubscriptionRepository subscriptionRepository) + { + SubscriptionRepository = subscriptionRepository; + } + + [Authorize(WebhooksManagementPermissions.WebhookSubscription.Create)] + public async virtual Task CreateAsync(WebhookSubscriptionCreateInput input) + { + await CheckSubscribedAsync(input); + + var subscription = new WebhookSubscription( + GuidGenerator.Create(), + input.WebhookUri, + input.Secret, + JsonConvert.SerializeObject(input.Webhooks), + JsonConvert.SerializeObject(input.Headers), + CurrentTenant.Id); + + await SubscriptionRepository.InsertAsync(subscription); + + await CurrentUnitOfWork.SaveChangesAsync(); + + return subscription.ToWebhookSubscriptionDto(); + } + + [Authorize(WebhooksManagementPermissions.WebhookSubscription.Delete)] + public virtual Task DeleteAsync(Guid id) + { + return SubscriptionRepository.DeleteAsync(id); + } + + public async virtual Task GetAsync(Guid id) + { + var subscription = await SubscriptionRepository.GetAsync(id); + + return subscription.ToWebhookSubscriptionDto(); + } + + public async virtual Task> GetListAsync(WebhookSubscriptionGetListInput input) + { + var filter = new WebhookSubscriptionFilter + { + Filter = input.Filter, + BeginCreationTime = input.BeginCreationTime, + EndCreationTime = input.EndCreationTime, + IsActive = input.IsActive, + Secret = input.Secret, + TenantId = input.TenantId, + Webhooks = input.Webhooks, + WebhookUri = input.WebhookUri + }; + + var totalCount = await SubscriptionRepository.GetCountAsync(filter); + var subscriptions = await SubscriptionRepository.GetListAsync(filter, + input.Sorting, input.MaxResultCount, input.SkipCount); + + return new PagedResultDto(totalCount, + subscriptions.Select(subscription => subscription.ToWebhookSubscriptionDto()).ToList()); + } + + [Authorize(WebhooksManagementPermissions.WebhookSubscription.Update)] + public async virtual Task UpdateAsync(Guid id, WebhookSubscriptionUpdateInput input) + { + var subscription = await SubscriptionRepository.GetAsync(id); + if (!string.Equals(subscription.WebhookUri, input.WebhookUri)) + { + await CheckSubscribedAsync(input); + } + + subscription.SetWebhookUri(input.WebhookUri); + subscription.SetWebhooks(input.ToSubscribedWebhooksString()); + subscription.SetHeaders(input.ToWebhookHeadersString()); + + await SubscriptionRepository.UpdateAsync(subscription); + + await CurrentUnitOfWork.SaveChangesAsync(); + + return subscription.ToWebhookSubscriptionDto(); + } + + protected async virtual Task CheckSubscribedAsync(WebhookSubscriptionCreateOrUpdateInput input) + { + foreach (var webhookName in input.Webhooks) + { + if (await SubscriptionRepository.IsSubscribedAsync(CurrentTenant.Id, input.WebhookUri, webhookName)) + { + throw new BusinessException(WebhooksManagementErrorCodes.WebhookSubscription.DuplicateSubscribed) + .WithData(nameof(WebhookSubscription.WebhookUri), input.WebhookUri) + .WithData(nameof(WebhookSubscription.Webhooks), webhookName); + } + } + } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/en.json b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/en.json index 5e4cfe7f2..10f87c775 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/en.json +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/en.json @@ -5,6 +5,7 @@ "Permission:WebhooksManagement": "Webhooks", "Permission:ManageSettings": "Manage Settings", "DisplayName:CheckConnect": "Check Connect", - "Description:CheckConnect": "When a third-party service is connected, it is used to check whether the communication is normal." + "Description:CheckConnect": "When a third-party service is connected, it is used to check whether the communication is normal.", + "Webhooks:010001": "Payload address {WebhookUri} has been mounted event {Webhooks}!" } } \ No newline at end of file diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/zh-Hans.json b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/zh-Hans.json index bd1941dd4..0e89e364a 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/zh-Hans.json +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/Localization/Resources/zh-Hans.json @@ -5,6 +5,7 @@ "Permission:WebhooksManagement": "Webhooks", "Permission:ManageSettings": "管理设置", "DisplayName:CheckConnect": "检查连接", - "Description:CheckConnect": "第三方服务接入时,用于检查是否通讯正常." + "Description:CheckConnect": "第三方服务接入时,用于检查是否通讯正常.", + "Webhooks:010001": "载荷地址 {WebhookUri} 已经挂载事件 {Webhooks}!" } } \ No newline at end of file diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhooksManagementErrorCodes.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhooksManagementErrorCodes.cs index a7d2af4f9..5c13db31c 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhooksManagementErrorCodes.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain.Shared/LINGYUN/Abp/WebhooksManagement/WebhooksManagementErrorCodes.cs @@ -2,5 +2,12 @@ public static class WebhooksManagementErrorCodes { - public const string Namespace = "WebhooksManagement"; + public const string Namespace = "Webhooks"; + + public static class WebhookSubscription + { + public const string Prefix = Namespace + ":010"; + + public const string DuplicateSubscribed = Prefix + "001"; + } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionRepository.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionRepository.cs index faf4dbbee..069e8375c 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionRepository.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionRepository.cs @@ -1,8 +1,27 @@ using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; namespace LINGYUN.Abp.WebhooksManagement; public interface IWebhookSubscriptionRepository : IRepository { + Task IsSubscribedAsync( + Guid? tenantId, + string webhookUri, + string webhookName, + CancellationToken cancellationToken = default); + + Task GetCountAsync( + WebhookSubscriptionFilter filter, + CancellationToken cancellationToken = default); + + Task> GetListAsync( + WebhookSubscriptionFilter filter, + string sorting = $"{nameof(WebhookSubscription.CreationTime)} DESC", + int maxResultCount = 10, + int skipCount = 0, + CancellationToken cancellationToken = default); } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionFilter.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionFilter.cs new file mode 100644 index 000000000..adbd92c7d --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionFilter.cs @@ -0,0 +1,22 @@ +using System; + +namespace LINGYUN.Abp.WebhooksManagement; + +public class WebhookSubscriptionFilter +{ + public string Filter { get; set; } + + public Guid? TenantId { get; set; } + + public string WebhookUri { get; set; } + + public string Secret { get; set; } + + public bool? IsActive { get; set; } + + public string Webhooks { get; set; } + + public DateTime? BeginCreationTime { get; set; } + + public DateTime? EndCreationTime { get; set; } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/EfCoreWebhookSubscriptionRepository.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/EfCoreWebhookSubscriptionRepository.cs index 4931d6eaf..035fc0591 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/EfCoreWebhookSubscriptionRepository.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.EntityFrameworkCore/LINGYUN/Abp/WebhooksManagement/EntityFrameworkCore/EfCoreWebhookSubscriptionRepository.cs @@ -1,4 +1,10 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; +using System.Threading; +using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; @@ -13,4 +19,54 @@ public class EfCoreWebhookSubscriptionRepository : : base(dbContextProvider) { } + + public async virtual Task IsSubscribedAsync( + Guid? tenantId, + string webhookUri, + string webhookName, + CancellationToken cancellationToken = default) + { + return await (await GetDbSetAsync()) + .AnyAsync(x => x.TenantId == tenantId && + x.WebhookUri == webhookUri && + x.Webhooks.Contains("\"" + webhookName + "\""), + GetCancellationToken(cancellationToken)); + } + + public async virtual Task GetCountAsync( + WebhookSubscriptionFilter filter, + CancellationToken cancellationToken = default) + { + return await ApplyFilter(await GetDbSetAsync(), filter) + .CountAsync(GetCancellationToken(cancellationToken)); + } + + public async virtual Task> GetListAsync( + WebhookSubscriptionFilter filter, + string sorting = $"{nameof(WebhookSubscription.CreationTime)} DESC", + int maxResultCount = 10, + int skipCount = 0, + CancellationToken cancellationToken = default) + { + return await ApplyFilter(await GetDbSetAsync(), filter) + .OrderBy(sorting ?? $"{nameof(WebhookSubscription.CreationTime)} DESC") + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + protected virtual IQueryable ApplyFilter( + IQueryable queryable, + WebhookSubscriptionFilter filter) + { + return queryable + .WhereIf(filter.TenantId.HasValue, x => x.TenantId == filter.TenantId) + .WhereIf(filter.IsActive.HasValue, x => x.IsActive == filter.IsActive) + .WhereIf(!filter.WebhookUri.IsNullOrWhiteSpace(), x => x.WebhookUri == filter.WebhookUri) + .WhereIf(!filter.Secret.IsNullOrWhiteSpace(), x => x.Secret == filter.Secret) + .WhereIf(!filter.Webhooks.IsNullOrWhiteSpace(), x => x.Webhooks.Contains("\"" + filter.Webhooks + "\"")) + .WhereIf(filter.BeginCreationTime.HasValue, x => x.CreationTime.CompareTo(filter.BeginCreationTime) >= 0) + .WhereIf(filter.EndCreationTime.HasValue, x => x.CreationTime.CompareTo(filter.EndCreationTime) <= 0) + .WhereIf(!filter.Filter.IsNullOrWhiteSpace(), x => x.WebhookUri.Contains(filter.Filter) || + x.Secret.Contains(filter.Filter) || x.Webhooks.Contains(filter.Filter)); + } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs new file mode 100644 index 000000000..5b7d91e6c --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs @@ -0,0 +1,59 @@ +using LINGYUN.Abp.WebhooksManagement.Authorization; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Dtos; + +namespace LINGYUN.Abp.WebhooksManagement; + +[RemoteService(Name = WebhooksManagementRemoteServiceConsts.RemoteServiceName)] +[Area(WebhooksManagementRemoteServiceConsts.ModuleName)] +[Authorize(WebhooksManagementPermissions.WebhookSubscription.Default)] +[Route("api/webhooks/subscriptions")] +public class WebhookSubscriptionController : WebhooksManagementControllerBase, IWebhookSubscriptionAppService +{ + protected IWebhookSubscriptionAppService SubscriptionAppService { get; } + + public WebhookSubscriptionController(IWebhookSubscriptionAppService subscriptionAppService) + { + SubscriptionAppService = subscriptionAppService; + } + + [HttpPost] + [Authorize(WebhooksManagementPermissions.WebhookSubscription.Create)] + public Task CreateAsync(WebhookSubscriptionCreateInput input) + { + return SubscriptionAppService.CreateAsync(input); + } + + [HttpDelete] + [Route("{id}")] + [Authorize(WebhooksManagementPermissions.WebhookSubscription.Delete)] + public Task DeleteAsync(Guid id) + { + return SubscriptionAppService.DeleteAsync(id); + } + + [HttpGet] + [Route("{id}")] + public Task GetAsync(Guid id) + { + return SubscriptionAppService.GetAsync(id); + } + + [HttpGet] + public Task> GetListAsync(WebhookSubscriptionGetListInput input) + { + return SubscriptionAppService.GetListAsync(input); + } + + [HttpPut] + [Route("{id}")] + [Authorize(WebhooksManagementPermissions.WebhookSubscription.Update)] + public Task UpdateAsync(Guid id, WebhookSubscriptionUpdateInput input) + { + return SubscriptionAppService.UpdateAsync(id, input); + } +}