2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Api.AdminConsole.Models.Request.Organizations;
|
|
|
|
|
|
using Bit.Api.AdminConsole.Models.Response.Organizations;
|
2025-04-28 08:20:47 -04:00
|
|
|
|
using Bit.Core;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
2025-04-28 08:20:47 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.AdminConsole.Controllers;
|
|
|
|
|
|
|
2025-04-28 08:20:47 -04:00
|
|
|
|
[RequireFeature(FeatureFlagKeys.EventBasedOrganizationIntegrations)]
|
2025-04-23 10:44:43 -04:00
|
|
|
|
[Route("organizations/{organizationId:guid}/integrations")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class OrganizationIntegrationController(
|
|
|
|
|
|
ICurrentContext currentContext,
|
|
|
|
|
|
IOrganizationIntegrationRepository integrationRepository) : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
[HttpPost("")]
|
|
|
|
|
|
public async Task<OrganizationIntegrationResponseModel> CreateAsync(Guid organizationId, [FromBody] OrganizationIntegrationRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var integration = await integrationRepository.CreateAsync(model.ToOrganizationIntegration(organizationId));
|
|
|
|
|
|
return new OrganizationIntegrationResponseModel(integration);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{integrationId:guid}")]
|
|
|
|
|
|
public async Task<OrganizationIntegrationResponseModel> UpdateAsync(Guid organizationId, Guid integrationId, [FromBody] OrganizationIntegrationRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var integration = await integrationRepository.GetByIdAsync(integrationId);
|
|
|
|
|
|
if (integration is null || integration.OrganizationId != organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await integrationRepository.ReplaceAsync(model.ToOrganizationIntegration(integration));
|
|
|
|
|
|
return new OrganizationIntegrationResponseModel(integration);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{integrationId:guid}")]
|
|
|
|
|
|
[HttpPost("{integrationId:guid}/delete")]
|
|
|
|
|
|
public async Task DeleteAsync(Guid organizationId, Guid integrationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var integration = await integrationRepository.GetByIdAsync(integrationId);
|
|
|
|
|
|
if (integration is null || integration.OrganizationId != organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await integrationRepository.DeleteAsync(integration);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> HasPermission(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await currentContext.OrganizationOwner(organizationId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|