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

203 lines
6.9 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
2017-03-08 21:55:08 -05:00
using Bit.Core.Models.Api;
using Bit.Core.Exceptions;
using Bit.Core.Services;
using Bit.Core;
namespace Bit.Api.Controllers
{
[Route("organizations")]
[Authorize("Application")]
public class OrganizationsController : Controller
{
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly IOrganizationService _organizationService;
private readonly IUserService _userService;
private readonly CurrentContext _currentContext;
public OrganizationsController(
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
IOrganizationService organizationService,
IUserService userService,
CurrentContext currentContext)
{
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
_organizationService = organizationService;
_userService = userService;
_currentContext = currentContext;
}
[HttpGet("{id}")]
2017-03-03 21:53:27 -05:00
public async Task<OrganizationResponseModel> Get(string id)
{
2017-04-06 13:21:26 -04:00
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
if(organization == null)
2017-03-03 21:53:27 -05:00
{
throw new NotFoundException();
}
return new OrganizationResponseModel(organization);
}
2017-04-06 13:21:26 -04:00
[HttpGet("{id}/billing")]
2017-04-06 16:52:39 -04:00
public async Task<OrganizationBillingResponseModel> GetBilling(string id)
2017-04-06 13:21:26 -04:00
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
if(organization == null)
{
throw new NotFoundException();
}
2017-04-06 16:52:39 -04:00
var billingInfo = await _organizationService.GetBillingAsync(organization);
if(billingInfo == null)
{
throw new NotFoundException();
}
2017-04-06 13:21:26 -04:00
2017-04-06 16:52:39 -04:00
return new OrganizationBillingResponseModel(organization, billingInfo);
2017-04-06 13:21:26 -04:00
}
[HttpGet("")]
2017-04-05 16:45:21 -04:00
public async Task<ListResponseModel<OrganizationResponseModel>> GetUser()
{
var userId = _userService.GetProperUserId(User).Value;
var organizations = await _organizationRepository.GetManyByUserIdAsync(userId);
var responses = organizations.Select(o => new OrganizationResponseModel(o));
return new ListResponseModel<OrganizationResponseModel>(responses);
}
[HttpPost("")]
public async Task<OrganizationResponseModel> Post([FromBody]OrganizationCreateRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
var organizationSignup = model.ToOrganizationSignup(user);
var result = await _organizationService.SignUpAsync(organizationSignup);
return new OrganizationResponseModel(result.Item1);
}
[HttpPut("{id}")]
[HttpPost("{id}")]
public async Task<OrganizationResponseModel> Put(string id, [FromBody]OrganizationUpdateRequestModel model)
{
2017-04-06 13:21:26 -04:00
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
if(organization == null)
{
throw new NotFoundException();
}
2017-04-10 19:07:38 -04:00
await _organizationService.UpdateAsync(model.ToOrganization(organization), true);
return new OrganizationResponseModel(organization);
}
2017-04-08 16:41:40 -04:00
[HttpPut("{id}/payment")]
[HttpPost("{id}/payment")]
public async Task PutPayment(string id, [FromBody]OrganizationPaymentRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.ReplacePaymentMethodAsync(orgIdGuid, model.PaymentToken);
}
[HttpPut("{id}/upgrade")]
[HttpPost("{id}/upgrade")]
public async Task PutUpgrade(string id, [FromBody]OrganizationUpgradeRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.UpgradePlanAsync(orgIdGuid, model.PlanType, model.AdditionalSeats);
}
[HttpPut("{id}/seat")]
[HttpPost("{id}/seat")]
public async Task PutSeat(string id, [FromBody]OrganizationSeatRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.AdjustSeatsAsync(orgIdGuid, model.SeatAdjustment.Value);
}
[HttpPut("{id}/cancel")]
[HttpPost("{id}/cancel")]
public async Task PutCancel(string id)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.CancelSubscriptionAsync(orgIdGuid, true);
}
[HttpPut("{id}/reinstate")]
[HttpPost("{id}/reinstate")]
public async Task PutReinstate(string id)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.ReinstateSubscriptionAsync(orgIdGuid);
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
public async Task Delete(string id)
{
2017-04-06 13:21:26 -04:00
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
if(organization == null)
{
throw new NotFoundException();
}
await _organizationRepository.DeleteAsync(organization);
}
}
}