Files
server/src/Api/Controllers/ProviderOrganizationsController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
3.4 KiB
C#
Raw Normal View History

using Bit.Api.Models.Request.Providers;
2021-12-14 15:05:07 +00:00
using Bit.Api.Models.Response;
using Bit.Api.Models.Response.Providers;
2021-06-30 09:35:26 +02:00
using Bit.Core.Context;
using Bit.Core.Exceptions;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
2021-06-30 09:35:26 +02:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Bit.Api.Controllers;
2022-08-29 16:06:55 -04:00
2021-06-30 09:35:26 +02:00
[Route("providers/{providerId:guid}/organizations")]
[Authorize("Application")]
public class ProviderOrganizationsController : Controller
{
private readonly IProviderOrganizationRepository _providerOrganizationRepository;
private readonly IProviderService _providerService;
private readonly IUserService _userService;
private readonly ICurrentContext _currentContext;
2022-08-29 16:06:55 -04:00
2021-06-30 09:35:26 +02:00
public ProviderOrganizationsController(
IProviderOrganizationRepository providerOrganizationRepository,
IProviderService providerService,
IUserService userService,
ICurrentContext currentContext)
2022-08-29 16:06:55 -04:00
{
2021-06-30 09:35:26 +02:00
_providerOrganizationRepository = providerOrganizationRepository;
_providerService = providerService;
_userService = userService;
_currentContext = currentContext;
2022-08-29 16:06:55 -04:00
}
2021-06-30 09:35:26 +02:00
[HttpGet("")]
public async Task<ListResponseModel<ProviderOrganizationOrganizationDetailsResponseModel>> Get(Guid providerId)
2022-08-29 16:06:55 -04:00
{
2021-06-30 09:35:26 +02:00
if (!_currentContext.AccessProviderOrganizations(providerId))
{
throw new NotFoundException();
}
2021-12-16 15:35:09 +01:00
2021-06-30 09:35:26 +02:00
var providerOrganizations = await _providerOrganizationRepository.GetManyDetailsByProviderAsync(providerId);
var responses = providerOrganizations.Select(o => new ProviderOrganizationOrganizationDetailsResponseModel(o));
return new ListResponseModel<ProviderOrganizationOrganizationDetailsResponseModel>(responses);
}
2021-06-30 09:35:26 +02:00
[HttpPost("add")]
public async Task Add(Guid providerId, [FromBody] ProviderOrganizationAddRequestModel model)
2022-08-29 16:06:55 -04:00
{
2021-06-30 09:35:26 +02:00
if (!_currentContext.ManageProviderOrganizations(providerId))
2022-08-29 16:06:55 -04:00
{
2021-06-30 09:35:26 +02:00
throw new NotFoundException();
}
var userId = _userService.GetProperUserId(User).Value;
2021-12-16 15:35:09 +01:00
2021-06-30 09:35:26 +02:00
await _providerService.AddOrganization(providerId, model.OrganizationId, userId, model.Key);
2022-08-29 16:06:55 -04:00
}
2021-06-30 09:35:26 +02:00
[HttpPost("")]
2021-06-30 09:35:26 +02:00
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<ProviderOrganizationResponseModel> Post(Guid providerId, [FromBody] ProviderOrganizationCreateRequestModel model)
2022-08-29 16:06:55 -04:00
{
2021-06-30 09:35:26 +02:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2021-06-30 09:35:26 +02:00
throw new UnauthorizedAccessException();
}
if (!_currentContext.ManageProviderOrganizations(providerId))
{
throw new NotFoundException();
}
2022-08-29 14:53:16 -04:00
var organizationSignup = model.OrganizationCreateRequest.ToOrganizationSignup(user);
2021-06-30 09:35:26 +02:00
var result = await _providerService.CreateOrganizationAsync(providerId, organizationSignup, model.ClientOwnerEmail, user);
return new ProviderOrganizationResponseModel(result);
}
[HttpDelete("{id:guid}")]
[HttpPost("{id:guid}/delete")]
public async Task Delete(Guid providerId, Guid id)
2022-08-29 16:06:55 -04:00
{
if (!_currentContext.ManageProviderOrganizations(providerId))
2022-08-29 16:06:55 -04:00
{
throw new NotFoundException();
}
2022-08-29 16:06:55 -04:00
var userId = _userService.GetProperUserId(User);
await _providerService.RemoveOrganizationAsync(providerId, id, userId.Value);
2021-06-30 09:35:26 +02:00
}
}