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; + } +}