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

remove old share solution code

This commit is contained in:
Kyle Spearrin 2017-02-28 22:51:29 -05:00
parent 321183c570
commit acb1fc0be5
17 changed files with 19 additions and 365 deletions

View File

@ -31,25 +31,25 @@ namespace Bit.Api.Controllers
}
[HttpGet("{id}")]
public async Task<CipherShareResponseModel> Get(string id)
public async Task<CipherResponseModel> Get(string id)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetShareByIdAsync(new Guid(id), userId);
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
return new CipherShareResponseModel(cipher, userId);
return new CipherResponseModel(cipher, userId);
}
[HttpGet("")]
public async Task<ListResponseModel<CipherShareResponseModel>> Get()
public async Task<ListResponseModel<CipherResponseModel>> Get()
{
var userId = _userService.GetProperUserId(User).Value;
var ciphers = await _cipherRepository.GetManyShareByUserIdAsync(userId);
var responses = ciphers.Select(c => new CipherShareResponseModel(c, userId));
return new ListResponseModel<CipherShareResponseModel>(responses);
var ciphers = await _cipherRepository.GetManyByUserIdAsync(userId);
var responses = ciphers.Select(c => new CipherResponseModel(c, userId));
return new ListResponseModel<CipherResponseModel>(responses);
}
[Obsolete]

View File

