1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00

created ciphers API controller. Moved import to ciphers controller.

This commit is contained in:
Kyle Spearrin 2016-06-07 20:05:27 -04:00
parent 585d7b4afd
commit 6861303586
5 changed files with 107 additions and 11 deletions

View File

@ -202,15 +202,6 @@ namespace Bit.Api.Controllers
return response;
}
[HttpPost("import")]
public async Task PostImport([FromBody]ImportRequestModel model)
{
await _cipherService.ImportCiphersAsync(
model.Folders.Select(f => f.ToCipher(_userManager.GetUserId(User))).ToList(),
model.Sites.Select(s => s.ToCipher(_userManager.GetUserId(User))).ToList(),
model.SiteRelationships);
}
[HttpPost("delete")]
public async Task PostDelete([FromBody]DeleteAccountRequestModel model)
{

View File

@ -0,0 +1,77 @@
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.Domains;
using Microsoft.AspNetCore.Identity;
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 UserManager<User> _userManager;
public CiphersController(
ICipherRepository cipherRepository,
ICipherService cipherService,
UserManager<User> userManager)
{
_cipherRepository = cipherRepository;
_cipherService = cipherService;
_userManager = userManager;
}
[HttpGet("{id}")]
public async Task<CipherResponseModel> Get(string id)
{
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User)));
if(cipher == null)
{
throw new NotFoundException();
}
return new CipherResponseModel(cipher);
}
[HttpGet("")]
public async Task<ListResponseModel<CipherResponseModel>> Get()
{
var ciphers = await _cipherRepository.GetManyByUserIdAsync(new Guid(_userManager.GetUserId(User)));
var responses = ciphers.Select(c => new CipherResponseModel(c));
return new ListResponseModel<CipherResponseModel>(responses);
}
[HttpPost("import")]
public async Task PostImport([FromBody]ImportRequestModel model)
{
var folderCiphers = model.Folders.Select(f => f.ToCipher(_userManager.GetUserId(User))).ToList();
var otherCiphers = model.Sites.Select(s => s.ToCipher(_userManager.GetUserId(User))).ToList();
await _cipherService.ImportCiphersAsync(
folderCiphers,
otherCiphers,
model.FolderRelationships);
}
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User)));
if(cipher == null)
{
throw new NotFoundException();
}
await _cipherRepository.DeleteAsync(cipher);
}
}
}

View File

@ -32,6 +32,5 @@
public string Username { get; set; }
public string Password { get; set; }
public string Notes { get; set; }
}
}

View File

@ -7,6 +7,6 @@ namespace Bit.Api.Models
{
public FolderRequestModel[] Folders { get; set; }
public SiteRequestModel[] Sites { get; set; }
public KeyValuePair<int, int>[] SiteRelationships { get; set; }
public KeyValuePair<int, int>[] FolderRelationships { get; set; }
}
}

View File

@ -0,0 +1,29 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class CipherResponseModel : ResponseModel
{
public CipherResponseModel(Cipher cipher)
: base("cipher")
{
if(cipher == null)
{
throw new ArgumentNullException(nameof(cipher));
}
Id = cipher.Id.ToString();
FolderId = cipher.FolderId?.ToString();
Type = cipher.Type;
Data = cipher.Data;
RevisionDate = cipher.RevisionDate;
}
public string Id { get; set; }
public string FolderId { get; set; }
public Core.Enums.CipherType Type { get; set; }
public string Data { get; set; }
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
}
}