2017-03-02 00:15:05 -05:00
|
|
|
|
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;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Services;
|
2017-04-05 16:13:40 -04:00
|
|
|
|
using Bit.Core;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("organizations")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class OrganizationsController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
2017-03-03 00:07:11 -05:00
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
|
private readonly IOrganizationService _organizationService;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
private readonly IUserService _userService;
|
2017-04-05 16:13:40 -04:00
|
|
|
|
private readonly CurrentContext _currentContext;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
|
|
|
|
|
|
public OrganizationsController(
|
|
|
|
|
|
IOrganizationRepository organizationRepository,
|
2017-03-03 00:07:11 -05:00
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
|
|
|
|
|
IOrganizationService organizationService,
|
2017-04-05 16:13:40 -04:00
|
|
|
|
IUserService userService,
|
|
|
|
|
|
CurrentContext currentContext)
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
|
|
|
|
|
_organizationRepository = organizationRepository;
|
2017-03-03 00:07:11 -05:00
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
|
|
|
|
|
_organizationService = organizationService;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
_userService = userService;
|
2017-04-05 16:13:40 -04:00
|
|
|
|
_currentContext = currentContext;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[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")]
|
|
|
|
|
|
public async Task<OrganizationResponseModel> GetBilling(string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var orgIdGuid = new Guid(id);
|
|
|
|
|
|
if(!_currentContext.OrganizationOwner(orgIdGuid))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
|
|
|
|
|
|
if(organization == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: billing stuff
|
|
|
|
|
|
|
|
|
|
|
|
return new OrganizationResponseModel(organization);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-02 00:15:05 -05:00
|
|
|
|
[HttpGet("")]
|
2017-04-05 16:45:21 -04:00
|
|
|
|
public async Task<ListResponseModel<OrganizationResponseModel>> GetUser()
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
|
|
|
|
|
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-04-05 16:13:40 -04:00
|
|
|
|
public async Task<OrganizationResponseModel> Post([FromBody]OrganizationCreateRequestModel model)
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
2017-03-03 00:07:11 -05:00
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
|
|
var organizationSignup = model.ToOrganizationSignup(user);
|
|
|
|
|
|
var result = await _organizationService.SignUpAsync(organizationSignup);
|
2017-04-05 16:13:40 -04:00
|
|
|
|
return new OrganizationResponseModel(result.Item1);
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[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)
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.ReplaceAsync(model.ToOrganization(organization));
|
|
|
|
|
|
return new OrganizationResponseModel(organization);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[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)
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.DeleteAsync(organization);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|