mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
refactor for cipher details, folders, favorites
This commit is contained in:
@ -38,7 +38,7 @@ namespace Bit.Api.Controllers
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new CipherResponseModel(cipher, userId);
|
||||
return new CipherResponseModel(cipher);
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
@ -46,25 +46,25 @@ namespace Bit.Api.Controllers
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var ciphers = await _cipherRepository.GetManyByUserIdAsync(userId);
|
||||
var responses = ciphers.Select(c => new CipherResponseModel(c, userId));
|
||||
var responses = ciphers.Select(c => new CipherResponseModel(c));
|
||||
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);
|
||||
}
|
||||
//[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.ToCipher(userId)).ToList();
|
||||
var folderCiphers = model.Folders.Select(f => f.ToFolder(userId)).ToList();
|
||||
var otherCiphers = model.Logins.Select(s => s.ToCipher(userId)).ToList();
|
||||
|
||||
await _cipherService.ImportCiphersAsync(
|
||||
@ -73,20 +73,20 @@ namespace Bit.Api.Controllers
|
||||
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();
|
||||
}
|
||||
//[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;
|
||||
// cipher.Favorite = !cipher.Favorite;
|
||||
|
||||
await _cipherService.SaveAsync(cipher);
|
||||
}
|
||||
// await _cipherService.SaveAsync(cipher);
|
||||
//}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[HttpPost("{id}/delete")]
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -7,7 +6,6 @@ using Bit.Core.Repositories;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
@ -33,7 +31,7 @@ namespace Bit.Api.Controllers
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<LoginResponseModel> Get(string id, string[] expand = null)
|
||||
public async Task<LoginResponseModel> Get(string id)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
|
||||
@ -42,37 +40,34 @@ namespace Bit.Api.Controllers
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var response = new LoginResponseModel(login, userId);
|
||||
await ExpandAsync(login, response, expand, null, userId);
|
||||
var response = new LoginResponseModel(login);
|
||||
return response;
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ListResponseModel<LoginResponseModel>> Get(string[] expand = null)
|
||||
public async Task<ListResponseModel<LoginResponseModel>> Get()
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
|
||||
userId);
|
||||
var responses = logins.Select(s => new LoginResponseModel(s, userId)).ToList();
|
||||
await ExpandManyAsync(logins, responses, expand, null, userId);
|
||||
var responses = logins.Select(s => new LoginResponseModel(s)).ToList();
|
||||
return new ListResponseModel<LoginResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpPost("")]
|
||||
public async Task<LoginResponseModel> Post([FromBody]LoginRequestModel model, string[] expand = null)
|
||||
public async Task<LoginResponseModel> Post([FromBody]LoginRequestModel model)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var login = model.ToCipher(userId);
|
||||
await _cipherService.SaveAsync(login);
|
||||
|
||||
var response = new LoginResponseModel(login, userId);
|
||||
await ExpandAsync(login, response, expand, null, userId);
|
||||
var response = new LoginResponseModel(login);
|
||||
return response;
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[HttpPost("{id}")]
|
||||
public async Task<LoginResponseModel> Put(string id, [FromBody]LoginRequestModel model, string[] expand = null)
|
||||
public async Task<LoginResponseModel> Put(string id, [FromBody]LoginRequestModel model)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var login = await _cipherRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
||||
@ -83,8 +78,7 @@ namespace Bit.Api.Controllers
|
||||
|
||||
await _cipherService.SaveAsync(model.ToCipher(login));
|
||||
|
||||
var response = new LoginResponseModel(login, userId);
|
||||
await ExpandAsync(login, response, expand, null, userId);
|
||||
var response = new LoginResponseModel(login);
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -100,61 +94,5 @@ namespace Bit.Api.Controllers
|
||||
|
||||
await _cipherService.DeleteAsync(login);
|
||||
}
|
||||
|
||||
private async Task ExpandAsync(Cipher login, LoginResponseModel response, string[] expand, Cipher folder, Guid userId)
|
||||
{
|
||||
if(expand == null || expand.Count() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(expand.Any(e => e.ToLower() == "folder") && login.FolderId.HasValue)
|
||||
{
|
||||
if(folder == null)
|
||||
{
|
||||
folder = await _cipherRepository.GetByIdAsync(login.FolderId.Value);
|
||||
}
|
||||
|
||||
response.Folder = new FolderResponseModel(folder, userId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExpandManyAsync(IEnumerable<Cipher> logins, ICollection<LoginResponseModel> responses,
|
||||
string[] expand, IEnumerable<Cipher> folders, Guid userId)
|
||||
{
|
||||
if(expand == null || expand.Count() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(expand.Any(e => e.ToLower() == "folder"))
|
||||
{
|
||||
if(folders == null)
|
||||
{
|
||||
folders = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Folder,
|
||||
_userService.GetProperUserId(User).Value);
|
||||
}
|
||||
|
||||
if(folders != null && folders.Count() > 0)
|
||||
{
|
||||
foreach(var response in responses)
|
||||
{
|
||||
var login = logins.SingleOrDefault(s => s.Id.ToString() == response.Id);
|
||||
if(login == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var folder = folders.SingleOrDefault(f => f.Id == login.FolderId);
|
||||
if(folder == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
response.Folder = new FolderResponseModel(folder, userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user