2017-03-07 23:06:14 -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-07 23:06:14 -05:00
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
2017-03-09 22:09:09 -05:00
|
|
|
|
[Route("organizations/{orgId}/subvaults")]
|
2017-03-07 23:06:14 -05:00
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class SubvaultsController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ISubvaultRepository _subvaultRepository;
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
|
|
|
|
|
|
public SubvaultsController(
|
|
|
|
|
|
ISubvaultRepository subvaultRepository,
|
|
|
|
|
|
IUserService userService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_subvaultRepository = subvaultRepository;
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2017-03-09 22:09:09 -05:00
|
|
|
|
public async Task<SubvaultResponseModel> Get(string orgId, string id)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
var subvault = await _subvaultRepository.GetByIdAdminUserIdAsync(new Guid(id), userId);
|
|
|
|
|
|
if(subvault == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new SubvaultResponseModel(subvault);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-09 22:09:09 -05:00
|
|
|
|
[HttpGet("~/subvaults")]
|
2017-03-07 23:06:14 -05:00
|
|
|
|
public async Task<ListResponseModel<SubvaultResponseModel>> Get()
|
|
|
|
|
|
{
|
|
|
|
|
|
var subvaults = await _subvaultRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value);
|
|
|
|
|
|
var responses = subvaults.Select(s => new SubvaultResponseModel(s));
|
|
|
|
|
|
return new ListResponseModel<SubvaultResponseModel>(responses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-09 22:09:09 -05:00
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<ListResponseModel<SubvaultResponseModel>> GetByOrganization(string orgId)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
2017-03-09 22:09:09 -05:00
|
|
|
|
var subvaults = await _subvaultRepository.GetManyByOrganizationIdAdminUserIdAsync(new Guid(orgId),
|
2017-03-07 23:06:14 -05:00
|
|
|
|
_userService.GetProperUserId(User).Value);
|
|
|
|
|
|
var responses = subvaults.Select(s => new SubvaultResponseModel(s));
|
|
|
|
|
|
return new ListResponseModel<SubvaultResponseModel>(responses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("")]
|
2017-03-09 22:09:09 -05:00
|
|
|
|
public async Task<SubvaultResponseModel> Post(string orgId, [FromBody]SubvaultRequestModel model)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
|
|
|
|
|
// TODO: permission check
|
2017-03-09 22:09:09 -05:00
|
|
|
|
var subvault = model.ToSubvault(new Guid(orgId));
|
2017-03-07 23:06:14 -05:00
|
|
|
|
await _subvaultRepository.CreateAsync(subvault);
|
|
|
|
|
|
return new SubvaultResponseModel(subvault);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
|
[HttpPost("{id}")]
|
2017-03-09 22:09:09 -05:00
|
|
|
|
public async Task<SubvaultResponseModel> Put(string orgId, string id, [FromBody]SubvaultRequestModel model)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
|
|
|
|
|
var subvault = await _subvaultRepository.GetByIdAdminUserIdAsync(new Guid(id),
|
|
|
|
|
|
_userService.GetProperUserId(User).Value);
|
|
|
|
|
|
if(subvault == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _subvaultRepository.ReplaceAsync(model.ToSubvault(subvault));
|
|
|
|
|
|
return new SubvaultResponseModel(subvault);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
[HttpPost("{id}/delete")]
|
2017-03-09 22:09:09 -05:00
|
|
|
|
public async Task Delete(string orgId, string id)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
|
|
|
|
|
var subvault = await _subvaultRepository.GetByIdAdminUserIdAsync(new Guid(id),
|
|
|
|
|
|
_userService.GetProperUserId(User).Value);
|
|
|
|
|
|
if(subvault == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _subvaultRepository.DeleteAsync(subvault);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|