2022-03-15 18:00:53 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
|
// any third-party rights.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
|
|
|
|
|
namespace ASC.Webhooks.Core;
|
2022-02-03 13:43:37 +00:00
|
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
|
public class DbWorker
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IDbContextFactory<WebhooksDbContext> _dbContextFactory;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
private readonly TenantManager _tenantManager;
|
|
|
|
|
private readonly AuthContext _authContext;
|
|
|
|
|
|
|
|
|
|
private int Tenant
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _tenantManager.GetCurrentTenant().Id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DbWorker(IDbContextFactory<WebhooksDbContext> dbContextFactory, TenantManager tenantManager, AuthContext authContext)
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
|
|
|
|
_dbContextFactory = dbContextFactory;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
_tenantManager = tenantManager;
|
|
|
|
|
_authContext = authContext;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 13:41:53 +00:00
|
|
|
|
public async Task<WebhooksConfig> AddWebhookConfig(string name, string uri, string secretKey)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2022-08-18 09:03:13 +00:00
|
|
|
|
|
2022-08-18 13:41:53 +00:00
|
|
|
|
var toAdd = new WebhooksConfig { TenantId = Tenant, Uri = uri, SecretKey = secretKey, Name = name };
|
|
|
|
|
toAdd = await webhooksDbContext.AddOrUpdateAsync(r => r.WebhooksConfigs, toAdd);
|
2022-08-18 09:03:13 +00:00
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return toAdd;
|
2022-02-03 13:43:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 09:34:48 +00:00
|
|
|
|
public async IAsyncEnumerable<WebhooksConfig> GetTenantWebhooks()
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2022-02-03 13:43:37 +00:00
|
|
|
|
|
2022-08-18 09:34:48 +00:00
|
|
|
|
var q = webhooksDbContext.WebhooksConfigs
|
2022-08-18 09:03:13 +00:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(it => it.TenantId == Tenant)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
.AsAsyncEnumerable();
|
|
|
|
|
|
|
|
|
|
await foreach (var webhook in q)
|
|
|
|
|
{
|
|
|
|
|
yield return webhook;
|
2022-03-17 15:01:39 +00:00
|
|
|
|
}
|
2022-02-03 13:43:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
public IAsyncEnumerable<WebhooksConfig> GetWebhookConfigs()
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2022-08-17 09:18:50 +00:00
|
|
|
|
var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
|
|
|
|
return webhooksDbContext.WebhooksConfigs
|
|
|
|
|
.Where(t => t.TenantId == Tenant)
|
|
|
|
|
.AsAsyncEnumerable();
|
2022-02-11 14:46:49 +00:00
|
|
|
|
}
|
2022-08-17 09:18:50 +00:00
|
|
|
|
|
2022-08-18 13:41:53 +00:00
|
|
|
|
public async Task<WebhooksConfig> UpdateWebhookConfig(int id, string name, string uri, string key, bool? enabled)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
|
|
|
|
var updateObj = await webhooksDbContext.WebhooksConfigs
|
2022-08-18 13:41:53 +00:00
|
|
|
|
.Where(it => it.TenantId == Tenant && it.Id == id)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
if (updateObj != null)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(uri))
|
|
|
|
|
{
|
|
|
|
|
updateObj.Uri = uri;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 13:41:53 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
updateObj.Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
|
|
|
|
{
|
|
|
|
|
updateObj.SecretKey = key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
if (enabled.HasValue)
|
|
|
|
|
{
|
|
|
|
|
updateObj.Enabled = enabled.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
webhooksDbContext.WebhooksConfigs.Update(updateObj);
|
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
2022-08-18 09:03:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateObj;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
2022-02-11 14:46:49 +00:00
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
public async Task<WebhooksConfig> RemoveWebhookConfig(int id)
|
2022-02-11 14:46:49 +00:00
|
|
|
|
{
|
2022-07-28 18:00:49 +00:00
|
|
|
|
var tenant = _tenantManager.GetCurrentTenant().Id;
|
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
|
|
|
|
var removeObj = await webhooksDbContext.WebhooksConfigs
|
2022-08-18 13:41:53 +00:00
|
|
|
|
.Where(it => it.TenantId == tenant && it.Id == id)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
.FirstOrDefaultAsync();
|
2022-02-11 14:46:49 +00:00
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
webhooksDbContext.WebhooksConfigs.Remove(removeObj);
|
2022-08-18 09:03:13 +00:00
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return removeObj;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-22 08:00:55 +00:00
|
|
|
|
public IAsyncEnumerable<WebhooksLog> ReadJournal(int startIndex, int limit, DateTime? delivery, string hookname, string route)
|
2022-08-18 09:34:48 +00:00
|
|
|
|
{
|
|
|
|
|
var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
2022-08-22 08:00:55 +00:00
|
|
|
|
var q = webhooksDbContext.WebhooksLogs
|
2022-08-18 13:41:53 +00:00
|
|
|
|
.AsNoTracking()
|
2022-08-22 08:00:55 +00:00
|
|
|
|
.Where(r => r.TenantId == Tenant);
|
|
|
|
|
|
|
|
|
|
if (delivery.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var date = delivery.Value;
|
|
|
|
|
q = q.Where(r => r.Delivery == date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(hookname))
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Config.Name == hookname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(route))
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Route == route);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (startIndex != 0)
|
|
|
|
|
{
|
|
|
|
|
q = q.Skip(startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (limit != 0)
|
|
|
|
|
{
|
|
|
|
|
q = q.Take(limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return q.OrderByDescending(t => t.Id).AsAsyncEnumerable();
|
2022-08-18 09:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 19:06:51 +00:00
|
|
|
|
public async Task<WebhooksLog> ReadJournal(int id)
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2022-08-18 09:03:13 +00:00
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
|
|
|
|
return await webhooksDbContext.WebhooksLogs
|
|
|
|
|
.AsNoTracking()
|
2022-08-18 19:06:51 +00:00
|
|
|
|
.Where(it => it.Id == id)
|
2022-08-18 09:03:13 +00:00
|
|
|
|
.FirstOrDefaultAsync();
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
2022-07-28 18:00:49 +00:00
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
public async Task<WebhooksLog> WriteToJournal(WebhooksLog webhook)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
webhook.TenantId = _tenantManager.GetCurrentTenant().Id;
|
|
|
|
|
webhook.Uid = _authContext.CurrentAccount.ID;
|
2022-07-28 18:00:49 +00:00
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2022-02-03 13:43:37 +00:00
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
var entity = await webhooksDbContext.WebhooksLogs.AddAsync(webhook);
|
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
2022-02-03 13:43:37 +00:00
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
return entity.Entity;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
public async Task<WebhooksLog> UpdateWebhookJournal(int id, int status, DateTime delivery, string requestHeaders, string responsePayload, string responseHeaders)
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2022-08-16 10:36:48 +00:00
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
var webhook = await webhooksDbContext.WebhooksLogs.Where(t => t.Id == id).FirstOrDefaultAsync();
|
2022-02-03 13:43:37 +00:00
|
|
|
|
webhook.Status = status;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
webhook.RequestHeaders = requestHeaders;
|
2022-02-03 13:43:37 +00:00
|
|
|
|
webhook.ResponsePayload = responsePayload;
|
2022-08-16 10:36:48 +00:00
|
|
|
|
webhook.ResponseHeaders = responseHeaders;
|
2022-08-18 09:03:13 +00:00
|
|
|
|
webhook.Delivery = delivery;
|
2022-08-16 10:36:48 +00:00
|
|
|
|
|
2022-07-28 18:00:49 +00:00
|
|
|
|
webhooksDbContext.WebhooksLogs.Update(webhook);
|
2022-08-18 09:03:13 +00:00
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return webhook;
|
2022-02-03 13:43:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|