2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Api.AdminConsole.Models.Request.Organizations;
|
|
|
|
|
|
using Bit.Api.AdminConsole.Models.Response.Organizations;
|
2025-12-05 15:28:07 -05:00
|
|
|
|
using Bit.Core.AdminConsole.EventIntegrations.OrganizationIntegrations.Interfaces;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.AdminConsole.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[Route("organizations/{organizationId:guid}/integrations")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class OrganizationIntegrationController(
|
|
|
|
|
|
ICurrentContext currentContext,
|
2025-12-05 15:28:07 -05:00
|
|
|
|
ICreateOrganizationIntegrationCommand createCommand,
|
|
|
|
|
|
IUpdateOrganizationIntegrationCommand updateCommand,
|
|
|
|
|
|
IDeleteOrganizationIntegrationCommand deleteCommand,
|
|
|
|
|
|
IGetOrganizationIntegrationsQuery getQuery) : Controller
|
2025-04-23 10:44:43 -04:00
|
|
|
|
{
|
2025-07-23 14:24:59 -04:00
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<List<OrganizationIntegrationResponseModel>> GetAsync(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-05 15:28:07 -05:00
|
|
|
|
var integrations = await getQuery.GetManyByOrganizationAsync(organizationId);
|
2025-07-23 14:24:59 -04:00
|
|
|
|
return integrations
|
|
|
|
|
|
.Select(integration => new OrganizationIntegrationResponseModel(integration))
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-23 10:44:43 -04:00
|
|
|
|
[HttpPost("")]
|
|
|
|
|
|
public async Task<OrganizationIntegrationResponseModel> CreateAsync(Guid organizationId, [FromBody] OrganizationIntegrationRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-05 15:28:07 -05:00
|
|
|
|
var integration = model.ToOrganizationIntegration(organizationId);
|
|
|
|
|
|
var created = await createCommand.CreateAsync(integration);
|
|
|
|
|
|
|
|
|
|
|
|
return new OrganizationIntegrationResponseModel(created);
|
2025-04-23 10:44:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{integrationId:guid}")]
|
|
|
|
|
|
public async Task<OrganizationIntegrationResponseModel> UpdateAsync(Guid organizationId, Guid integrationId, [FromBody] OrganizationIntegrationRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-05 15:28:07 -05:00
|
|
|
|
var integration = model.ToOrganizationIntegration(organizationId);
|
|
|
|
|
|
var updated = await updateCommand.UpdateAsync(organizationId, integrationId, integration);
|
2025-04-23 10:44:43 -04:00
|
|
|
|
|
2025-12-05 15:28:07 -05:00
|
|
|
|
return new OrganizationIntegrationResponseModel(updated);
|
2025-04-23 10:44:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{integrationId:guid}")]
|
|
|
|
|
|
public async Task DeleteAsync(Guid organizationId, Guid integrationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!await HasPermission(organizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-05 15:28:07 -05:00
|
|
|
|
await deleteCommand.DeleteAsync(organizationId, integrationId);
|
2025-04-23 10:44:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-10 01:00:07 +02:00
|
|
|
|
[HttpPost("{integrationId:guid}/delete")]
|
|
|
|
|
|
[Obsolete("This endpoint is deprecated. Use DELETE method instead")]
|
|
|
|
|
|
public async Task PostDeleteAsync(Guid organizationId, Guid integrationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await DeleteAsync(organizationId, integrationId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-23 10:44:43 -04:00
|
|
|
|
private async Task<bool> HasPermission(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await currentContext.OrganizationOwner(organizationId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|