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

178 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;
2017-04-12 12:42:00 -04:00
using Bit.Core;
namespace Bit.Api.Controllers
{
[Route("ciphers")]
[Authorize("Application")]
public class CiphersController : Controller
{
private readonly ICipherRepository _cipherRepository;
2017-03-23 17:43:12 -04:00
private readonly ISubvaultCipherRepository _subvaultCipherRepository;
private readonly ICipherService _cipherService;
private readonly IUserService _userService;
2017-04-12 12:42:00 -04:00
private readonly CurrentContext _currentContext;
public CiphersController(
ICipherRepository cipherRepository,
2017-03-23 17:43:12 -04:00
ISubvaultCipherRepository subvaultCipherRepository,
ICipherService cipherService,
2017-04-12 12:42:00 -04:00
IUserService userService,
CurrentContext currentContext)
{
_cipherRepository = cipherRepository;
2017-03-23 17:43:12 -04:00
_subvaultCipherRepository = subvaultCipherRepository;
_cipherService = cipherService;
_userService = userService;
2017-04-12 12:42:00 -04:00
_currentContext = currentContext;
}
[HttpGet("{id}")]
2017-02-28 22:51:29 -05:00
public async Task<CipherResponseModel> Get(string id)
{
2017-02-18 01:17:09 -05:00
var userId = _userService.GetProperUserId(User).Value;
2017-02-28 22:51:29 -05:00
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
return new CipherResponseModel(cipher);
}
2017-04-04 17:22:47 -04:00
[HttpGet("{id}/full-details")]
public async Task<CipherFullDetailsResponseModel> GetDetails(string id)
{
var userId = _userService.GetProperUserId(User).Value;
var cipherId = new Guid(id);
var cipher = await _cipherRepository.GetFullDetailsByIdAsync(cipherId, userId);
if(cipher == null)
{
throw new NotFoundException();
}
var subvaultCiphers = await _subvaultCipherRepository.GetManyByUserIdCipherIdAsync(userId, cipherId);
return new CipherFullDetailsResponseModel(cipher, subvaultCiphers);
}
[HttpGet("")]
2017-02-28 22:51:29 -05:00
public async Task<ListResponseModel<CipherResponseModel>> Get()
{
2017-02-18 01:17:09 -05:00
var userId = _userService.GetProperUserId(User).Value;
2017-02-28 22:51:29 -05:00
var ciphers = await _cipherRepository.GetManyByUserIdAsync(userId);
var responses = ciphers.Select(c => new CipherResponseModel(c));
2017-02-28 22:51:29 -05:00
return new ListResponseModel<CipherResponseModel>(responses);
}
2017-04-04 17:22:47 -04:00
[HttpGet("details")]
2017-03-23 17:43:12 -04:00
public async Task<ListResponseModel<CipherDetailsResponseModel>> GetSubvaults()
{
var userId = _userService.GetProperUserId(User).Value;
var ciphers = await _cipherRepository.GetManyByUserIdHasSubvaultsAsync(userId);
2017-03-23 17:43:12 -04:00
var subvaultCiphers = await _subvaultCipherRepository.GetManyByUserIdAsync(userId);
var subvaultCiphersGroupDict = subvaultCiphers.GroupBy(s => s.CipherId).ToDictionary(s => s.Key);
var responses = ciphers.Select(c => new CipherDetailsResponseModel(c, subvaultCiphersGroupDict));
2017-03-23 17:43:12 -04:00
return new ListResponseModel<CipherDetailsResponseModel>(responses);
}
[Obsolete]
[HttpGet("history")]
public Task<CipherHistoryResponseModel> Get(DateTime since)
{
return Task.FromResult(new CipherHistoryResponseModel());
}
[HttpPost("import")]
public async Task PostImport([FromBody]ImportRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var folderCiphers = model.Folders.Select(f => f.ToFolder(userId)).ToList();
2017-03-18 23:41:46 -04:00
var otherCiphers = model.Logins.Select(s => s.ToCipherDetails(userId)).ToList();
await _cipherService.ImportCiphersAsync(
folderCiphers,
otherCiphers,
model.FolderRelationships);
}
[Obsolete]
[HttpPut("{id}/favorite")]
[HttpPost("{id}/favorite")]
public async Task Favorite(string id)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
await _cipherService.UpdatePartialAsync(new Guid(id), userId, cipher.FolderId, !cipher.Favorite);
}
[HttpPut("{id}/partial")]
[HttpPost("{id}/partial")]
public async Task PutPartial(string id, [FromBody]CipherPartialRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var folderId = string.IsNullOrWhiteSpace(model.FolderId) ? null : (Guid?)new Guid(model.FolderId);
await _cipherService.UpdatePartialAsync(new Guid(id), userId, folderId, model.Favorite);
}
2017-04-12 12:42:00 -04:00
[HttpPut("{id}/share")]
[HttpPost("{id}/share")]
public async Task PutShare(string id, [FromBody]CipherShareRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
2017-03-21 21:13:20 -04:00
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
2017-04-12 12:42:00 -04:00
if(cipher == null || cipher.UserId != userId ||
!_currentContext.OrganizationUser(new Guid(model.Cipher.OrganizationId)))
{
throw new NotFoundException();
}
2017-04-12 12:42:00 -04:00
await _cipherService.ShareAsync(model.Cipher.ToCipher(cipher), new Guid(model.Cipher.OrganizationId),
model.SubvaultIds.Select(s => new Guid(s)), userId);
}
2017-04-12 12:42:00 -04:00
[HttpPut("{id}/subvaults")]
[HttpPost("{id}/subvaults")]
public async Task PutSubvaults(string id, [FromBody]CipherSubvaultsRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null || !cipher.OrganizationId.HasValue ||
!_currentContext.OrganizationUser(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
await _cipherService.SaveSubvaultsAsync(cipher, model.SubvaultIds.Select(s => new Guid(s)), userId);
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
public async Task Delete(string id)
{
2017-03-21 21:13:20 -04:00
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
await _cipherService.DeleteAsync(cipher, userId);
}
}
}