2021-06-30 09:35:26 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Models.Api;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
2021-07-15 16:37:27 +02:00
|
|
|
|
using Bit.Core.Settings;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("providers")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class ProvidersController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IProviderRepository _providerRepository;
|
|
|
|
|
|
private readonly IProviderService _providerService;
|
|
|
|
|
|
private readonly ICurrentContext _currentContext;
|
2021-07-15 16:37:27 +02:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
|
|
|
|
|
|
public ProvidersController(IUserService userService, IProviderRepository providerRepository,
|
2021-07-15 16:37:27 +02:00
|
|
|
|
IProviderService providerService, ICurrentContext currentContext, GlobalSettings globalSettings)
|
2021-06-30 09:35:26 +02:00
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_providerRepository = providerRepository;
|
|
|
|
|
|
_providerService = providerService;
|
|
|
|
|
|
_currentContext = currentContext;
|
2021-07-15 16:37:27 +02:00
|
|
|
|
_globalSettings = globalSettings;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id:guid}")]
|
|
|
|
|
|
public async Task<ProviderResponseModel> Get(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_currentContext.ProviderUser(id))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new ProviderResponseModel(provider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-15 16:37:27 +02:00
|
|
|
|
[HttpPut("{id:guid}")]
|
|
|
|
|
|
[HttpPost("{id:guid}")]
|
|
|
|
|
|
public async Task<ProviderResponseModel> Put(Guid id, [FromBody]ProviderUpdateRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_currentContext.ProviderProviderAdmin(id))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _providerService.UpdateAsync(model.ToProvider(provider, _globalSettings));
|
|
|
|
|
|
return new ProviderResponseModel(provider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
[HttpPost("{id:guid}/setup")]
|
|
|
|
|
|
public async Task<ProviderResponseModel> Setup(Guid id, [FromBody]ProviderSetupRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_currentContext.ProviderProviderAdmin(id))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var provider = await _providerRepository.GetByIdAsync(id);
|
|
|
|
|
|
if (provider == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
|
await _providerService.CompleteSetupAsync(model.ToProvider(provider), userId, model.Token, model.Key);
|
|
|
|
|
|
|
|
|
|
|
|
return new ProviderResponseModel(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|