1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-08 19:47:44 +01:00
bitwarden-server/src/Api/Controllers/CiphersController.cs

105 lines
3.5 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-09 03:55:08 +01:00
using Bit.Core.Models.Api;
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;
private readonly IUserService _userService;
public CiphersController(
ICipherRepository cipherRepository,
ICipherService cipherService,
IUserService userService)
{
_cipherRepository = cipherRepository;
_cipherService = cipherService;
_userService = userService;
}
[HttpGet("{id}")]
2017-03-01 04:51:29 +01:00
public async Task<CipherResponseModel> Get(string id)
{
2017-02-18 07:17:09 +01:00
var userId = _userService.GetProperUserId(User).Value;
2017-03-01 04:51:29 +01:00
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
return new CipherResponseModel(cipher);
}
[HttpGet("")]
2017-03-01 04:51:29 +01:00
public async Task<ListResponseModel<CipherResponseModel>> Get()
{
2017-02-18 07:17:09 +01:00
var userId = _userService.GetProperUserId(User).Value;
2017-03-01 04:51:29 +01:00
var ciphers = await _cipherRepository.GetManyByUserIdAsync(userId);
var responses = ciphers.Select(c => new CipherResponseModel(c));
2017-03-01 04:51:29 +01:00
return new ListResponseModel<CipherResponseModel>(responses);
}
//[Obsolete]
//[HttpGet("history")]
//public async Task<CipherHistoryResponseModel> Get(DateTime since)
//{
// var userId = _userService.GetProperUserId(User).Value;
// var history = await _cipherRepository.GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(
// since, userId);
// return new CipherHistoryResponseModel(history.Item1, history.Item2, userId);
//}
[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-19 04:41:46 +01:00
var otherCiphers = model.Logins.Select(s => s.ToCipherDetails(userId)).ToList();
await _cipherService.ImportCiphersAsync(
folderCiphers,
otherCiphers,
model.FolderRelationships);
}
//[HttpPut("{id}/favorite")]
//[HttpPost("{id}/favorite")]
//public async Task Favorite(string id)
//{
// var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
// if(cipher == null)
// {
// throw new NotFoundException();
// }
// cipher.Favorite = !cipher.Favorite;
// await _cipherService.SaveAsync(cipher);
//}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
public async Task Delete(string id)
{
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
if(cipher == null)
{
throw new NotFoundException();
}
await _cipherService.DeleteAsync(cipher);
}
}
}