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

111 lines
3.9 KiB
C#
Raw Normal View History

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;
using Bit.Core;
2017-03-07 23:06:14 -05:00
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 ISubvaultService _subvaultService;
2017-03-07 23:06:14 -05:00
private readonly IUserService _userService;
private readonly CurrentContext _currentContext;
2017-03-07 23:06:14 -05:00
public SubvaultsController(
ISubvaultRepository subvaultRepository,
ISubvaultService subvaultService,
IUserService userService,
CurrentContext currentContext)
2017-03-07 23:06:14 -05:00
{
_subvaultRepository = subvaultRepository;
_subvaultService = subvaultService;
2017-03-07 23:06:14 -05:00
_userService = userService;
_currentContext = currentContext;
2017-03-07 23:06:14 -05:00
}
[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 subvault = await _subvaultRepository.GetByIdAsync(new Guid(id));
if(subvault == null || !_currentContext.OrganizationAdmin(subvault.OrganizationId))
2017-03-07 23:06:14 -05:00
{
throw new NotFoundException();
}
return new SubvaultResponseModel(subvault);
}
[HttpGet("")]
public async Task<ListResponseModel<SubvaultResponseModel>> Get(string orgId)
2017-03-07 23:06:14 -05:00
{
var orgIdGuid = new Guid(orgId);
if(!_currentContext.OrganizationAdmin(orgIdGuid))
{
throw new NotFoundException();
}
var subvaults = await _subvaultRepository.GetManyByOrganizationIdAsync(orgIdGuid);
2017-03-07 23:06:14 -05:00
var responses = subvaults.Select(s => new SubvaultResponseModel(s));
return new ListResponseModel<SubvaultResponseModel>(responses);
}
[HttpGet("~/subvaults")]
public async Task<ListResponseModel<SubvaultResponseModel>> GetUser()
2017-03-07 23:06:14 -05:00
{
var subvaults = await _subvaultRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value);
2017-03-07 23:06:14 -05:00
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
{
var orgIdGuid = new Guid(orgId);
if(!_currentContext.OrganizationAdmin(orgIdGuid))
{
throw new NotFoundException();
}
var subvault = model.ToSubvault(orgIdGuid);
await _subvaultService.SaveAsync(subvault);
2017-03-07 23:06:14 -05:00
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.GetByIdAsync(new Guid(id));
if(subvault == null || !_currentContext.OrganizationAdmin(subvault.OrganizationId))
2017-03-07 23:06:14 -05:00
{
throw new NotFoundException();
}
await _subvaultService.SaveAsync(model.ToSubvault(subvault));
2017-03-07 23:06:14 -05:00
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.GetByIdAsync(new Guid(id));
if(subvault == null || !_currentContext.OrganizationAdmin(subvault.OrganizationId))
2017-03-07 23:06:14 -05:00
{
throw new NotFoundException();
}
await _subvaultRepository.DeleteAsync(subvault);
}
}
}