mirror of
https://github.com/bitwarden/server.git
synced 2025-12-12 07:34:55 -06:00
* Add CQRS and caching support for OrganizationIntegrations * Use primary constructor for Delete command, per Claude suggestion * Fix namespace * Add XMLDoc for new commands / queries * Remove unnecessary extra call to AddExtendedCache in Startup (call in EventIntegrationsServiceCollectionExtensions handles this instead) * Alter strategy to use one cache / database call to retrieve all configurations for an event (including wildcards) * Updated README documentation to reflect updated Caching doc and updated CQRS approach
85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
using Bit.Api.AdminConsole.Models.Request.Organizations;
|
|
using Bit.Api.AdminConsole.Models.Response.Organizations;
|
|
using Bit.Core.AdminConsole.EventIntegrations.OrganizationIntegrations.Interfaces;
|
|
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,
|
|
ICreateOrganizationIntegrationCommand createCommand,
|
|
IUpdateOrganizationIntegrationCommand updateCommand,
|
|
IDeleteOrganizationIntegrationCommand deleteCommand,
|
|
IGetOrganizationIntegrationsQuery getQuery) : Controller
|
|
{
|
|
[HttpGet("")]
|
|
public async Task<List<OrganizationIntegrationResponseModel>> GetAsync(Guid organizationId)
|
|
{
|
|
if (!await HasPermission(organizationId))
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
var integrations = await getQuery.GetManyByOrganizationAsync(organizationId);
|
|
return integrations
|
|
.Select(integration => new OrganizationIntegrationResponseModel(integration))
|
|
.ToList();
|
|
}
|
|
|
|
[HttpPost("")]
|
|
public async Task<OrganizationIntegrationResponseModel> CreateAsync(Guid organizationId, [FromBody] OrganizationIntegrationRequestModel model)
|
|
{
|
|
if (!await HasPermission(organizationId))
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
var integration = model.ToOrganizationIntegration(organizationId);
|
|
var created = await createCommand.CreateAsync(integration);
|
|
|
|
return new OrganizationIntegrationResponseModel(created);
|
|
}
|
|
|
|
[HttpPut("{integrationId:guid}")]
|
|
public async Task<OrganizationIntegrationResponseModel> UpdateAsync(Guid organizationId, Guid integrationId, [FromBody] OrganizationIntegrationRequestModel model)
|
|
{
|
|
if (!await HasPermission(organizationId))
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
var integration = model.ToOrganizationIntegration(organizationId);
|
|
var updated = await updateCommand.UpdateAsync(organizationId, integrationId, integration);
|
|
|
|
return new OrganizationIntegrationResponseModel(updated);
|
|
}
|
|
|
|
[HttpDelete("{integrationId:guid}")]
|
|
public async Task DeleteAsync(Guid organizationId, Guid integrationId)
|
|
{
|
|
if (!await HasPermission(organizationId))
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await deleteCommand.DeleteAsync(organizationId, integrationId);
|
|
}
|
|
|
|
[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);
|
|
}
|
|
|
|
private async Task<bool> HasPermission(Guid organizationId)
|
|
{
|
|
return await currentContext.OrganizationOwner(organizationId);
|
|
}
|
|
}
|