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

365 lines
13 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;
2017-04-11 10:52:28 -04:00
using Microsoft.AspNetCore.Identity;
using Bit.Core.Models.Table;
2017-08-14 20:57:45 -04:00
using Bit.Api.Utilities;
using Bit.Core.Models.Business;
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;
2017-08-14 22:05:37 -04:00
private readonly GlobalSettings _globalSettings;
2017-04-11 10:52:28 -04:00
private readonly UserManager<User> _userManager;
public OrganizationsController(
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
IOrganizationService organizationService,
IUserService userService,
2017-04-11 10:52:28 -04:00
CurrentContext currentContext,
2017-08-14 22:05:37 -04:00
GlobalSettings globalSettings,
2017-04-11 10:52:28 -04:00
UserManager<User> userManager)
{
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
_organizationService = organizationService;
_userService = userService;
_currentContext = currentContext;
2017-04-11 10:52:28 -04:00
_userManager = userManager;
2017-08-14 22:05:37 -04:00
_globalSettings = globalSettings;
}
[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-08-14 22:05:37 -04:00
if(!_globalSettings.SelfHosted && organization.Gateway != null)
2017-04-06 16:52:39 -04:00
{
2017-08-14 22:05:37 -04:00
var paymentService = new StripePaymentService();
var billingInfo = await paymentService.GetBillingAsync(organization);
if(billingInfo == null)
{
throw new NotFoundException();
}
return new OrganizationBillingResponseModel(organization, billingInfo);
}
else
{
return new OrganizationBillingResponseModel(organization);
2017-04-06 16:52:39 -04:00
}
2017-04-06 13:21:26 -04:00
}
[HttpGet("{id}/license")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<OrganizationLicense> GetLicense(string id, [FromQuery]Guid installationId)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var license = await _organizationService.GenerateLicenseAsync(orgIdGuid, installationId);
if(license == null)
{
throw new NotFoundException();
}
return license;
}
[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("")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<OrganizationResponseModel> Post([FromBody]OrganizationCreateRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
var organizationSignup = model.ToOrganizationSignup(user);
var result = await _organizationService.SignUpAsync(organizationSignup);
return new OrganizationResponseModel(result.Item1);
}
2017-08-14 20:57:45 -04:00
[HttpPost("license")]
[SelfHosted(SelfHostedOnly = true)]
public async Task<OrganizationResponseModel> PostLicense(OrganizationCreateLicenseRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
var license = await ApiHelpers.ReadJsonFileFromBody<OrganizationLicense>(HttpContext, model.License);
if(license == null)
{
throw new BadRequestException("Invalid license");
}
var result = await _organizationService.SignUpAsync(license, user, model.Key);
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();
}
var updatebilling = model.BusinessName != organization.BusinessName ||
model.BillingEmail != organization.BillingEmail;
await _organizationService.UpdateAsync(model.ToOrganization(organization), updatebilling);
return new OrganizationResponseModel(organization);
}
2017-04-08 16:41:40 -04:00
[HttpPut("{id}/payment")]
[HttpPost("{id}/payment")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
2017-07-06 14:55:58 -04:00
public async Task PutPayment(string id, [FromBody]PaymentRequestModel model)
2017-04-08 16:41:40 -04:00
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.ReplacePaymentMethodAsync(orgIdGuid, model.PaymentToken);
}
[HttpPut("{id}/upgrade")]
[HttpPost("{id}/upgrade")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
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")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
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);
}
2017-07-11 10:59:59 -04:00
[HttpPut("{id}/storage")]
[HttpPost("{id}/storage")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
2017-07-11 10:59:59 -04:00
public async Task PutStorage(string id, [FromBody]StorageRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.AdjustStorageAsync(orgIdGuid, model.StorageGbAdjustment.Value);
}
2017-08-14 22:05:37 -04:00
2017-08-14 09:23:54 -04:00
[HttpPost("{id}/verify-bank")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
2017-08-14 09:23:54 -04:00
public async Task PostVerifyBank(string id, [FromBody]OrganizationVerifyBankRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.VerifyBankAsync(orgIdGuid, model.Amount1.Value, model.Amount2.Value);
}
2017-07-11 10:59:59 -04:00
[HttpPut("{id}/cancel")]
[HttpPost("{id}/cancel")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
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")]
2017-08-14 20:57:45 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutReinstate(string id)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
await _organizationService.ReinstateSubscriptionAsync(orgIdGuid);
}
2017-04-12 10:07:27 -04:00
[HttpPost("{id}/leave")]
public async Task Leave(string id)
{
var orgGuidId = new Guid(id);
if(!_currentContext.OrganizationUser(orgGuidId))
{
throw new NotFoundException();
}
var userId = _userService.GetProperUserId(User);
await _organizationService.DeleteUserAsync(orgGuidId, userId.Value);
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
2017-04-11 10:52:28 -04:00
public async Task Delete(string id, [FromBody]OrganizationDeleteRequestModel 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-11 10:52:28 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-04-11 10:52:28 -04:00
if(!await _userManager.CheckPasswordAsync(user, model.MasterPasswordHash))
{
await Task.Delay(2000);
throw new BadRequestException("MasterPasswordHash", "Invalid password.");
2017-04-11 10:52:28 -04:00
}
else
{
await _organizationService.DeleteAsync(organization);
}
}
2017-08-14 21:25:06 -04:00
[HttpPut("{id}/license")]
[HttpPost("{id}/license")]
[SelfHosted(SelfHostedOnly = true)]
public async Task PutLicense(string id, LicenseRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
2017-08-14 22:05:37 -04:00
var license = await ApiHelpers.ReadJsonFileFromBody<OrganizationLicense>(HttpContext, model.License);
2017-08-14 21:25:06 -04:00
if(license == null)
{
throw new BadRequestException("Invalid license");
}
2017-08-14 22:05:37 -04:00
await _organizationService.UpdateLicenseAsync(new Guid(id), license);
2017-08-14 21:25:06 -04:00
}
[HttpPost("{id}/import")]
2017-05-13 17:08:56 -04:00
public async Task Import(string id, [FromBody]ImportOrganizationUsersRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationAdmin(orgIdGuid))
{
throw new NotFoundException();
}
2017-05-13 17:08:56 -04:00
var userId = _userService.GetProperUserId(User);
2017-05-15 16:37:56 -04:00
await _organizationService.ImportAsync(
orgIdGuid,
userId.Value,
2017-05-16 00:11:21 -04:00
model.Groups.Select(g => g.ToImportedGroup(orgIdGuid)),
model.Users.Where(u => !u.Deleted).Select(u => u.ToImportedOrganizationUser()),
2017-05-16 11:21:53 -04:00
model.Users.Where(u => u.Deleted).Select(u => u.ExternalId));
}
}
}