From 6861303586e88e2930820cfea8d7ef019aa1627b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 7 Jun 2016 20:05:27 -0400 Subject: [PATCH] created ciphers API controller. Moved import to ciphers controller. --- src/Api/Controllers/AccountsController.cs | 9 --- src/Api/Controllers/CiphersController.cs | 77 +++++++++++++++++++ src/Api/Models/CipherDataModel.cs | 1 - .../Request/Accounts/ImportRequestModel.cs | 2 +- .../Models/Response/CipherResponseModel.cs | 29 +++++++ 5 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 src/Api/Controllers/CiphersController.cs create mode 100644 src/Api/Models/Response/CipherResponseModel.cs diff --git a/src/Api/Controllers/AccountsController.cs b/src/Api/Controllers/AccountsController.cs index 8fe3619b6..9bb1e791c 100644 --- a/src/Api/Controllers/AccountsController.cs +++ b/src/Api/Controllers/AccountsController.cs @@ -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) { diff --git a/src/Api/Controllers/CiphersController.cs b/src/Api/Controllers/CiphersController.cs new file mode 100644 index 000000000..57d0710fd --- /dev/null +++ b/src/Api/Controllers/CiphersController.cs @@ -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 _userManager; + + public CiphersController( + ICipherRepository cipherRepository, + ICipherService cipherService, + UserManager userManager) + { + _cipherRepository = cipherRepository; + _cipherService = cipherService; + _userManager = userManager; + } + + [HttpGet("{id}")] + public async Task 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> Get() + { + var ciphers = await _cipherRepository.GetManyByUserIdAsync(new Guid(_userManager.GetUserId(User))); + var responses = ciphers.Select(c => new CipherResponseModel(c)); + return new ListResponseModel(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); + } + } +} diff --git a/src/Api/Models/CipherDataModel.cs b/src/Api/Models/CipherDataModel.cs index 11498769a..b8ed7a4ad 100644 --- a/src/Api/Models/CipherDataModel.cs +++ b/src/Api/Models/CipherDataModel.cs @@ -32,6 +32,5 @@ public string Username { get; set; } public string Password { get; set; } public string Notes { get; set; } - } } diff --git a/src/Api/Models/Request/Accounts/ImportRequestModel.cs b/src/Api/Models/Request/Accounts/ImportRequestModel.cs index 01c4e81a5..9c7d36ac9 100644 --- a/src/Api/Models/Request/Accounts/ImportRequestModel.cs +++ b/src/Api/Models/Request/Accounts/ImportRequestModel.cs @@ -7,6 +7,6 @@ namespace Bit.Api.Models { public FolderRequestModel[] Folders { get; set; } public SiteRequestModel[] Sites { get; set; } - public KeyValuePair[] SiteRelationships { get; set; } + public KeyValuePair[] FolderRelationships { get; set; } } } diff --git a/src/Api/Models/Response/CipherResponseModel.cs b/src/Api/Models/Response/CipherResponseModel.cs new file mode 100644 index 000000000..369651286 --- /dev/null +++ b/src/Api/Models/Response/CipherResponseModel.cs @@ -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; + } +}