mirror of
https://github.com/bitwarden/server
synced 2026-01-15 23:13:56 +00:00
Move all event integration code to Dirt (#6757)
* Move all event integration code to Dirt * Format to fix lint
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
|
||||
namespace Bit.Api.AdminConsole.Models.Request.Organizations;
|
||||
|
||||
public class OrganizationIntegrationConfigurationRequestModel
|
||||
{
|
||||
public string? Configuration { get; set; }
|
||||
|
||||
public EventType? EventType { get; set; }
|
||||
|
||||
public string? Filters { get; set; }
|
||||
|
||||
public string? Template { get; set; }
|
||||
|
||||
public OrganizationIntegrationConfiguration ToOrganizationIntegrationConfiguration(Guid organizationIntegrationId)
|
||||
{
|
||||
return new OrganizationIntegrationConfiguration()
|
||||
{
|
||||
OrganizationIntegrationId = organizationIntegrationId,
|
||||
Configuration = Configuration,
|
||||
Filters = Filters,
|
||||
EventType = EventType,
|
||||
Template = Template
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Models.Data.EventIntegrations;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Api.AdminConsole.Models.Request.Organizations;
|
||||
|
||||
public class OrganizationIntegrationRequestModel : IValidatableObject
|
||||
{
|
||||
public string? Configuration { get; init; }
|
||||
|
||||
public IntegrationType Type { get; init; }
|
||||
|
||||
public OrganizationIntegration ToOrganizationIntegration(Guid organizationId)
|
||||
{
|
||||
return new OrganizationIntegration()
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
Configuration = Configuration,
|
||||
Type = Type,
|
||||
};
|
||||
}
|
||||
|
||||
public OrganizationIntegration ToOrganizationIntegration(OrganizationIntegration currentIntegration)
|
||||
{
|
||||
currentIntegration.Configuration = Configuration;
|
||||
return currentIntegration;
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case IntegrationType.CloudBillingSync or IntegrationType.Scim:
|
||||
yield return new ValidationResult($"{nameof(Type)} integrations are not yet supported.", [nameof(Type)]);
|
||||
break;
|
||||
case IntegrationType.Slack or IntegrationType.Teams:
|
||||
yield return new ValidationResult($"{nameof(Type)} integrations cannot be created directly.", [nameof(Type)]);
|
||||
break;
|
||||
case IntegrationType.Webhook:
|
||||
foreach (var r in ValidateConfiguration<WebhookIntegration>(allowNullOrEmpty: true))
|
||||
yield return r;
|
||||
break;
|
||||
case IntegrationType.Hec:
|
||||
foreach (var r in ValidateConfiguration<HecIntegration>(allowNullOrEmpty: false))
|
||||
yield return r;
|
||||
break;
|
||||
case IntegrationType.Datadog:
|
||||
foreach (var r in ValidateConfiguration<DatadogIntegration>(allowNullOrEmpty: false))
|
||||
yield return r;
|
||||
break;
|
||||
default:
|
||||
yield return new ValidationResult(
|
||||
$"Integration type '{Type}' is not recognized.",
|
||||
[nameof(Type)]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private List<ValidationResult> ValidateConfiguration<T>(bool allowNullOrEmpty)
|
||||
{
|
||||
var results = new List<ValidationResult>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Configuration))
|
||||
{
|
||||
if (!allowNullOrEmpty)
|
||||
results.Add(InvalidConfig<T>());
|
||||
return results;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (JsonSerializer.Deserialize<T>(Configuration) is null)
|
||||
results.Add(InvalidConfig<T>());
|
||||
}
|
||||
catch
|
||||
{
|
||||
results.Add(InvalidConfig<T>());
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ValidationResult InvalidConfig<T>() =>
|
||||
new(errorMessage: $"Must include valid {typeof(T).Name} configuration.", memberNames: [nameof(Configuration)]);
|
||||
}
|
||||
Reference in New Issue
Block a user