@ -33,29 +33,29 @@ namespace Bit.Api.Controllers
}
[HttpGet("{id}")]
public async Task<LoginShareResponseModel> Get(string id, string[] expand = null)
public async Task<LoginResponseModel> Get(string id, string[] expand = null)
{
var userId = _userService.GetProperUserId(User).Value;
var login = await _cipherRepository.GetShareByIdAsync(new Guid(id), userId);
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(login == null || login.Type != Core.Enums.CipherType.Login)
{
throw new NotFoundException();
}
var response = new LoginShareResponseModel(login, userId);
var response = new LoginResponseModel(login, userId);
await ExpandAsync(login, response, expand, null, userId);
return response;
}
[HttpGet("")]
public async Task<ListResponseModel<LoginShareResponseModel>> Get(string[] expand = null)
public async Task<ListResponseModel<LoginResponseModel>> Get(string[] expand = null)
{
var userId = _userService.GetProperUserId(User).Value;
var logins = await _cipherRepository.GetManyShareByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
var logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
userId);
var responses = logins.Select(s => new LoginShareResponseModel(s, userId)).ToList();
var responses = logins.Select(s => new LoginResponseModel(s, userId)).ToList();
await ExpandManyAsync(logins, responses, expand, null, userId);
return new ListResponseModel<LoginShareResponseModel>(responses);
return new ListResponseModel<LoginResponseModel>(responses);
}
[HttpPost("")]
@ -119,8 +119,8 @@ namespace Bit.Api.Controllers
}
}
private async Task ExpandManyAsync<TResponseModel>(IEnumerable<Cipher> logins, ICollection<TResponseModel> responses,
string[] expand, IEnumerable<Cipher> folders, Guid userId) where TResponseModel : LoginResponseModel
private async Task ExpandManyAsync(IEnumerable<Cipher> logins, ICollection<LoginResponseModel> responses,
string[] expand, IEnumerable<Cipher> folders, Guid userId)
{
if(expand == null || expand.Count() == 0)
{

View File

@ -1,83 +0,0 @@
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.Services;
using System.Collections.Generic;
namespace Bit.Api.Controllers
{
[Route("shares")]
[Authorize("Application")]
public class SharesController : Controller
{
private readonly IShareRepository _shareRepository;
private readonly IUserService _userService;
private readonly ICipherService _cipherService;
public SharesController(
IShareRepository shareRepository,
IUserService userService,
ICipherService cipherService)
{
_shareRepository = shareRepository;
_userService = userService;
_cipherService = cipherService;
}
[HttpGet("{id}")]
public async Task<ShareResponseModel> Get(string id)
{
var userId = _userService.GetProperUserId(User).Value;
var share = await _shareRepository.GetByIdAsync(new Guid(id), userId);
if(share == null)
{
throw new NotFoundException();
}
return new ShareResponseModel(share);
}
//[HttpGet("cipher/{cipherId}")]
//public async Task<IEnumerable<ShareResponseModel>> GetCipher(string cipherId)
//{
// var userId = _userService.GetProperUserId(User).Value;
// var share = await _shareRepository.GetByIdAsync(new Guid(cipherId), userId);
// if(share == null)
// {
// throw new NotFoundException();
// }
// return new ShareResponseModel(share);
//}
[HttpPost("")]
public async Task<ShareResponseModel> Post([FromBody]ShareRequestModel model)
{
var share = model.ToShare(_userService.GetProperUserId(User).Value);
await _cipherService.ShareAsync(share, model.Email);
var response = new ShareResponseModel(share);
return response;
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
public async Task Delete(string id)
{
var share = await _shareRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
if(share == null)
{
throw new NotFoundException();
}
// TODO: permission checks
await _shareRepository.DeleteAsync(share);
}
}
}

View File

@ -1,33 +0,0 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Core.Domains;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
public class ShareRequestModel
{
[Required]
public string Email { get; set; }
[Required]
[StringLength(36)]
public string CipherId { get; set; }
public string Key { get; set; }
public Share ToShare(Guid sharerUserId)
{
return ToShare(new Share
{
SharerUserId = sharerUserId
});
}
public Share ToShare(Share existingShare)
{
existingShare.CipherId = new Guid(CipherId);
existingShare.Key = Key;
return existingShare;
}
}
}

View File

@ -1,6 +1,5 @@
using System;
using Bit.Core.Domains;
using Bit.Core.Models.Data;
namespace Bit.Api.Models
{
@ -19,7 +18,6 @@ namespace Bit.Api.Models
Type = cipher.Type;
Favorite = cipher.Favorite;
RevisionDate = cipher.RevisionDate;
Key = cipher.Key;
switch(cipher.Type)
{
@ -38,21 +36,7 @@ namespace Bit.Api.Models
public string FolderId { get; set; }
public Core.Enums.CipherType Type { get; set; }
public bool Favorite { get; set; }
public string Key { get; set; }
public dynamic Data { get; set; }
public DateTime RevisionDate { get; set; }
}
public class CipherShareResponseModel : CipherResponseModel
{
public CipherShareResponseModel(CipherShare cipherShare, Guid userId)
: base(cipherShare, userId, "cipherShare")
{
ReadOnly = cipherShare.ReadOnly;
Status = cipherShare.Status;
}
public bool ReadOnly { get; set; }
public Core.Enums.ShareStatusType? Status { get; set; }
}
}

View File

@ -1,13 +1,12 @@
using System;
using Bit.Core.Domains;
using Bit.Core.Models.Data;
namespace Bit.Api.Models
{
public class LoginResponseModel : ResponseModel
{
public LoginResponseModel(Cipher cipher, Guid userId, string obj = "login")
: base("login")
: base(obj)
{
if(cipher == null)
{
@ -30,7 +29,6 @@ namespace Bit.Api.Models
Password = data.Password;
Notes = data.Notes;
RevisionDate = cipher.RevisionDate;
Key = cipher.Key;
}
public string Id { get; set; }
@ -42,22 +40,8 @@ namespace Bit.Api.Models
public string Password { get; set; }
public string Notes { get; set; }
public DateTime RevisionDate { get; set; }
public string Key { get; set; }
// Expandables
public FolderResponseModel Folder { get; set; }
}
public class LoginShareResponseModel : LoginResponseModel
{
public LoginShareResponseModel(CipherShare cipherShare, Guid userId)
: base(cipherShare, userId, "loginShare")
{
ReadOnly = cipherShare.ReadOnly;
Status = cipherShare.Status;
}
public bool ReadOnly { get; set; }
public Core.Enums.ShareStatusType? Status { get; set; }
}
}

View File

@ -1,33 +0,0 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class ShareResponseModel : ResponseModel
{
public ShareResponseModel(Share share)
: base("share")
{
if(share == null)
{
throw new ArgumentNullException(nameof(share));
}
Id = share.Id.ToString();
UserId = share.UserId.ToString();
SharerUserId = share.SharerUserId.ToString();
CipherId = share.CipherId.ToString();
Key = Key;
ReadOnly = share.ReadOnly;
Status = share.Status;
}
public string Id { get; set; }
public string UserId { get; set; }
public string SharerUserId { get; set; }
public string CipherId { get; set; }
public string Key { get; set; }
public bool ReadOnly { get; set; }
public Core.Enums.ShareStatusType? Status { get; set; }
}
}

View File

@ -79,7 +79,6 @@ namespace Bit.Api
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
services.AddSingleton<IShareRepository, SqlServerRepos.ShareRepository>();
// Context
services.AddScoped<CurrentContext>();

View File

@ -10,7 +10,6 @@ namespace Bit.Core.Domains
public Guid? FolderId { get; set; }
public Enums.CipherType Type { get; set; }
public bool Favorite { get; set; }
public string Key { get; set; }
public string Data { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -1,23 +0,0 @@
using System;
using Bit.Core.Utilities;
namespace Bit.Core.Domains
{
public class Share : IDataObject<Guid>
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public Guid SharerUserId { get; set; }
public Guid CipherId { get; set; }
public string Key { get; set; }
public bool ReadOnly { get; set; }
public Enums.ShareStatusType Status { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public void SetNewId()
{
Id = CoreHelpers.GenerateComb();
}
}
}

View File

@ -1,10 +0,0 @@
using Bit.Core.Domains;
namespace Bit.Core.Models.Data
{
public class CipherShare : Cipher
{
public bool ReadOnly { get; internal set; }
public Enums.ShareStatusType? Status { get; internal set; }
}
}

View File

@ -2,20 +2,16 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Core.Domains;
using Bit.Core.Models.Data;
namespace Bit.Core.Repositories
{
public interface ICipherRepository : IRepository<Cipher, Guid>
{
Task<Cipher> GetByIdAsync(Guid id, Guid userId);
Task<CipherShare> GetShareByIdAsync(Guid id, Guid userId);
Task<ICollection<Cipher>> GetManyByUserIdAsync(Guid userId);
Task<ICollection<CipherShare>> GetManyShareByUserIdAsync(Guid userId);
Task<ICollection<Cipher>> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId);
Task<ICollection<CipherShare>> GetManyShareByTypeAndUserIdAsync(Enums.CipherType type, Guid userId);
Task<Tuple<ICollection<Cipher>, ICollection<Guid>>>
GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(DateTime sinceRevisionDate, Guid userId);
Task<Tuple<ICollection<Cipher>, ICollection<Guid>>> GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(
DateTime sinceRevisionDate, Guid userId);
Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers);
Task CreateAsync(IEnumerable<Cipher> ciphers);
}

View File

@ -1,13 +0,0 @@
using System;
using Bit.Core.Domains;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Bit.Core.Repositories
{
public interface IShareRepository : IRepository<Share, Guid>
{
Task<Share> GetByIdAsync(Guid id, Guid userId);
Task<ICollection<Share>> GetManyByCipherId(Guid id);
}
}

View File

@ -7,7 +7,6 @@ using DataTableProxy;
using Bit.Core.Domains;
using System.Data;
using Dapper;
using Bit.Core.Models.Data;
namespace Bit.Core.Repositories.SqlServer
{
@ -32,19 +31,6 @@ namespace Bit.Core.Repositories.SqlServer
return cipher;
}
public async Task<CipherShare> GetShareByIdAsync(Guid id, Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherShare>(
$"[{Schema}].[CipherShare_ReadById]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.FirstOrDefault(c => c.UserId == userId);
}
}
public async Task<ICollection<Cipher>> GetManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
@ -58,19 +44,6 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<CipherShare>> GetManyShareByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherShare>(
$"[{Schema}].[CipherShare_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<ICollection<Cipher>> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
@ -88,23 +61,6 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task<ICollection<CipherShare>> GetManyShareByTypeAndUserIdAsync(Enums.CipherType type, Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<CipherShare>(
$"[{Schema}].[CipherShare_ReadByTypeUserId]",
new
{
Type = type,
UserId = userId
},
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<Tuple<ICollection<Cipher>, ICollection<Guid>>>
GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(DateTime sinceRevisionDate, Guid userId)
{

View File

@ -1,46 +0,0 @@
using System;
using Bit.Core.Domains;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Data.SqlClient;
using Dapper;
using System.Data;
using System.Linq;
namespace Bit.Core.Repositories.SqlServer
{
public class ShareRepository : Repository<Share, Guid>, IShareRepository
{
public ShareRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString)
{ }
public ShareRepository(string connectionString)
: base(connectionString)
{ }
public async Task<Share> GetByIdAsync(Guid id, Guid userId)
{
var share = await GetByIdAsync(id);
if(share == null || (share.UserId != userId && share.SharerUserId != userId))
{
return null;
}
return share;
}
public async Task<ICollection<Share>> GetManyByCipherId(Guid cipherId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Share>(
$"[{Schema}].[Share_ReadByCipherId]",
new { CipherId = cipherId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
}
}

View File

@ -10,6 +10,5 @@ namespace Bit.Core.Services
Task DeleteAsync(Cipher cipher);
Task ImportCiphersAsync(List<Cipher> folders, List<Cipher> ciphers,
IEnumerable<KeyValuePair<int, int>> folderRelationships);
Task ShareAsync(Share share, string email);
}
}

View File

@ -10,18 +10,15 @@ namespace Bit.Core.Services
public class CipherService : ICipherService
{
private readonly ICipherRepository _cipherRepository;
private readonly IShareRepository _shareRepository;
private readonly IUserRepository _userRepository;
private readonly IPushService _pushService;
public CipherService(
ICipherRepository cipherRepository,
IShareRepository shareRepository,
IUserRepository userRepository,
IPushService pushService)
{
_cipherRepository = cipherRepository;
_shareRepository = shareRepository;
_userRepository = userRepository;
_pushService = pushService;
}
@ -90,24 +87,5 @@ namespace Bit.Core.Services
await _pushService.PushSyncCiphersAsync(userId.Value);
}
}
public async Task ShareAsync(Share share, string email)
{
// TODO: Make sure share does not already exist between these two users.
var user = await _userRepository.GetByEmailAsync(email);
if(user == null)
{
return;
}
share.UserId = user.Id;
// TODO: Permissions and status
share.ReadOnly = false;
share.Status = Enums.ShareStatusType.Accepted;
await _shareRepository.CreateAsync(share);
}
}
}