2016-06-07 20:05:27 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Bit.Api.Models;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("ciphers")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class CiphersController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
|
|
|
|
|
private readonly ICipherService _cipherService;
|
2017-01-24 22:46:54 -05:00
|
|
|
|
private readonly IUserService _userService;
|
2016-06-07 20:05:27 -04:00
|
|
|
|
|
|
|
|
|
|
public CiphersController(
|
|
|
|
|
|
ICipherRepository cipherRepository,
|
|
|
|
|
|
ICipherService cipherService,
|
2017-01-24 22:46:54 -05:00
|
|
|
|
IUserService userService)
|
2016-06-07 20:05:27 -04:00
|
|
|
|
{
|
|
|
|
|
|
_cipherRepository = cipherRepository;
|
|
|
|
|
|
_cipherService = cipherService;
|
2017-01-24 22:46:54 -05:00
|
|
|
|
_userService = userService;
|
2016-06-07 20:05:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2017-02-28 22:51:29 -05:00
|
|
|
|
public async Task<CipherResponseModel> Get(string id)
|
2016-06-07 20:05:27 -04:00
|
|
|
|
{
|
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);
|
2016-06-07 20:05:27 -04:00
|
|
|
|
if(cipher == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-28 22:51:29 -05:00
|
|
|
|
return new CipherResponseModel(cipher, userId);
|
2016-06-07 20:05:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
2017-02-28 22:51:29 -05:00
|
|
|
|
public async Task<ListResponseModel<CipherResponseModel>> Get()
|
2016-06-07 20:05:27 -04:00
|
|
|
|
{
|
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, userId));
|
|
|
|
|
|
return new ListResponseModel<CipherResponseModel>(responses);
|
2016-06-07 20:05:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-21 22:52:02 -05:00
|
|
|
|
[Obsolete]
|
2016-06-08 20:40:20 -04:00
|
|
|
|
[HttpGet("history")]
|
|
|
|
|
|
public async Task<CipherHistoryResponseModel> Get(DateTime since)
|
|
|
|
|
|
{
|
2017-02-18 01:17:09 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2016-06-08 20:40:20 -04:00
|
|
|
|
var history = await _cipherRepository.GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(
|
2017-02-18 01:17:09 -05:00
|
|
|
|
since, userId);
|
|
|
|
|
|
return new CipherHistoryResponseModel(history.Item1, history.Item2, userId);
|
2016-06-08 20:40:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-07 20:05:27 -04:00
|
|
|
|
[HttpPost("import")]
|
|
|
|
|
|
public async Task PostImport([FromBody]ImportRequestModel model)
|
|
|
|
|
|
{
|
2017-01-24 22:46:54 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
var folderCiphers = model.Folders.Select(f => f.ToCipher(userId)).ToList();
|
|
|
|
|
|
var otherCiphers = model.Logins.Select(s => s.ToCipher(userId)).ToList();
|
2016-06-07 20:05:27 -04:00
|
|
|
|
|
|
|
|
|
|
await _cipherService.ImportCiphersAsync(
|
|
|
|
|
|
folderCiphers,
|
|
|
|
|
|
otherCiphers,
|
|
|
|
|
|
model.FolderRelationships);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-08 22:19:08 -04:00
|
|
|
|
[HttpPut("{id}/favorite")]
|
2016-07-13 21:43:48 -04:00
|
|
|
|
[HttpPost("{id}/favorite")]
|
2016-06-08 22:19:08 -04:00
|
|
|
|
public async Task Favorite(string id)
|
|
|
|
|
|
{
|
2017-01-24 22:46:54 -05:00
|
|
|
|
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
2016-06-08 22:19:08 -04:00
|
|
|
|
if(cipher == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cipher.Favorite = !cipher.Favorite;
|
|
|
|
|
|
|
2016-06-29 01:15:37 -04:00
|
|
|
|
await _cipherService.SaveAsync(cipher);
|
2016-06-08 22:19:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-07 20:05:27 -04:00
|
|
|
|
[HttpDelete("{id}")]
|
2016-07-13 21:43:48 -04:00
|
|
|
|
[HttpPost("{id}/delete")]
|
2016-06-07 20:05:27 -04:00
|
|
|
|
public async Task Delete(string id)
|
|
|
|
|
|
{
|
2017-01-24 22:46:54 -05:00
|
|
|
|
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
2016-06-07 20:05:27 -04:00
|
|
|
|
if(cipher == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-29 01:15:37 -04:00
|
|
|
|
await _cipherService.DeleteAsync(cipher);
|
2016-06-07 20:05:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|